From 52a414bd2291dbf9b99606019ab50a914280ecb0 Mon Sep 17 00:00:00 2001 From: Wolfgang Beck Date: Thu, 15 Dec 2011 14:11:46 +1000 Subject: Grue example doesn't work in simulator Change-Id: I486e24befd3f31345ec7f8dffd9a183f9f2ec4cb Sanity-Review: Qt Sanity Bot Reviewed-by: Lincoln Ramsay --- examples/sensors/grue/plugin/gruesensorimpl.cpp | 17 +++++++++++------ examples/sensors/grue/plugin/gruesensorimpl.h | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'examples/sensors/grue/plugin') diff --git a/examples/sensors/grue/plugin/gruesensorimpl.cpp b/examples/sensors/grue/plugin/gruesensorimpl.cpp index d06ab8ee..7ea4aa00 100644 --- a/examples/sensors/grue/plugin/gruesensorimpl.cpp +++ b/examples/sensors/grue/plugin/gruesensorimpl.cpp @@ -46,6 +46,7 @@ char const * const gruesensorimpl::id("grue.gruesensor"); gruesensorimpl::gruesensorimpl(QSensor *sensor) : QSensorBackend(sensor) + , lightLevel(QAmbientLightReading::Undefined) { // We need a light sensor lightSensor = new QAmbientLightSensor(this); @@ -95,13 +96,18 @@ void gruesensorimpl::stop() void gruesensorimpl::lightChanged() { - qreal chance = 0.0; + if (lightLevel == lightSensor->reading()->lightLevel()) + return; + + lightLevel = lightSensor->reading()->lightLevel(); + + int chance = 0; darkTimer->stop(); switch (lightSensor->reading()->lightLevel()) { case QAmbientLightReading::Dark: // It is dark. You are likely to be eaten by a grue. - chance = 0.1; + chance = 10; darkTimer->start(); break; default: @@ -112,7 +118,6 @@ void gruesensorimpl::lightChanged() if (chance != m_reading.chanceOfBeingEaten() || m_reading.timestamp() == 0) { m_reading.setTimestamp(timer.elapsed()); m_reading.setChanceOfBeingEaten(chance); - newReadingAvailable(); } } @@ -120,16 +125,16 @@ void gruesensorimpl::lightChanged() void gruesensorimpl::increaseChance() { // The longer you stay in the dark, the higher your chance of being eaten - qreal chance = m_reading.chanceOfBeingEaten() + 0.1; + int chance = m_reading.chanceOfBeingEaten() + 10; m_reading.setTimestamp(timer.elapsed()); m_reading.setChanceOfBeingEaten(chance); newReadingAvailable(); - // No point in using the timer anymore if we've hit 1... you can't get more + // No point in using the timer anymore if we've hit 100... you can't get more // likely to be eaten than 100% - if (chance >= 1.0) + if (chance >= 100) darkTimer->stop(); } diff --git a/examples/sensors/grue/plugin/gruesensorimpl.h b/examples/sensors/grue/plugin/gruesensorimpl.h index fa4938e7..55d567be 100644 --- a/examples/sensors/grue/plugin/gruesensorimpl.h +++ b/examples/sensors/grue/plugin/gruesensorimpl.h @@ -70,6 +70,7 @@ private: QAmbientLightSensor *lightSensor; QTimer *darkTimer; QTime timer; + QAmbientLightReading::LightLevel lightLevel; }; #endif -- cgit v1.2.3