diff options
author | Lincoln Ramsay <lincoln.ramsay@nokia.com> | 2010-03-24 14:14:17 +1000 |
---|---|---|
committer | Lincoln Ramsay <lincoln.ramsay@nokia.com> | 2010-03-26 17:13:58 +1000 |
commit | 4edf0c1343ff69bca8a73fa18a994779eb87ed93 (patch) | |
tree | 49ee8e27f8d0c13dba35b113a936e0b229de7223 | |
parent | fca2c910549c82f670164370175d659c9ca1ae2d (diff) |
updateInterval -> dataRate
don't allow setting invalid rates
also warn about backends doing the wrong thing
Some magic so that sensor defaults don't override
stuff the user sets before calling connectToBacked()
unless the user's value is invalid for the sensor.
-rw-r--r-- | examples/sensors/accel/main.cpp | 1 | ||||
-rw-r--r-- | examples/sensors/cubehouse/view.cpp | 1 | ||||
-rw-r--r-- | examples/sensors/grueplugin/gruesensorimpl.cpp | 2 | ||||
-rw-r--r-- | examples/sensors/sensor_explorer/explorer.cpp | 1 | ||||
-rw-r--r-- | plugins/sensors/dummy/dummyaccelerometer.cpp | 2 | ||||
-rw-r--r-- | plugins/sensors/dummy/dummycommon.cpp | 2 | ||||
-rw-r--r-- | plugins/sensors/generic/genericorientationsensor.cpp | 2 | ||||
-rw-r--r-- | plugins/sensors/generic/genericrotationsensor.cpp | 2 | ||||
-rw-r--r-- | plugins/sensors/n900/n900accelerometer.cpp | 6 | ||||
-rw-r--r-- | plugins/sensors/n900/n900filebasedsensor.cpp | 7 | ||||
-rw-r--r-- | plugins/sensors/n900/n900lightsensor.cpp | 3 | ||||
-rw-r--r-- | plugins/sensors/n900/n900proximitysensor.cpp | 3 | ||||
-rw-r--r-- | src/sensors/qsensor.cpp | 37 | ||||
-rw-r--r-- | src/sensors/qsensor.h | 6 | ||||
-rw-r--r-- | src/sensors/qsensor_p.h | 4 | ||||
-rw-r--r-- | src/sensors/qsensorbackend.cpp | 8 | ||||
-rw-r--r-- | src/sensors/qsensormanager.cpp | 15 | ||||
-rw-r--r-- | tests/auto/qsensor/test_sensorimpl.cpp | 14 | ||||
-rw-r--r-- | tests/auto/qsensor/tst_qsensor.cpp | 14 |
19 files changed, 90 insertions, 40 deletions
diff --git a/examples/sensors/accel/main.cpp b/examples/sensors/accel/main.cpp index 200b02405e..39dada5080 100644 --- a/examples/sensors/accel/main.cpp +++ b/examples/sensors/accel/main.cpp @@ -68,7 +68,6 @@ int main(int argc, char **argv) accelerometer = &sensor; AccelerometerFilter filter; sensor.addFilter(&filter); - sensor.setUpdateInterval(100); // as fast as the sensor can go! sensor.start(); if (!sensor.isActive()) { diff --git a/examples/sensors/cubehouse/view.cpp b/examples/sensors/cubehouse/view.cpp index 5958dfb375..6bf0cd8567 100644 --- a/examples/sensors/cubehouse/view.cpp +++ b/examples/sensors/cubehouse/view.cpp @@ -64,7 +64,6 @@ View::View(QWidget *parent) sensor = new QAccelerometer(this); connect(sensor, SIGNAL(readingChanged()), this, SLOT(accelerometerTimeout())); - sensor->setUpdateInterval(10); sensor->start(); time.start(); diff --git a/examples/sensors/grueplugin/gruesensorimpl.cpp b/examples/sensors/grueplugin/gruesensorimpl.cpp index dcbfdbfcd5..fe703025b9 100644 --- a/examples/sensors/grueplugin/gruesensorimpl.cpp +++ b/examples/sensors/grueplugin/gruesensorimpl.cpp @@ -59,7 +59,7 @@ gruesensorimpl::gruesensorimpl(QSensor *sensor) void gruesensorimpl::start() { - lightSensor->setUpdateInterval(sensor()->updateInterval()); + lightSensor->setDataRate(sensor()->dataRate()); lightSensor->start(); } diff --git a/examples/sensors/sensor_explorer/explorer.cpp b/examples/sensors/sensor_explorer/explorer.cpp index c34736adee..3eaf5a4a65 100644 --- a/examples/sensors/sensor_explorer/explorer.cpp +++ b/examples/sensors/sensor_explorer/explorer.cpp @@ -135,7 +135,6 @@ void Explorer::on_sensors_currentItemChanged() qWarning() << "Can't connect to the sensor!"; return; } - m_sensor->setUpdateInterval(200); loadSensorProperties(); loadReading(); diff --git a/plugins/sensors/dummy/dummyaccelerometer.cpp b/plugins/sensors/dummy/dummyaccelerometer.cpp index 5db9137fa0..99abe2e1c5 100644 --- a/plugins/sensors/dummy/dummyaccelerometer.cpp +++ b/plugins/sensors/dummy/dummyaccelerometer.cpp @@ -49,6 +49,8 @@ dummyaccelerometer::dummyaccelerometer(QSensor *sensor) : dummycommon(sensor) { setReading<QAccelerometerReading>(&m_reading); + addDataRate(100, 100); // 100Hz + sensor->setDataRate(100); // default is 100Hz } void dummyaccelerometer::poll() diff --git a/plugins/sensors/dummy/dummycommon.cpp b/plugins/sensors/dummy/dummycommon.cpp index 7f46519474..baf956e296 100644 --- a/plugins/sensors/dummy/dummycommon.cpp +++ b/plugins/sensors/dummy/dummycommon.cpp @@ -60,7 +60,7 @@ void dummycommon::start() if (m_timerid) return; - int interval = sensor()->updateInterval(); + int interval = 1000 / sensor()->dataRate(); if (interval < 0) interval = 1000; diff --git a/plugins/sensors/generic/genericorientationsensor.cpp b/plugins/sensors/generic/genericorientationsensor.cpp index 7ef2d63191..acea82277e 100644 --- a/plugins/sensors/generic/genericorientationsensor.cpp +++ b/plugins/sensors/generic/genericorientationsensor.cpp @@ -57,7 +57,7 @@ genericorientationsensor::genericorientationsensor(QSensor *sensor) void genericorientationsensor::start() { - accelerometer->setUpdateInterval(sensor()->updateInterval()); + accelerometer->setDataRate(sensor()->dataRate()); accelerometer->start(); if (!accelerometer->isActive()) sensorStopped(); diff --git a/plugins/sensors/generic/genericrotationsensor.cpp b/plugins/sensors/generic/genericrotationsensor.cpp index 611e5d3180..143db32d99 100644 --- a/plugins/sensors/generic/genericrotationsensor.cpp +++ b/plugins/sensors/generic/genericrotationsensor.cpp @@ -62,7 +62,7 @@ genericrotationsensor::genericrotationsensor(QSensor *sensor) void genericrotationsensor::start() { - accelerometer->setUpdateInterval(sensor()->updateInterval()); + accelerometer->setDataRate(sensor()->dataRate()); accelerometer->start(); if (!accelerometer->isActive()) sensorStopped(); diff --git a/plugins/sensors/n900/n900accelerometer.cpp b/plugins/sensors/n900/n900accelerometer.cpp index 25b610c676..280b5d5c64 100644 --- a/plugins/sensors/n900/n900accelerometer.cpp +++ b/plugins/sensors/n900/n900accelerometer.cpp @@ -57,6 +57,7 @@ n900accelerometer::n900accelerometer(QSensor *sensor) // Details derived from the kernel driver addDataRate(100, 100); // 100Hz addDataRate(400, 400); // 400Hz + sensor->setDataRate(100); // default is 100Hz addOutputRange(-22.418, 22.418, 0.17651); // 2G addOutputRange(-89.672, 89.672, 0.70608); // 8G setDescription(QLatin1String("lis302dl")); @@ -81,10 +82,7 @@ void n900accelerometer::start() // Configure the rate fd = fopen(rate, "w"); if (!fd) goto error; - if (sensor()->updateInterval() < 10) - fprintf(fd, "400\n"); - else - fprintf(fd, "100\n"); + fprintf(fd, "%d\n", sensor()->dataRate()); fclose(fd); n900filebasedsensor::start(); diff --git a/plugins/sensors/n900/n900filebasedsensor.cpp b/plugins/sensors/n900/n900filebasedsensor.cpp index 7836b3066e..1236d736c7 100644 --- a/plugins/sensors/n900/n900filebasedsensor.cpp +++ b/plugins/sensors/n900/n900filebasedsensor.cpp @@ -56,7 +56,12 @@ void n900filebasedsensor::start() if (m_timerid) return; - int interval = sensor()->updateInterval(); + if (sensor()->dataRate() == 0) { + sensorStopped(); + return; + } + + int interval = 1000 / sensor()->dataRate(); if (interval < 0) interval = 1000; diff --git a/plugins/sensors/n900/n900lightsensor.cpp b/plugins/sensors/n900/n900lightsensor.cpp index d6422a593f..f3a31979dc 100644 --- a/plugins/sensors/n900/n900lightsensor.cpp +++ b/plugins/sensors/n900/n900lightsensor.cpp @@ -54,7 +54,8 @@ n900lightsensor::n900lightsensor(QSensor *sensor) // Sensor takes 12-400ms to complete one reading and is triggered by // a read of the /sys file (no interrupt/timing loop/etc. is used). // Since no continuous operation is possible, don't set a data rate. - //addDataRate(2, 2); // Approx 2Hz operation. + addDataRate(2, 2); // Close enough to 2 Hz + sensor->setDataRate(2); setDescription(QLatin1String("tsl2563")); } diff --git a/plugins/sensors/n900/n900proximitysensor.cpp b/plugins/sensors/n900/n900proximitysensor.cpp index 6f454e486c..1c44e4e7b5 100644 --- a/plugins/sensors/n900/n900proximitysensor.cpp +++ b/plugins/sensors/n900/n900proximitysensor.cpp @@ -52,7 +52,8 @@ n900proximitysensor::n900proximitysensor(QSensor *sensor) : n900filebasedsensor(sensor) { setReading<QProximityReading>(&m_reading); - addOutputRange(0, 0.01, 0.01); + addDataRate(100, 100); // 100Hz + sensor->setDataRate(100); // default is 10Hz } void n900proximitysensor::start() diff --git a/src/sensors/qsensor.cpp b/src/sensors/qsensor.cpp index 65d0481306..1b00268704 100644 --- a/src/sensors/qsensor.cpp +++ b/src/sensors/qsensor.cpp @@ -180,7 +180,10 @@ bool QSensor::connectToBackend() if (d->backend) return true; + int rate = d->dataRate; d->backend = QSensorManager::createBackend(this); + if (rate != 0) + setDataRate(rate); return (d->backend != 0); } @@ -235,7 +238,7 @@ bool QSensor::isActive() const See the sensor_explorer example for an example of how to interpret and use this information. - \sa updateInterval + \sa QSensor::dataRate */ qrangelist QSensor::availableDataRates() const @@ -244,29 +247,35 @@ qrangelist QSensor::availableDataRates() const } /*! - \property QSensor::updateInterval - \brief the update interval of the sensor (measured in milliseconds). + \property QSensor::dataRate + \brief the data rate that the sensor should be run at. - The default value is 0. Note that this causes undefined behaviour. + The default value is determined by the backend. This should be set before calling start() because the sensor may not notice changes to this value while it is running. - Note that some sensors can only operate at particular rates. - The system will attempt to run the sensor at an appropriate rate - while delivering updates as often as requested. - - \sa availableDataRates + \sa QSensor::availableDataRates */ -int QSensor::updateInterval() const +int QSensor::dataRate() const { - return d->updateInterval; + return d->dataRate; } -void QSensor::setUpdateInterval(int interval) +void QSensor::setDataRate(int rate) { - d->updateInterval = interval; + bool warn = true; + Q_FOREACH (const qrange &range, d->availableDataRates) { + if (rate >= range.first && rate <= range.second) { + warn = false; + d->dataRate = rate; + break; + } + } + if (warn) { + qWarning() << "setDataRate: rate" << rate << "is not supported by the sensor."; + } } /*! @@ -283,6 +292,8 @@ bool QSensor::start() return true; if (!connectToBackend()) return false; + if (d->availableDataRates.count() == 0) + return false; // Set these flags to their defaults d->active = true; d->busy = false; diff --git a/src/sensors/qsensor.h b/src/sensors/qsensor.h index 3f702c3271..ee802867f6 100644 --- a/src/sensors/qsensor.h +++ b/src/sensors/qsensor.h @@ -90,7 +90,7 @@ class Q_SENSORS_EXPORT QSensor : public QObject Q_PROPERTY(QByteArray type READ type) Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend) Q_PROPERTY(QtMobility::qrangelist availableDataRates READ availableDataRates) - Q_PROPERTY(int updateInterval READ updateInterval WRITE setUpdateInterval) + Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate) Q_PROPERTY(QSensorReading* reading READ reading NOTIFY readingChanged) Q_PROPERTY(bool busy READ isBusy) Q_PROPERTY(bool active READ isActive) @@ -114,8 +114,8 @@ public: bool isActive() const; qrangelist availableDataRates() const; - int updateInterval() const; - void setUpdateInterval(int interval); + int dataRate() const; + void setDataRate(int rate); qoutputrangelist outputRanges() const; int outputRange() const; diff --git a/src/sensors/qsensor_p.h b/src/sensors/qsensor_p.h index 09cad86bc3..bfd56ae68c 100644 --- a/src/sensors/qsensor_p.h +++ b/src/sensors/qsensor_p.h @@ -66,7 +66,7 @@ public: : identifier() , type() , outputRange(-1) - , updateInterval(0) + , dataRate(0) , backend(0) , active(false) , busy(false) @@ -88,7 +88,7 @@ public: // policy qrangelist availableDataRates; - int updateInterval; + int dataRate; QSensorBackend *backend; QFilterList filters; diff --git a/src/sensors/qsensorbackend.cpp b/src/sensors/qsensorbackend.cpp index ab8e7e6b9c..3493204ccb 100644 --- a/src/sensors/qsensorbackend.cpp +++ b/src/sensors/qsensorbackend.cpp @@ -235,8 +235,16 @@ void QSensorBackend::setDataRates(const QSensor *otherSensor) qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with 0"; return; } + if (otherSensor->identifier().count() == 0) { + qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor"; + return; + } QSensorPrivate *d = m_sensor->d_func(); d->availableDataRates = otherSensor->availableDataRates(); + d->dataRate = otherSensor->dataRate(); + if (d->availableDataRates.count() == 0) { + qWarning() << otherSensor->identifier() << "backend does not support any data rates."; + } } /*! diff --git a/src/sensors/qsensormanager.cpp b/src/sensors/qsensormanager.cpp index 3414a211e7..a3aa2ccf07 100644 --- a/src/sensors/qsensormanager.cpp +++ b/src/sensors/qsensormanager.cpp @@ -161,7 +161,7 @@ QSensorBackend *QSensorManager::createBackend(QSensor *sensor) //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); sensor->setIdentifier(defaultIdentifier); // the factory requires this backend = factory->createBackend(sensor); - if (backend) return backend; // Got it! + if (backend) goto gotbackend; // Got it! // The default failed to instantiate so try any other registered sensors for this type Q_FOREACH (const QByteArray &identifier, factoryByIdentifier.keys()) { @@ -171,7 +171,7 @@ QSensorBackend *QSensorManager::createBackend(QSensor *sensor) //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); sensor->setIdentifier(identifier); // the factory requires this backend = factory->createBackend(sensor); - if (backend) return backend; // Got it! + if (backend) goto gotbackend; // Got it! } SENSORLOG() << "FAILED"; sensor->setIdentifier(QByteArray()); // clear the identifier @@ -185,11 +185,20 @@ QSensorBackend *QSensorManager::createBackend(QSensor *sensor) factory = factoryByIdentifier[sensor->identifier()]; //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); backend = factory->createBackend(sensor); - if (backend) return backend; // Got it! + if (backend) goto gotbackend; // Got it! } SENSORLOG() << "no suitable backend found for requested identifier" << sensor->identifier() << "and type" << sensor->type(); return 0; + +gotbackend: + if (sensor->availableDataRates().count() == 0) { + qWarning() << sensor->identifier() << "backend does not support any data rates. It cannot be used."; + } + if (sensor->dataRate() == 0) { + qWarning() << sensor->identifier() << "backend did not supply default data rate."; + } + return backend; } // ===================================================================== diff --git a/tests/auto/qsensor/test_sensorimpl.cpp b/tests/auto/qsensor/test_sensorimpl.cpp index 6197bd24b2..78c433b2d0 100644 --- a/tests/auto/qsensor/test_sensorimpl.cpp +++ b/tests/auto/qsensor/test_sensorimpl.cpp @@ -53,12 +53,20 @@ testsensorimpl::testsensorimpl(QSensor *sensor) addOutputRange(0, 1, 0.5); addOutputRange(0, 2, 1); QString doThis = sensor->property("doThis").toString(); - if (doThis == "rates(0)") + if (doThis == "rates(0)") { setDataRates(0); - else if (doThis == "rates") + } else if (doThis == "rates") { setDataRates(new QAccelerometer(this)); - else + if (sensor->availableDataRates().count()) { + sensor->setDataRate(sensor->availableDataRates().first().first); + } else { + addDataRate(100, 100); + sensor->setDataRate(100); + } + } else { addDataRate(100, 100); + sensor->setDataRate(100); + } reading(); } diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp index 3abc9f40e6..409a5db40b 100644 --- a/tests/auto/qsensor/tst_qsensor.cpp +++ b/tests/auto/qsensor/tst_qsensor.cpp @@ -229,8 +229,8 @@ private slots: sensor.setOutputRange(1); sensor.outputRanges(); sensor.availableDataRates(); - sensor.setUpdateInterval(1000); - sensor.updateInterval(); + sensor.setDataRate(100); + sensor.dataRate(); sensor.isBusy(); sensor.error(); sensor.isConnectedToBackend(); @@ -246,6 +246,8 @@ private slots: TestSensor sensor; sensor.setProperty("doThis", "rates(0)"); QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensorBackend::setDataRates with 0 "); + QTest::ignoreMessage(QtWarningMsg, "\"test sensor impl\" backend does not support any data rates. It cannot be used. "); + QTest::ignoreMessage(QtWarningMsg, "\"test sensor impl\" backend did not supply default data rate. "); sensor.connectToBackend(); } @@ -299,6 +301,14 @@ private slots: } + void testSetBadRate() + { + TestSensor sensor; + sensor.connectToBackend(); + + QTest::ignoreMessage(QtWarningMsg, "setDataRate: rate 300 is not supported by the sensor. "); + sensor.setDataRate(300); + } }; QTEST_MAIN(tst_QSensor) |