| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "gruesensorimpl.h" |
| 52 | #include <QDebug> |
| 53 | #include <QTimer> |
| 54 | |
| 55 | char const * const gruesensorimpl::id("grue.gruesensor" ); |
| 56 | |
| 57 | gruesensorimpl::gruesensorimpl(QSensor *sensor) |
| 58 | : QSensorBackend(sensor) |
| 59 | , lightLevel(QAmbientLightReading::Undefined) |
| 60 | { |
| 61 | // We need a light sensor |
| 62 | lightSensor = new QAmbientLightSensor(this); |
| 63 | connect(sender: lightSensor, SIGNAL(readingChanged()), receiver: this, SLOT(lightChanged())); |
| 64 | lightSensor->connectToBackend(); |
| 65 | |
| 66 | // We need a timer |
| 67 | darkTimer = new QTimer(this); |
| 68 | darkTimer->setInterval(1000); |
| 69 | connect(sender: darkTimer, SIGNAL(timeout()), receiver: this, SLOT(increaseChance())); |
| 70 | |
| 71 | // We use this as our timestamp source |
| 72 | timer.start(); |
| 73 | |
| 74 | //! [setReading] |
| 75 | // Register our reading instance |
| 76 | setReading<GrueSensorReading>(&m_reading); |
| 77 | //! [setReading] |
| 78 | |
| 79 | //! [metadata] |
| 80 | // Supply metadata |
| 81 | // We can run as fast as the light sensor does |
| 82 | setDataRates(lightSensor); |
| 83 | // Only one output range, 0 to 1 in .1 increments |
| 84 | addOutputRange(min: 0, max: 1, accuracy: 0.1); |
| 85 | setDescription(QLatin1String("Grue Sensor" )); |
| 86 | //! [metadata] |
| 87 | } |
| 88 | |
| 89 | void gruesensorimpl::start() |
| 90 | { |
| 91 | //! [start] |
| 92 | lightSensor->setDataRate(sensor()->dataRate()); |
| 93 | lightSensor->start(); |
| 94 | // If the light sensor doesn't work we don't work either |
| 95 | if (!lightSensor->isActive()) |
| 96 | sensorStopped(); |
| 97 | if (lightSensor->isBusy()) |
| 98 | sensorBusy(); |
| 99 | //! [start] |
| 100 | } |
| 101 | |
| 102 | void gruesensorimpl::stop() |
| 103 | { |
| 104 | lightSensor->stop(); |
| 105 | } |
| 106 | |
| 107 | void gruesensorimpl::lightChanged() |
| 108 | { |
| 109 | if (lightLevel == lightSensor->reading()->lightLevel()) |
| 110 | return; |
| 111 | |
| 112 | lightLevel = lightSensor->reading()->lightLevel(); |
| 113 | |
| 114 | int chance = 0; |
| 115 | darkTimer->stop(); |
| 116 | |
| 117 | switch (lightSensor->reading()->lightLevel()) { |
| 118 | case QAmbientLightReading::Dark: |
| 119 | // It is dark. You are likely to be eaten by a grue. |
| 120 | chance = 10; |
| 121 | darkTimer->start(); |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | // Only send an update if the value has changed. |
| 128 | if (chance != m_reading.chanceOfBeingEaten() || m_reading.timestamp() == 0) { |
| 129 | m_reading.setTimestamp(timer.elapsed()); |
| 130 | m_reading.setChanceOfBeingEaten(chance); |
| 131 | newReadingAvailable(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void gruesensorimpl::increaseChance() |
| 136 | { |
| 137 | // The longer you stay in the dark, the higher your chance of being eaten |
| 138 | int chance = m_reading.chanceOfBeingEaten() + 10; |
| 139 | |
| 140 | m_reading.setTimestamp(timer.elapsed()); |
| 141 | m_reading.setChanceOfBeingEaten(chance); |
| 142 | |
| 143 | newReadingAvailable(); |
| 144 | |
| 145 | // No point in using the timer anymore if we've hit 100... you can't get more |
| 146 | // likely to be eaten than 100% |
| 147 | if (chance >= 100) |
| 148 | darkTimer->stop(); |
| 149 | } |
| 150 | |
| 151 | |