summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/sensors/qmlqtsensors5/main.qml62
-rw-r--r--src/imports/sensors2/qsensor2tilt.cpp101
-rw-r--r--src/imports/sensors2/qsensor2tilt.h24
-rw-r--r--tests/auto/sensors2qmlapi/qdeclaccelerometer.h7
-rw-r--r--tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp93
5 files changed, 254 insertions, 33 deletions
diff --git a/examples/sensors/qmlqtsensors5/main.qml b/examples/sensors/qmlqtsensors5/main.qml
index a1144d8c..e93e8825 100644
--- a/examples/sensors/qmlqtsensors5/main.qml
+++ b/examples/sensors/qmlqtsensors5/main.qml
@@ -43,13 +43,13 @@ import QtQuick 2.0
import QtSensors 5.0
/* Layout
- tiltrect
- /
------------------------------------------------------------/
+ tiltrect
+ /
+----------------------------------------------------------------------------/
| Accuracy <----- textAccuracy
-| @ 0 Degree <- selButton0DegreeAccuracy
-| O 1 Degree <- selButton1DegreeAccuracy
-| O 5 Degree <- selButton5DegreeAccuracy
+| @ 0 Degree <- selButton0DegreeAccuracy @ Slow <- selButtonSlowSpeed
+| O 1 Degree <- selButton1DegreeAccuracy O Medium <- selButtonMediumSpeed
+| O 5 Degree <- selButton5DegreeAccuracy O Fast <- selButtonFastSpeed
| -----------
| |calibrate| <------------------ calibrate
| -----------
@@ -73,7 +73,7 @@ import QtSensors 5.0
| ---------
| |start | <-------------------- proxiStart
| ---------
-------------------------------------------------------------
+------------------------------------------------------------------------------
*/
@@ -95,12 +95,21 @@ Rectangle {
enabled: false
accuracy: 1.0
}
+
Text{
id: textAccuracy
x: 5
y: 20
text: "Accuracy"
}
+
+ Text{
+ id: textSpeed
+ x: 150
+ y: 20
+ text: "Speed"
+ }
+
SelectionButton{
id: selButton0DegreeAccuracy
x: 5
@@ -114,6 +123,19 @@ Rectangle {
}
}
SelectionButton{
+ id: selButtonSlowSpeed
+ x: 150
+ y: 45
+ checked: true
+ text: "Slow"
+ onClicked:{
+ selButtonMediumSpeed.setCheck(false);
+ selButtonFastSpeed.setCheck(false);
+ tilt.speed = TiltSensor.Slow;
+ }
+ }
+
+ SelectionButton{
id: selButton1DegreeAccuracy
x: 5
y: 70
@@ -126,6 +148,19 @@ Rectangle {
}
}
SelectionButton{
+ id: selButtonMediumSpeed
+ x: 150
+ y: 70
+ checked: false
+ text: "Medium"
+ onClicked:{
+ selButtonSlowSpeed.setCheck(false);
+ selButtonFastSpeed.setCheck(false);
+ tilt.speed = TiltSensor.Medium;
+ }
+ }
+
+ SelectionButton{
id: selButton5DegreeAccuracy
x: 5
y: 95
@@ -138,6 +173,19 @@ Rectangle {
}
}
+ SelectionButton{
+ id: selButtonFastSpeed
+ x: 150
+ y: 95
+ checked: false
+ text: "Fast"
+ onClicked:{
+ selButtonSlowSpeed.setCheck(false);
+ selButtonMediumSpeed.setCheck(false);
+ tilt.speed = TiltSensor.Fast;
+ }
+ }
+
Button{
id: calibrate
x: 5
diff --git a/src/imports/sensors2/qsensor2tilt.cpp b/src/imports/sensors2/qsensor2tilt.cpp
index 978ec19d..b173b0db 100644
--- a/src/imports/sensors2/qsensor2tilt.cpp
+++ b/src/imports/sensors2/qsensor2tilt.cpp
@@ -45,6 +45,8 @@
#include <QtCore/QDebug>
#include <QtCore/QStringList>
+#define MAXRATE 30
+
QT_BEGIN_NAMESPACE
/*!
@@ -66,6 +68,7 @@ QSensor2Tilt::QSensor2Tilt(QObject* parent)
, _roll(0)
, _calibratedPitch(0)
, _calibratedRoll(0)
+ , _speed(QSensor2Tilt::Slow)
{
_accel = new QAccelerometer(this);
_accel->addFilter(this);
@@ -75,21 +78,92 @@ QSensor2Tilt::~QSensor2Tilt()
{
}
+int searchDataRate(const QList<int>& datarates, int value)
+{
+ int l = 0;
+ int r = datarates.count() - 1;
+ int m = (l + r) / 2;
+
+ while (l <= r) {
+ m = (l + r) / 2;
+ if (datarates[m] == value)
+ break;
+ else if (datarates[m] < value)
+ r = m - 1;
+ else
+ l = m + 1;
+ }
+ if (m > 0){
+ int ddr = datarates[m - 1];
+ ddr -= value;
+ int ddr1 = datarates[m] - value;
+ if (ddr1 < 0) ddr1 = -ddr1;
+ if (ddr < ddr1)
+ return datarates[m - 1];
+ }
+
+ return datarates[m];
+}
+
+void QSensor2Tilt::createRunModeDataRateMap()
+{
+ _dataRate.clear();
+ qrangelist rl = _accel->availableDataRates();
+
+ //1. make a list of all available datarates
+ QList<int> dr;
+ foreach (const qrange &r, rl) {
+ for (int i = r.first; i <= r.second; i++){
+ if (i <= MAXRATE){
+ if (!dr.contains(i))
+ dr.append(i);
+ }
+ }
+ }
+
+ //2. Sort the list
+ if (dr.count() > 0){
+ qSort(dr.begin(), dr.end(), qGreater<int>());
+ _dataRate.insert(QSensor2Tilt::Slow, searchDataRate(dr, 2));
+ _dataRate.insert(QSensor2Tilt::Medium, searchDataRate(dr, 10));
+ _dataRate.insert(QSensor2Tilt::Fast, searchDataRate(dr, 20));
+ }
+}
+
/*!
- \qmlproperty bool QtSensors5::TiltSensor::dataRate
- Holds the data rate that the sensor should be run at.
+ \qmlproperty bool QtSensors5::TiltSensor::speed
+ Holds the speed that the sensor should be run at.
+ Default is Slow.
+
+ \table
+ \row
+ \o TiltSensor.Slow
+ \o The sensor runs in slow mode.
+ \o Closest available datarate at 2Hz.
+ \row
+ \o TiltSensor.Medium
+ \o The sensor runs in medium mode.
+ \o Closest available datarate at 10Hz.
+ \row
+ \o TiltSensor.Fast
+ \o The sensor runs in fast mode.
+ \o Closest available datarate at 20Hz.
+ \endtable
*/
-int QSensor2Tilt::dataRate()
+QSensor2Tilt::Speed QSensor2Tilt::speed()
{
- return _accel->dataRate();
+ return _speed;
}
-void QSensor2Tilt::setDataRate(int val)
+void QSensor2Tilt::setSpeed(const QSensor2Tilt::Speed val)
{
- if (val != dataRate()){
- _accel->setDataRate(val);
- emit dataRateChanged();
+ if (_dataRate.keys().contains(val)){
+ if (_dataRate.value(val) != _accel->dataRate()){
+ _accel->setDataRate(_dataRate.value(val));
+ emit speedChanged();
+ }
}
+ _speed = val;
}
/*!
@@ -101,14 +175,19 @@ bool QSensor2Tilt::enabled()
return _accel->isActive();
}
-void QSensor2Tilt::setEnabled(bool val)
+void QSensor2Tilt::setEnabled(const bool val)
{
bool active = enabled();
if (active != val){
if (val){
+ bool readDatarateMap = !_accel->isConnectedToBackend();
bool ret = _accel->start();
if (!ret)
qWarning() << "couldn't start the sensor.";
+ else if (readDatarateMap){
+ createRunModeDataRateMap();
+ setSpeed(_speed);
+ }
}
else
_accel->stop();
@@ -224,7 +303,9 @@ inline qreal calcTheta(double Ax, double Ay, double Az)
/*!
\qmlsignal QtSensors5::TiltSensor::tiltChanged(qreal deltaX, qreal deltaY)
This signal is emitted whenever the change from at leat one of the rotation values was higher than the accuracy.
- The angle value is based on the specified unit (Degree or Radian) \sa {unit_property} {TiltSensor.unit}.
+ The angle value is based on the specified unit (Degree or Radian).
+
+ \sa {QtSensors5::TiltSensor::unit} {TiltSensor.unit}
*/
qreal QSensor2Tilt::accuracy()
{
diff --git a/src/imports/sensors2/qsensor2tilt.h b/src/imports/sensors2/qsensor2tilt.h
index bf6c2b93..7b8f1977 100644
--- a/src/imports/sensors2/qsensor2tilt.h
+++ b/src/imports/sensors2/qsensor2tilt.h
@@ -51,11 +51,11 @@ QT_BEGIN_NAMESPACE
class QSensor2Tilt : public QObject, public QAccelerometerFilter
{
Q_OBJECT
- Q_ENUMS(Unit)
+ Q_ENUMS(Unit Speed)
Q_PROPERTY(qreal yRotation READ yRotation NOTIFY yRotationChanged)
Q_PROPERTY(qreal xRotation READ xRotation NOTIFY xRotationChanged)
Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged)
- Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged)
+ Q_PROPERTY(Speed speed READ speed WRITE setSpeed NOTIFY speedChanged)
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(qreal accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged)
Q_PROPERTY(QByteArray settings READ settings WRITE setSettings)
@@ -70,11 +70,18 @@ public:
, Degrees
};
+ enum Speed{
+ Slow = 0
+ , Medium
+ , Fast
+ };
+
+
Q_SIGNALS:
void unitChanged();
void yRotationChanged();
void xRotationChanged();
- void dataRateChanged();
+ void speedChanged();
void enabledChanged();
void tiltChanged(qreal deltaX, qreal deltaY);
void accuracyChanged();
@@ -86,14 +93,15 @@ private:
qreal xRotation();
Unit unit();
void setUnit(const Unit val);
- int dataRate();
- void setDataRate(const int val);
+ Speed speed();
+ void setSpeed(const Speed val);
bool enabled();
- void setEnabled(bool val);
+ void setEnabled(const bool val);
qreal accuracy();
void setAccuracy(const qreal val);
QByteArray settings() const;
void setSettings(const QByteArray val);
+ void createRunModeDataRateMap();
QAccelerometer* _accel;
qreal _yRotation;
@@ -104,6 +112,10 @@ private:
qreal _roll;
qreal _calibratedPitch;
qreal _calibratedRoll;
+ QMap<Speed, int> _dataRate;
+ Speed _speed;
+
+ friend class tst_Sensors2QMLAPI;
};
QT_END_NAMESPACE
diff --git a/tests/auto/sensors2qmlapi/qdeclaccelerometer.h b/tests/auto/sensors2qmlapi/qdeclaccelerometer.h
index a94bd62f..5d8da9fb 100644
--- a/tests/auto/sensors2qmlapi/qdeclaccelerometer.h
+++ b/tests/auto/sensors2qmlapi/qdeclaccelerometer.h
@@ -54,12 +54,15 @@ public:
, _active(false)
, _sensor(sensor)
{
- this->addDataRate(10, 50);
}
virtual ~QDeclAccelerometer() {}
void start() { _active = true; }
- void stop() { _active = false; }
+ void stop()
+ {
+ _sensor->stop();
+ _active = false;
+ }
bool isActive() { return _active; }
void test(float x, float y, float z)
diff --git a/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp b/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp
index e37091c3..25419bc5 100644
--- a/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp
+++ b/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp
@@ -66,6 +66,7 @@ private slots:
void testTilt_receivedSignalsCount();
void testTiltAccuracy();
void testTiltCalibration();
+ void testTiltRunningMode();
void testProximity();
void testAmbientLight();
@@ -210,7 +211,6 @@ void tst_Sensors2QMLAPI::testTilt_data()
int xrotch = 0;
int yrotch = 0;
-int datarate = 10;
void tst_Sensors2QMLAPI::testTilt()
{
QFETCH(qreal, pitch);
@@ -267,11 +267,6 @@ void tst_Sensors2QMLAPI::testTilt()
spy.clear();
_tilt->setProperty("enabled", QVariant(false));
QCOMPARE(spy.count() , 0);
-
- datarate++;
- QSignalSpy spydr(_tilt, SIGNAL(dataRateChanged()));
- _tilt->setProperty("dataRate", QVariant(datarate));
- QCOMPARE(spydr.count() , 1);
}
void tst_Sensors2QMLAPI::testTiltAccuracy()
@@ -284,7 +279,7 @@ void tst_Sensors2QMLAPI::testTiltAccuracy()
settings.append(";");
settings.append(QString::number((double)(0)));
_tilt->setProperty("settings", QVariant((QByteArray)settings));
- _tilt->setProperty("enable", QVariant(true));
+ _tilt->setProperty("enabled", QVariant(true));
QDeclAccelerometer* accel = _plugin.stAccel;
@@ -326,7 +321,7 @@ void tst_Sensors2QMLAPI::testTiltCalibration()
settings.append(";");
settings.append(QString::number((double)(0)));
_tilt->setProperty("settings", QVariant((QByteArray)settings));
- _tilt->setProperty("enable", QVariant(true));
+ _tilt->setProperty("enabled", QVariant(true));
_tilt->setProperty("unit", QVariant((int)QSensor2Tilt::Degrees));
QDeclAccelerometer* accel = _plugin.stAccel;
@@ -352,6 +347,88 @@ void tst_Sensors2QMLAPI::testTiltCalibration()
if (yRotation < 0) yRotation = -yRotation;
QVERIFY(xRotation < 0.25);
QVERIFY(yRotation < 0.12);
+ _tilt->setProperty("enabled", QVariant(false));
+}
+
+void tst_Sensors2QMLAPI::testTiltRunningMode()
+{
+ if (!_tilt)
+ _tilt = new QSensor2Tilt(this);
+ QDeclAccelerometer* accel = _plugin.stAccel;
+ QCOMPARE(_tilt->_dataRate.count(), 0);
+ accel->addDataRate(13, 15);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 13);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 13);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 15);
+
+ accel->addDataRate(1, 8);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 8);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 15);
+
+ accel->addDataRate(11, 12);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 11);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 15);
+
+ accel->addDataRate(13, 18);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 11);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 18);
+
+ accel->addDataRate(21, 25);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 11);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 21);
+
+ accel->addDataRate(19, 20);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 11);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 20);
+
+ accel->addDataRate(9, 10);
+ _tilt->createRunModeDataRateMap();
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Slow), 2);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Medium), 10);
+ QCOMPARE(_tilt->_dataRate.value(QSensor2Tilt::Fast), 20);
+
+ QSensor2Tilt::Speed speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Slow);
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Slow));
+
+ QSignalSpy spymode(_tilt, SIGNAL(speedChanged()));
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Slow));
+ QCOMPARE(spymode.count() , 0);
+ speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Slow);
+
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Medium));
+ QCOMPARE(spymode.count() , 1);
+ spymode.clear();
+ speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Medium);
+
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Medium));
+ QCOMPARE(spymode.count() , 0);
+ speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Medium);
+
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Fast));
+ QCOMPARE(spymode.count() , 1);
+ spymode.clear();
+ speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Fast);
+
+ _tilt->setProperty("speed", QVariant(QSensor2Tilt::Fast));
+ QCOMPARE(spymode.count() , 0);
+ speed = (QSensor2Tilt::Speed)_tilt->property("speed").toInt();
+ QCOMPARE(speed, QSensor2Tilt::Fast);
}
QTEST_MAIN(tst_Sensors2QMLAPI)