1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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:LGPL$ |
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 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "genericalssensor.h" |
41 | #include <QDebug> |
42 | |
43 | char const * const genericalssensor::id("generic.als" ); |
44 | |
45 | genericalssensor::genericalssensor(QSensor *sensor) |
46 | : QSensorBackend(sensor) |
47 | { |
48 | lightSensor = new QLightSensor(this); |
49 | lightSensor->addFilter(filter: this); |
50 | lightSensor->connectToBackend(); |
51 | |
52 | setReading<QAmbientLightReading>(&m_reading); |
53 | setDataRates(lightSensor); |
54 | } |
55 | |
56 | void genericalssensor::start() |
57 | { |
58 | lightSensor->setDataRate(sensor()->dataRate()); |
59 | lightSensor->setAlwaysOn(sensor()->isAlwaysOn()); |
60 | lightSensor->start(); |
61 | if (!lightSensor->isActive()) |
62 | sensorStopped(); |
63 | if (lightSensor->isBusy()) |
64 | sensorBusy(); |
65 | } |
66 | |
67 | void genericalssensor::stop() |
68 | { |
69 | lightSensor->stop(); |
70 | } |
71 | |
72 | struct lux_limit { |
73 | int min; |
74 | int max; |
75 | }; |
76 | |
77 | // Defines the min and max lux values that a given level has. |
78 | // These are used to add histeresis to the sensor. |
79 | // If the previous level is below a level, the lux must be at or above the minimum. |
80 | // If the previous level is above a level, the lux muyt be at or below the maximum. |
81 | static lux_limit limits[] = { |
82 | { .min: 0, .max: 0 }, // Undefined (not used) |
83 | { .min: 0, .max: 5 }, // Dark |
84 | { .min: 10, .max: 50 }, // Twilight |
85 | { .min: 100, .max: 200 }, // Light |
86 | { .min: 500, .max: 2000 }, // Bright |
87 | { .min: 5000, .max: 0 } // Sunny |
88 | }; |
89 | |
90 | #if 0 |
91 | // Used for debugging |
92 | static QString light_level(int level) |
93 | { |
94 | switch (level) { |
95 | case 1: |
96 | return QLatin1String("Dark" ); |
97 | case 2: |
98 | return QLatin1String("Twilight" ); |
99 | case 3: |
100 | return QLatin1String("Light" ); |
101 | case 4: |
102 | return QLatin1String("Bright" ); |
103 | case 5: |
104 | return QLatin1String("Sunny" ); |
105 | default: |
106 | return QLatin1String("Undefined" ); |
107 | } |
108 | } |
109 | #endif |
110 | |
111 | bool genericalssensor::filter(QLightReading *reading) |
112 | { |
113 | // It's unweildly dealing with these constants so make some |
114 | // local aliases that are shorter. This makes the code below |
115 | // much easier to read. |
116 | enum { |
117 | Undefined = QAmbientLightReading::Undefined, |
118 | Dark = QAmbientLightReading::Dark, |
119 | Twilight = QAmbientLightReading::Twilight, |
120 | Light = QAmbientLightReading::Light, |
121 | Bright = QAmbientLightReading::Bright, |
122 | Sunny = QAmbientLightReading::Sunny |
123 | }; |
124 | |
125 | int lightLevel = m_reading.lightLevel(); |
126 | qreal lux = reading->lux(); |
127 | |
128 | // Check for change direction to allow for histeresis |
129 | if (lightLevel < Sunny && lux >= limits[Sunny ].min) lightLevel = Sunny; |
130 | else if (lightLevel < Bright && lux >= limits[Bright ].min) lightLevel = Bright; |
131 | else if (lightLevel < Light && lux >= limits[Light ].min) lightLevel = Light; |
132 | else if (lightLevel < Twilight && lux >= limits[Twilight].min) lightLevel = Twilight; |
133 | else if (lightLevel < Dark && lux >= limits[Dark ].min) lightLevel = Dark; |
134 | else if (lightLevel > Dark && lux <= limits[Dark ].max) lightLevel = Dark; |
135 | else if (lightLevel > Twilight && lux <= limits[Twilight].max) lightLevel = Twilight; |
136 | else if (lightLevel > Light && lux <= limits[Light ].max) lightLevel = Light; |
137 | else if (lightLevel > Bright && lux <= limits[Bright ].max) lightLevel = Bright; |
138 | |
139 | //qDebug() << "lightLevel" << light_level(lightLevel) << "lux" << lux; |
140 | |
141 | if (static_cast<int>(m_reading.lightLevel()) != lightLevel || m_reading.timestamp() == 0) { |
142 | m_reading.setTimestamp(reading->timestamp()); |
143 | m_reading.setLightLevel(static_cast<QAmbientLightReading::LightLevel>(lightLevel)); |
144 | |
145 | newReadingAvailable(); |
146 | } |
147 | |
148 | return false; |
149 | } |
150 | |
151 | |