qtmobility/src/sensors/qsensor.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
   177 bool QSensor::connectToBackend()
   177 bool QSensor::connectToBackend()
   178 {
   178 {
   179     if (d->backend)
   179     if (d->backend)
   180         return true;
   180         return true;
   181 
   181 
   182     int rate = d->dataRate;
       
   183     d->backend = QSensorManager::createBackend(this);
   182     d->backend = QSensorManager::createBackend(this);
   184     if (rate != 0)
       
   185         setDataRate(rate);
       
   186     return (d->backend != 0);
   183     return (d->backend != 0);
   187 }
   184 }
   188 
   185 
   189 /*!
   186 /*!
   190     \property QSensor::busy
   187     \property QSensor::busy
   251     \property QSensor::dataRate
   248     \property QSensor::dataRate
   252     \brief the data rate that the sensor should be run at.
   249     \brief the data rate that the sensor should be run at.
   253 
   250 
   254     Measured in Hertz.
   251     Measured in Hertz.
   255 
   252 
   256     The default value is determined by the backend.
   253     The data rate is the maximum frequency at which the sensor can detect changes.
       
   254 
       
   255     Setting this property is not portable and can cause conflicts with other
       
   256     applications. Check with the sensor backend and platform documentation for
       
   257     any policy regarding multiple applications requesting a data rate.
       
   258 
       
   259     The default value (0) means that the app does not care what the data rate is.
       
   260     Applications should consider using a timer-based poll of the current value or
       
   261     ensure that the code that processes values can run very quickly as the platform
       
   262     may provide updates hundreds of times each second.
   257 
   263 
   258     This should be set before calling start() because the sensor may not
   264     This should be set before calling start() because the sensor may not
   259     notice changes to this value while it is running.
   265     notice changes to this value while it is running.
   260 
   266 
       
   267     Note that there is no mechanism to determine the current data rate in use by the
       
   268     platform.
       
   269 
   261     \sa QSensor::availableDataRates
   270     \sa QSensor::availableDataRates
   262 */
   271 */
   263 
   272 
   264 int QSensor::dataRate() const
   273 int QSensor::dataRate() const
   265 {
   274 {
   266     return d->dataRate;
   275     return d->dataRate;
   267 }
   276 }
   268 
   277 
   269 void QSensor::setDataRate(int rate)
   278 void QSensor::setDataRate(int rate)
   270 {
   279 {
       
   280     if (rate == 0) {
       
   281         d->dataRate = rate;
       
   282         return;
       
   283     }
   271     bool warn = true;
   284     bool warn = true;
   272     Q_FOREACH (const qrange &range, d->availableDataRates) {
   285     Q_FOREACH (const qrange &range, d->availableDataRates) {
   273         if (rate >= range.first && rate <= range.second) {
   286         if (rate >= range.first && rate <= range.second) {
   274             warn = false;
   287             warn = false;
   275             d->dataRate = rate;
   288             d->dataRate = rate;
   293 {
   306 {
   294     if (d->active)
   307     if (d->active)
   295         return true;
   308         return true;
   296     if (!connectToBackend())
   309     if (!connectToBackend())
   297         return false;
   310         return false;
   298     if (d->availableDataRates.count() == 0)
       
   299         return false;
       
   300     // Set these flags to their defaults
   311     // Set these flags to their defaults
   301     d->active = true;
   312     d->active = true;
   302     d->busy = false;
   313     d->busy = false;
   303     // Backend will update the flags appropriately
   314     // Backend will update the flags appropriately
   304     d->backend->start();
   315     d->backend->start();
   396 
   407 
   397 /*!
   408 /*!
   398     \property QSensor::outputRange
   409     \property QSensor::outputRange
   399     \brief the output range in use by the sensor.
   410     \brief the output range in use by the sensor.
   400 
   411 
   401     A sensor may have more than one output range. Typically this is done
   412     This value represents the index in the QSensor::outputRanges list to use.
   402     to give a greater measurement range at the cost of lowering accuracy.
   413 
       
   414     Setting this property is not portable and can cause conflicts with other
       
   415     applications. Check with the sensor backend and platform documentation for
       
   416     any policy regarding multiple applications requesting an output range.
       
   417 
       
   418     The default value (-1) means that the app does not care what the output range is.
       
   419 
       
   420     Note that there is no mechanism to determine the current output range in use by the
       
   421     platform.
   403 
   422 
   404     \sa QSensor::outputRanges
   423     \sa QSensor::outputRanges
   405 */
   424 */
   406 
   425 
   407 int QSensor::outputRange() const
   426 int QSensor::outputRange() const
   409     return d->outputRange;
   428     return d->outputRange;
   410 }
   429 }
   411 
   430 
   412 void QSensor::setOutputRange(int index)
   431 void QSensor::setOutputRange(int index)
   413 {
   432 {
   414     if (index < 0 || index >= d->outputRanges.count()) {
   433     if (index < -1 || index >= d->outputRanges.count()) {
   415         qWarning() << "ERROR: Output range" << index << "is not valid";
   434         qWarning() << "ERROR: Output range" << index << "is not valid";
   416         return;
   435         return;
   417     }
   436     }
   418     d->outputRange = index;
   437     d->outputRange = index;
   419 }
   438 }
   527 /*!
   546 /*!
   528     \internal
   547     \internal
   529 */
   548 */
   530 QSensorReading::QSensorReading(QObject *parent, QSensorReadingPrivate *_d)
   549 QSensorReading::QSensorReading(QObject *parent, QSensorReadingPrivate *_d)
   531     : QObject(parent)
   550     : QObject(parent)
   532     , d(_d)
   551     , d(_d?_d:new QSensorReadingPrivate)
   533 {
   552 {
   534 }
   553 }
   535 
       
   536 /*!
       
   537     \fn QSensorReading::d_ptr()
       
   538     \internal
       
   539 */
       
   540 
   554 
   541 /*!
   555 /*!
   542     \internal
   556     \internal
   543 */
   557 */
   544 QSensorReading::~QSensorReading()
   558 QSensorReading::~QSensorReading()
   636     Copy values from other into this reading. Implemented by sub-classes
   650     Copy values from other into this reading. Implemented by sub-classes
   637     using the DECLARE_READING() and IMPLEMENT_READING() macros.
   651     using the DECLARE_READING() and IMPLEMENT_READING() macros.
   638 
   652 
   639     Note that this method should only be called by QSensorBackend.
   653     Note that this method should only be called by QSensorBackend.
   640 */
   654 */
       
   655 void QSensorReading::copyValuesFrom(QSensorReading *other)
       
   656 {
       
   657     QSensorReadingPrivate *my_ptr = d.data();
       
   658     QSensorReadingPrivate *other_ptr = other->d.data();
       
   659     /* Do a direct copy of the private class */
       
   660     *(my_ptr) = *(other_ptr);
       
   661 }
   641 
   662 
   642 /*!
   663 /*!
   643     \macro DECLARE_READING(classname)
   664     \macro DECLARE_READING(classname)
   644     \relates QSensorReading
   665     \relates QSensorReading
   645     \brief The DECLARE_READING macro adds some required methods to a reading class.
   666     \brief The DECLARE_READING macro adds some required methods to a reading class.