|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qsensorbackend.h" |
|
43 #include "qsensor_p.h" |
|
44 #include <QDebug> |
|
45 |
|
46 QTM_BEGIN_NAMESPACE |
|
47 |
|
48 /*! |
|
49 \class QSensorBackend |
|
50 \ingroup sensors_backend |
|
51 |
|
52 \brief The QSensorBackend class is a sensor implementation. |
|
53 |
|
54 Sensors on a device will be represented by sub-classes of |
|
55 QSensorBackend. |
|
56 */ |
|
57 |
|
58 /*! |
|
59 \internal |
|
60 */ |
|
61 QSensorBackend::QSensorBackend(QSensor *sensor) |
|
62 : m_sensor(sensor) |
|
63 { |
|
64 } |
|
65 |
|
66 /*! |
|
67 \internal |
|
68 */ |
|
69 QSensorBackend::~QSensorBackend() |
|
70 { |
|
71 } |
|
72 |
|
73 /*! |
|
74 Notify the QSensor class that a new reading is available. |
|
75 */ |
|
76 void QSensorBackend::newReadingAvailable() |
|
77 { |
|
78 QSensorPrivate *d = m_sensor->d_func(); |
|
79 |
|
80 // Copy the values from the device reading to the filter reading |
|
81 d->filter_reading->copyValuesFrom(d->device_reading); |
|
82 |
|
83 for (QFilterList::const_iterator it = d->filters.constBegin(); it != d->filters.constEnd(); ++it) { |
|
84 QSensorFilter *filter = (*it); |
|
85 if (!filter->filter(d->filter_reading)) |
|
86 return; |
|
87 } |
|
88 |
|
89 // Copy the values from the filter reading to the cached reading |
|
90 d->cache_reading->copyValuesFrom(d->filter_reading); |
|
91 |
|
92 Q_EMIT m_sensor->readingChanged(); |
|
93 } |
|
94 |
|
95 /*! |
|
96 \fn QSensorBackend::start() |
|
97 |
|
98 Start reporting values. |
|
99 */ |
|
100 |
|
101 /*! |
|
102 \fn QSensorBackend::stop() |
|
103 |
|
104 Stop reporting values. |
|
105 */ |
|
106 |
|
107 /*! |
|
108 If the backend has lost its reference to the reading |
|
109 it can call this method to get the address. |
|
110 |
|
111 Note that you will need to down-cast to the appropriate |
|
112 type. |
|
113 |
|
114 \sa setReading() |
|
115 */ |
|
116 QSensorReading *QSensorBackend::reading() const |
|
117 { |
|
118 QSensorPrivate *d = m_sensor->d_func(); |
|
119 return d->device_reading; |
|
120 } |
|
121 |
|
122 /*! |
|
123 \fn QSensorBackend::sensor() const |
|
124 |
|
125 Returns the sensor front end associated with this backend. |
|
126 */ |
|
127 |
|
128 /*! |
|
129 \fn QSensorBackend::setReading(T *reading) |
|
130 |
|
131 This function is called to initialize the \a reading |
|
132 classes used for a sensor. |
|
133 |
|
134 If your backend has already allocated a reading you |
|
135 should pass the address of this to the function. |
|
136 Otherwise you should pass 0 and the function will |
|
137 return the address of the reading your backend |
|
138 should use when it wants to notify the sensor API |
|
139 of new readings. |
|
140 |
|
141 Note that this is a template function so it should |
|
142 be called with the appropriate type. |
|
143 |
|
144 \code |
|
145 class MyBackend : public QSensorBackend |
|
146 { |
|
147 QAccelerometerReading m_reading; |
|
148 public: |
|
149 MyBackend(QSensor *sensor) |
|
150 : QSensorBackend(sensor) |
|
151 { |
|
152 setReading<QAccelerometerReading>(&m_reading); |
|
153 } |
|
154 |
|
155 ... |
|
156 \endcode |
|
157 |
|
158 Note that this function must be called or you will |
|
159 not be able to send readings to the front end. |
|
160 |
|
161 If you do not wish to store the address of the reading |
|
162 you may use the reading() method to get it again later. |
|
163 |
|
164 \code |
|
165 class MyBackend : public QSensorBackend |
|
166 { |
|
167 public: |
|
168 MyBackend(QSensor *sensor) |
|
169 : QSensorBackend(sensor) |
|
170 { |
|
171 setReading<QAccelerometerReading>(0); |
|
172 } |
|
173 |
|
174 void poll() |
|
175 { |
|
176 qtimestamp timestamp; |
|
177 qreal x, y, z; |
|
178 ... |
|
179 QAccelerometerReading *reading = static_cast<QAccelerometerReading*>(reading()); |
|
180 reading->setTimestamp(timestamp); |
|
181 reading->setX(x); |
|
182 reading->setY(y); |
|
183 reading->setZ(z); |
|
184 } |
|
185 |
|
186 ... |
|
187 \endcode |
|
188 |
|
189 \sa reading() |
|
190 */ |
|
191 |
|
192 /*! |
|
193 \internal |
|
194 */ |
|
195 void QSensorBackend::setReadings(QSensorReading *device, QSensorReading *filter, QSensorReading *cache) |
|
196 { |
|
197 QSensorPrivate *d = m_sensor->d_func(); |
|
198 d->device_reading = device; |
|
199 d->filter_reading = filter; |
|
200 d->cache_reading = cache; |
|
201 } |
|
202 |
|
203 /*! |
|
204 Add a data rate (consisting of \a min and \a max values) for the sensor. |
|
205 |
|
206 Note that this function should be called from the constructor so that the information |
|
207 is available immediately. |
|
208 |
|
209 \sa QSensor::availableDataRates |
|
210 */ |
|
211 void QSensorBackend::addDataRate(qreal min, qreal max) |
|
212 { |
|
213 QSensorPrivate *d = m_sensor->d_func(); |
|
214 d->availableDataRates << qrange(min, max); |
|
215 } |
|
216 |
|
217 /*! |
|
218 Set the data rates for the sensor based on \a otherSensor. |
|
219 |
|
220 This is designed for sensors that are based on other sensors. |
|
221 |
|
222 \code |
|
223 setDataRates(otherSensor); |
|
224 \endcode |
|
225 |
|
226 Note that this function should be called from the constructor so that the information |
|
227 is available immediately. |
|
228 |
|
229 \sa QSensor::availableDataRates, addDataRate() |
|
230 */ |
|
231 void QSensorBackend::setDataRates(const QSensor *otherSensor) |
|
232 { |
|
233 if (!otherSensor) { |
|
234 qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with 0"; |
|
235 return; |
|
236 } |
|
237 if (otherSensor->identifier().isEmpty()) { |
|
238 qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor"; |
|
239 return; |
|
240 } |
|
241 QSensorPrivate *d = m_sensor->d_func(); |
|
242 d->availableDataRates = otherSensor->availableDataRates(); |
|
243 d->dataRate = otherSensor->dataRate(); |
|
244 } |
|
245 |
|
246 /*! |
|
247 Add an output range (consisting of \a min, \a max values and \a accuracy) for the sensor. |
|
248 |
|
249 Note that this function should be called from the constructor so that the information |
|
250 is available immediately. |
|
251 |
|
252 \sa QSensor::outputRange, QSensor::outputRanges |
|
253 */ |
|
254 void QSensorBackend::addOutputRange(qreal min, qreal max, qreal accuracy) |
|
255 { |
|
256 QSensorPrivate *d = m_sensor->d_func(); |
|
257 |
|
258 qoutputrange details = {min, max, accuracy}; |
|
259 |
|
260 d->outputRanges << details; |
|
261 } |
|
262 |
|
263 /*! |
|
264 Set the \a description for the sensor. |
|
265 |
|
266 Note that this function should be called from the constructor so that the information |
|
267 is available immediately. |
|
268 */ |
|
269 void QSensorBackend::setDescription(const QString &description) |
|
270 { |
|
271 QSensorPrivate *d = m_sensor->d_func(); |
|
272 d->description = description; |
|
273 } |
|
274 |
|
275 /*! |
|
276 Inform the front end that the sensor has stopped. |
|
277 This can be due to start() failing or for some |
|
278 unexpected reason (eg. hardware failure). |
|
279 |
|
280 Note that the front end must call QSensor::isActive() to see if |
|
281 the sensor has stopped. If the sensor has stopped due to an error |
|
282 the sensorError() function should be called to notify the class |
|
283 of the error condition. |
|
284 */ |
|
285 void QSensorBackend::sensorStopped() |
|
286 { |
|
287 QSensorPrivate *d = m_sensor->d_func(); |
|
288 d->active = false; |
|
289 } |
|
290 |
|
291 /*! |
|
292 Inform the front end that the sensor is busy. |
|
293 This implicitly calls sensorStopped() and |
|
294 is typically called from start(). |
|
295 |
|
296 Note that the front end must call QSensor::isBusy() to see if |
|
297 the sensor is busy. If the sensor has stopped due to an error |
|
298 the sensorError() function should be called to notify the class |
|
299 of the error condition. |
|
300 */ |
|
301 void QSensorBackend::sensorBusy() |
|
302 { |
|
303 QSensorPrivate *d = m_sensor->d_func(); |
|
304 d->active = false; |
|
305 d->busy = true; |
|
306 } |
|
307 |
|
308 /*! |
|
309 Inform the front end that a sensor error occurred. |
|
310 Note that this only reports an \a error code. It does |
|
311 not stop the sensor. |
|
312 |
|
313 \sa sensorStopped() |
|
314 */ |
|
315 void QSensorBackend::sensorError(int error) |
|
316 { |
|
317 QSensorPrivate *d = m_sensor->d_func(); |
|
318 d->error = error; |
|
319 Q_EMIT m_sensor->sensorError(error); |
|
320 } |
|
321 |
|
322 #include "moc_qsensorbackend.cpp" |
|
323 QTM_END_NAMESPACE |
|
324 |