diff options
-rw-r--r-- | src/mobility/mobilitydata.cpp | 1 | ||||
-rw-r--r-- | src/mobility/mobilitymanager.cpp | 6 | ||||
-rw-r--r-- | src/ui/sensorsui.cpp | 92 | ||||
-rw-r--r-- | src/ui/sensorsui.h | 7 |
4 files changed, 104 insertions, 2 deletions
diff --git a/src/mobility/mobilitydata.cpp b/src/mobility/mobilitydata.cpp index f6d385d..00eb592 100644 --- a/src/mobility/mobilitydata.cpp +++ b/src/mobility/mobilitydata.cpp @@ -78,6 +78,7 @@ void MobilityData::setInitialSensorsData() { SensorsUi::SensorsData sensors; sensors.ambientLightLevel = SensorsScriptInterface::Light; + sensors.lux = 100.0; sensors.accelerometerX = 0; sensors.accelerometerY = 9.8; diff --git a/src/mobility/mobilitymanager.cpp b/src/mobility/mobilitymanager.cpp index b9b37af..52ad9a0 100644 --- a/src/mobility/mobilitymanager.cpp +++ b/src/mobility/mobilitymanager.cpp @@ -340,6 +340,12 @@ void MobilityClient::sendSensorsData(const SensorsUi::SensorsData &data) QtSimulatorPrivate::RemoteMetacall<void>::call(mSendSocket, QtSimulatorPrivate::NoSync, "setAmbientLightData", ambientData); + QLightReadingData lightData; + lightData.lux = data.lux; + lightData.timestamp = timestampToSend; + QtSimulatorPrivate::RemoteMetacall<void>::call(mSendSocket, QtSimulatorPrivate::NoSync, + "setLightData", lightData); + QAccelerometerReadingData accelermometerData; accelermometerData.x = data.accelerometerX; accelermometerData.y = data.accelerometerY; diff --git a/src/ui/sensorsui.cpp b/src/ui/sensorsui.cpp index a49b9f0..2bd4fbf 100644 --- a/src/ui/sensorsui.cpp +++ b/src/ui/sensorsui.cpp @@ -129,6 +129,7 @@ void SensorDoubleEdit::updateSlider() SensorsUi::SensorsUi(QWidget *parent) : ToolBoxPage(parent) , mAmbientLightBox(0) + , mLightEdit(0) , mAccelerometerXEdit(0) , mAccelerometerYEdit(0) , mAccelerometerZEdit(0) @@ -149,10 +150,19 @@ SensorsUi::SensorsUi(QWidget *parent) QList<OptionsItem *> optionsList; mAmbientLightBox = new QComboBox(); - connect(mAmbientLightBox, SIGNAL(activated(int)), SLOT(emitSensorsDataChange())); + connect(mAmbientLightBox, SIGNAL(activated(int)), SLOT(ambientLightChanged())); OptionsItem *item = new OptionsItem(tr("Ambient light"), mAmbientLightBox); optionsList << item; + mLightEdit = new SensorDoubleEdit(); + mLightEdit->setRange(0, 5000); + mLightEdit->setDecimalPlaces(0); + item = new OptionsItem(tr("Light (LUX)"), mLightEdit); + connect(mLightEdit, SIGNAL(valueChanged(double)), SLOT(lightChanged())); + item->setTags(tags); + item->setAdvanced(true); + optionsList << item; + { QWidget *control = new QWidget; QVBoxLayout *vlayout = new QVBoxLayout; @@ -324,6 +334,7 @@ SensorsUi::SensorsData SensorsUi::sensorsData() const { SensorsUi::SensorsData data; data.ambientLightLevel = static_cast<SensorsScriptInterface::LightLevel>(mAmbientLightBox->currentIndex()); + data.lux = mLightEdit->value(); data.accelerometerX = mAccelerometerXEdit->value(); data.accelerometerY = mAccelerometerYEdit->value(); data.accelerometerZ = mAccelerometerZEdit->value(); @@ -348,6 +359,7 @@ void SensorsUi::setSensorsData(const SensorsUi::SensorsData &data) void SensorsUi::setDisplayedSensorsData(const SensorsUi::SensorsData &data) { mAmbientLightBox->setCurrentIndex(data.ambientLightLevel); + mLightEdit->setValue(data.lux); mAccelerometerXEdit->setValue(data.accelerometerX); mAccelerometerYEdit->setValue(data.accelerometerY); mAccelerometerZEdit->setValue(data.accelerometerZ); @@ -463,7 +475,7 @@ void SensorsScriptInterface::setAmbientLightLevel(const LightLevel &data) int index = static_cast<int>(data); if (ui->mAmbientLightBox->currentIndex() != index) { ui->mAmbientLightBox->setCurrentIndex(index); - ui->emitSensorsDataChange(); + ui->ambientLightChanged(); } } @@ -474,6 +486,20 @@ SensorsScriptInterface::LightLevel SensorsScriptInterface::ambientLightLevel() c return lightLevel; } +void SensorsScriptInterface::setLux(double lux) +{ + if (ui->mLightEdit->value() != lux) { + ui->mLightEdit->setValue(lux); + ui->lightChanged(); + } + +} + +double SensorsScriptInterface::lux() const +{ + return ui->mLightEdit->value(); +} + void SensorsScriptInterface::setAccelerometerX(double x) { if (ui->mAccelerometerXEdit->value() != x) { @@ -649,3 +675,65 @@ void SensorsUi::updateDeviceDefaultOrientation(bool isPortrait) mOrientationButtons.at(5)->setIcon(QIcon(":/ui/icons/facedown_landscape.png")); } } + +// Defines the min and max lux values that a given level has. +// These are used to add histeresis to the sensor. +// If the previous level is below a level, the lux must be at or above the minimum. +// If the previous level is above a level, the lux muyt be at or below the maximum. +static struct { + int min; + int max; +} limits[] = { + { 0, 0 }, // Undefined (not used) + { 0, 5 }, // Dark + { 10, 50 }, // Twilight + { 100, 200 }, // Light + { 500, 2000 }, // Bright + { 5000, 0 } // Sunny +}; + +void SensorsUi::ambientLightChanged() +{ + int lightLevel = mAmbientLightBox->currentIndex(); + qreal lux = limits[lightLevel].min; + mLightEdit->setValue(lux); + emitSensorsDataChange(); +} + +void SensorsUi::lightChanged() +{ + // It's unweildly dealing with these constants so make some + // local aliases that are shorter. This makes the code below + // much easier to read. + enum { + Undefined = 0, + Dark, + Twilight, + Light, + Bright, + Sunny + }; + + int lightLevel = mAmbientLightBox->currentIndex(); + qreal lux = mLightEdit->value(); + + if (lux != 0 || lightLevel != Undefined) { + // Check for change direction to allow for histeresis + if (lightLevel < Sunny && lux >= limits[Sunny ].min) lightLevel = Sunny; + else if (lightLevel < Bright && lux >= limits[Bright ].min) lightLevel = Bright; + else if (lightLevel < Light && lux >= limits[Light ].min) lightLevel = Light; + else if (lightLevel < Twilight && lux >= limits[Twilight].min) lightLevel = Twilight; + else if (lightLevel < Dark && lux >= limits[Dark ].min) lightLevel = Dark; + else if (lightLevel > Dark && lux <= limits[Dark ].max) lightLevel = Dark; + else if (lightLevel > Twilight && lux <= limits[Twilight].max) lightLevel = Twilight; + else if (lightLevel > Light && lux <= limits[Light ].max) lightLevel = Light; + else if (lightLevel > Bright && lux <= limits[Bright ].max) lightLevel = Bright; + } + + if (mAmbientLightBox->currentIndex() != lightLevel) { + mAmbientLightBox->setCurrentIndex(lightLevel); + } + + emitSensorsDataChange(); +} + diff --git a/src/ui/sensorsui.h b/src/ui/sensorsui.h index fbaa847..7b1efd1 100644 --- a/src/ui/sensorsui.h +++ b/src/ui/sensorsui.h @@ -106,6 +106,7 @@ public: virtual ~SensorsScriptInterface(); Q_PROPERTY(LightLevel ambientLightLevel READ ambientLightLevel WRITE setAmbientLightLevel) + Q_PROPERTY(double lux READ lux WRITE setLux) Q_PROPERTY(double accelerometerX READ accelerometerX WRITE setAccelerometerX) Q_PROPERTY(double accelerometerY READ accelerometerY WRITE setAccelerometerY) Q_PROPERTY(double accelerometerZ READ accelerometerZ WRITE setAccelerometerZ) @@ -125,6 +126,8 @@ public: void setAmbientLightLevel(const LightLevel &data); LightLevel ambientLightLevel() const; + void setLux(double lux); + double lux() const; void setAccelerometerX(double x); double accelerometerX() const; @@ -172,6 +175,7 @@ class SensorsUi : public ToolBoxPage public: struct SensorsData { int ambientLightLevel; + double lux; double accelerometerX; double accelerometerY; double accelerometerZ; @@ -197,6 +201,8 @@ public slots: void setSensorsData(const SensorsUi::SensorsData &data); void setDisplayedSensorsData(const SensorsUi::SensorsData &data); void updateDeviceDefaultOrientation(bool isPortrait); + void ambientLightChanged(); + void lightChanged(); signals: void sensorsDataChanged(const SensorsUi::SensorsData &data) const; @@ -224,6 +230,7 @@ private: QDateTime getTimestamp() const; QComboBox *mAmbientLightBox; + SensorDoubleEdit *mLightEdit; SensorDoubleEdit *mAccelerometerXEdit; SensorDoubleEdit *mAccelerometerYEdit; |