qtmobility/src/sensors/qsensorbackend.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include "qsensorbackend.h"
    42 #include "qsensorbackend.h"
    43 #include "qsensor_p.h"
    43 #include "qsensor_p.h"
       
    44 #include <QDebug>
    44 
    45 
    45 QTM_BEGIN_NAMESPACE
    46 QTM_BEGIN_NAMESPACE
    46 
    47 
    47 /*!
    48 /*!
    48     \class QSensorBackend
    49     \class QSensorBackend
    66 /*!
    67 /*!
    67     \internal
    68     \internal
    68 */
    69 */
    69 QSensorBackend::~QSensorBackend()
    70 QSensorBackend::~QSensorBackend()
    70 {
    71 {
    71 }
       
    72 
       
    73 /*!
       
    74     Set the supported \a policies for this sensor.
       
    75     If this isn't called the sensor will not report
       
    76     any supported policies to the user.
       
    77 */
       
    78 void QSensorBackend::setSupportedUpdatePolicies(QSensor::UpdatePolicies policies)
       
    79 {
       
    80     QSensorPrivate *d = m_sensor->d_func();
       
    81     d->supportedUpdatePolicies = policies;
       
    82 }
    72 }
    83 
    73 
    84 /*!
    74 /*!
    85     Notify the QSensor class that a new reading is available.
    75     Notify the QSensor class that a new reading is available.
    86 */
    76 */
    98     }
    88     }
    99 
    89 
   100     // Copy the values from the filter reading to the cached reading
    90     // Copy the values from the filter reading to the cached reading
   101     d->cache_reading->copyValuesFrom(d->filter_reading);
    91     d->cache_reading->copyValuesFrom(d->filter_reading);
   102 
    92 
   103     if (d->updatePolicy == QSensor::PolledUpdates)
    93     Q_EMIT m_sensor->readingChanged();
   104         return; // We don't emit the signal if we're polling
       
   105 
       
   106     if (d->signalEnabled)
       
   107         Q_EMIT m_sensor->readingChanged();
       
   108 }
    94 }
   109 
    95 
   110 /*!
    96 /*!
   111     \fn QSensorBackend::start()
    97     \fn QSensorBackend::start()
   112 
    98 
   115 
   101 
   116 /*!
   102 /*!
   117     \fn QSensorBackend::stop()
   103     \fn QSensorBackend::stop()
   118 
   104 
   119     Stop reporting values.
   105     Stop reporting values.
   120 */
       
   121 
       
   122 /*!
       
   123     \fn QSensorBackend::poll()
       
   124 
       
   125     Poll the sensor for a reading.
       
   126 */
   106 */
   127 
   107 
   128 /*!
   108 /*!
   129     If the backend has lost its reference to the reading
   109     If the backend has lost its reference to the reading
   130     it can call this method to get the address.
   110     it can call this method to get the address.
   137 QSensorReading *QSensorBackend::reading() const
   117 QSensorReading *QSensorBackend::reading() const
   138 {
   118 {
   139     QSensorPrivate *d = m_sensor->d_func();
   119     QSensorPrivate *d = m_sensor->d_func();
   140     return d->device_reading;
   120     return d->device_reading;
   141 }
   121 }
       
   122 
       
   123 /*!
       
   124     \fn QSensorBackend::sensor() const
       
   125 
       
   126     Returns the sensor front end associated with this backend.
       
   127 */
   142 
   128 
   143 /*!
   129 /*!
   144     \fn QSensorBackend::setReading(T *reading)
   130     \fn QSensorBackend::setReading(T *reading)
   145 
   131 
   146     This function is called to initialize the \a reading
   132     This function is called to initialize the \a reading
   213     d->device_reading = device;
   199     d->device_reading = device;
   214     d->filter_reading = filter;
   200     d->filter_reading = filter;
   215     d->cache_reading = cache;
   201     d->cache_reading = cache;
   216 }
   202 }
   217 
   203 
       
   204 /*!
       
   205     Add a data rate (consisting of \a min and \a max values) for the sensor.
       
   206 
       
   207     Note that this function should be called from the constructor so that the information
       
   208     is available immediately.
       
   209 
       
   210     \sa QSensor::availableDataRates
       
   211 */
       
   212 void QSensorBackend::addDataRate(qreal min, qreal max)
       
   213 {
       
   214     QSensorPrivate *d = m_sensor->d_func();
       
   215     d->availableDataRates << qrange(min, max);
       
   216 }
       
   217 
       
   218 /*!
       
   219     Set the data rates for the sensor based on \a otherSensor.
       
   220 
       
   221     This is designed for sensors that are based on other sensors.
       
   222 
       
   223     \code
       
   224     setDataRates(otherSensor);
       
   225     \endcode
       
   226 
       
   227     Note that this function should be called from the constructor so that the information
       
   228     is available immediately.
       
   229 
       
   230     \sa QSensor::availableDataRates, addDataRate()
       
   231 */
       
   232 void QSensorBackend::setDataRates(const QSensor *otherSensor)
       
   233 {
       
   234     if (!otherSensor) {
       
   235         qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with 0";
       
   236         return;
       
   237     }
       
   238     if (otherSensor->identifier().count() == 0) {
       
   239         qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor";
       
   240         return;
       
   241     }
       
   242     QSensorPrivate *d = m_sensor->d_func();
       
   243     d->availableDataRates = otherSensor->availableDataRates();
       
   244     d->dataRate = otherSensor->dataRate();
       
   245     if (d->availableDataRates.count() == 0) {
       
   246         qWarning() << otherSensor->identifier() << "backend does not support any data rates.";
       
   247     }
       
   248 }
       
   249 
       
   250 /*!
       
   251     Add an output range (consisting of \a min, \a max values and \a accuracy) for the sensor.
       
   252 
       
   253     Note that this function should be called from the constructor so that the information
       
   254     is available immediately.
       
   255 
       
   256     \sa QSensor::outputRange, QSensor::outputRanges
       
   257 */
       
   258 void QSensorBackend::addOutputRange(qreal min, qreal max, qreal accuracy)
       
   259 {
       
   260     QSensorPrivate *d = m_sensor->d_func();
       
   261 
       
   262     qoutputrange details = {min, max, accuracy};
       
   263 
       
   264     d->outputRanges << details;
       
   265 
       
   266     // When adding the first range, set outputRage to it
       
   267     if (d->outputRange == -1) {
       
   268         d->outputRange = 0;
       
   269     }
       
   270 }
       
   271 
       
   272 /*!
       
   273     Set the \a description for the sensor.
       
   274 
       
   275     Note that this function should be called from the constructor so that the information
       
   276     is available immediately.
       
   277 */
       
   278 void QSensorBackend::setDescription(const QString &description)
       
   279 {
       
   280     QSensorPrivate *d = m_sensor->d_func();
       
   281     d->description = description;
       
   282 }
       
   283 
       
   284 /*!
       
   285     Inform the front end that the sensor has stopped.
       
   286     This can be due to start() failing or for some
       
   287     unexpected reason (eg. hardware failure).
       
   288 
       
   289     Note that the front end must call QSensor::isActive() to see if
       
   290     the sensor has stopped. If the sensor has stopped due to an error
       
   291     the sensorError() function should be called to notify the class
       
   292     of the error condition.
       
   293 */
       
   294 void QSensorBackend::sensorStopped()
       
   295 {
       
   296     QSensorPrivate *d = m_sensor->d_func();
       
   297     d->active = false;
       
   298 }
       
   299 
       
   300 /*!
       
   301     Inform the front end that the sensor is busy.
       
   302     This implicitly calls sensorStopped() and
       
   303     is typically called from start().
       
   304 
       
   305     Note that the front end must call QSensor::isBusy() to see if
       
   306     the sensor is busy. If the sensor has stopped due to an error
       
   307     the sensorError() function should be called to notify the class
       
   308     of the error condition.
       
   309 */
       
   310 void QSensorBackend::sensorBusy()
       
   311 {
       
   312     QSensorPrivate *d = m_sensor->d_func();
       
   313     d->active = false;
       
   314     d->busy = true;
       
   315 }
       
   316 
       
   317 /*!
       
   318     Inform the front end that a sensor error occurred.
       
   319     Note that this only reports an \a error code. It does
       
   320     not stop the sensor.
       
   321 
       
   322     \sa sensorStopped()
       
   323 */
       
   324 void QSensorBackend::sensorError(int error)
       
   325 {
       
   326     QSensorPrivate *d = m_sensor->d_func();
       
   327     d->error = error;
       
   328     Q_EMIT m_sensor->sensorError(error);
       
   329 }
       
   330 
   218 #include "moc_qsensorbackend.cpp"
   331 #include "moc_qsensorbackend.cpp"
   219 QTM_END_NAMESPACE
   332 QTM_END_NAMESPACE
   220 
   333