1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSENSOR_H |
5 | #define QSENSOR_H |
6 | |
7 | #include <QtSensors/qsensorsglobal.h> |
8 | |
9 | #include <QtCore/QObject> |
10 | #include <QtCore/QByteArray> |
11 | #include <QtCore/QMetaType> |
12 | #include <QtCore/QVariant> |
13 | #include <QtCore/QPair> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | class QSensorPrivate; |
18 | class QSensorBackend; |
19 | class QSensorReading; |
20 | class QSensorReadingPrivate; |
21 | class QSensorFilter; |
22 | |
23 | using qrange = QPair<int,int>; |
24 | using qrangelist = QList<qrange>; |
25 | |
26 | struct qoutputrange |
27 | { |
28 | qreal minimum; |
29 | qreal maximum; |
30 | qreal accuracy; |
31 | }; |
32 | |
33 | using qoutputrangelist = QList<qoutputrange>; |
34 | |
35 | class Q_SENSORS_EXPORT QSensor : public QObject |
36 | { |
37 | friend class QSensorBackend; |
38 | Q_OBJECT |
39 | Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier NOTIFY identifierChanged) |
40 | Q_PROPERTY(QByteArray type READ type CONSTANT) |
41 | Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend) |
42 | Q_PROPERTY(qrangelist availableDataRates READ availableDataRates) |
43 | Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged) |
44 | Q_PROPERTY(QSensorReading* reading READ reading NOTIFY readingChanged) |
45 | Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged) |
46 | Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged) |
47 | Q_PROPERTY(qoutputrangelist outputRanges READ outputRanges) |
48 | Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange) |
49 | Q_PROPERTY(QString description READ description) |
50 | Q_PROPERTY(int error READ error NOTIFY sensorError) |
51 | Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged) |
52 | Q_PROPERTY(bool skipDuplicates READ skipDuplicates WRITE setSkipDuplicates NOTIFY skipDuplicatesChanged) |
53 | Q_PROPERTY(AxesOrientationMode axesOrientationMode READ axesOrientationMode WRITE setAxesOrientationMode NOTIFY axesOrientationModeChanged) |
54 | Q_PROPERTY(int currentOrientation READ currentOrientation NOTIFY currentOrientationChanged) |
55 | Q_PROPERTY(int userOrientation READ userOrientation WRITE setUserOrientation NOTIFY userOrientationChanged) |
56 | Q_PROPERTY(int maxBufferSize READ maxBufferSize NOTIFY maxBufferSizeChanged) |
57 | Q_PROPERTY(int efficientBufferSize READ efficientBufferSize NOTIFY efficientBufferSizeChanged) |
58 | Q_PROPERTY(int bufferSize READ bufferSize WRITE setBufferSize NOTIFY bufferSizeChanged) |
59 | public: |
60 | enum Feature { |
61 | Buffering, |
62 | AlwaysOn, |
63 | GeoValues, |
64 | FieldOfView, |
65 | AccelerationMode, |
66 | SkipDuplicates, |
67 | AxesOrientation, |
68 | PressureSensorTemperature, |
69 | Reserved = 257 // Make sure at least 2 bytes are used for the enum to avoid breaking BC later |
70 | }; |
71 | Q_ENUM(Feature) |
72 | |
73 | // Keep in sync with QmlSensor::AxesOrientationMode |
74 | enum AxesOrientationMode { |
75 | FixedOrientation, |
76 | AutomaticOrientation, |
77 | UserOrientation |
78 | }; |
79 | Q_ENUM(AxesOrientationMode) |
80 | |
81 | explicit QSensor(const QByteArray &type, QObject *parent = nullptr); |
82 | virtual ~QSensor(); |
83 | |
84 | QByteArray identifier() const; |
85 | void setIdentifier(const QByteArray &identifier); |
86 | |
87 | QByteArray type() const; |
88 | |
89 | Q_INVOKABLE bool connectToBackend(); |
90 | bool isConnectedToBackend() const; |
91 | |
92 | bool isBusy() const; |
93 | |
94 | void setActive(bool active); |
95 | bool isActive() const; |
96 | |
97 | bool isAlwaysOn() const; |
98 | void setAlwaysOn(bool alwaysOn); |
99 | |
100 | bool skipDuplicates() const; |
101 | void setSkipDuplicates(bool skipDuplicates); |
102 | |
103 | qrangelist availableDataRates() const; |
104 | int dataRate() const; |
105 | void setDataRate(int rate); |
106 | |
107 | qoutputrangelist outputRanges() const; |
108 | int outputRange() const; |
109 | void setOutputRange(int index); |
110 | |
111 | QString description() const; |
112 | int error() const; |
113 | |
114 | // Filters modify the reading |
115 | void addFilter(QSensorFilter *filter); |
116 | void removeFilter(QSensorFilter *filter); |
117 | QList<QSensorFilter*> filters() const; |
118 | |
119 | // The readings are exposed via this object |
120 | QSensorReading *reading() const; |
121 | |
122 | // Information about available sensors |
123 | // These functions are implemented in qsensormanager.cpp |
124 | static QList<QByteArray> sensorTypes(); |
125 | static QList<QByteArray> sensorsForType(const QByteArray &type); |
126 | static QByteArray defaultSensorForType(const QByteArray &type); |
127 | |
128 | Q_INVOKABLE bool isFeatureSupported(Feature feature) const; |
129 | |
130 | AxesOrientationMode axesOrientationMode() const; |
131 | void setAxesOrientationMode(AxesOrientationMode axesOrientationMode); |
132 | |
133 | int currentOrientation() const; |
134 | void setCurrentOrientation(int currentOrientation); |
135 | |
136 | int userOrientation() const; |
137 | void setUserOrientation(int userOrientation); |
138 | |
139 | int maxBufferSize() const; |
140 | void setMaxBufferSize(int maxBufferSize); |
141 | |
142 | int efficientBufferSize() const; |
143 | void setEfficientBufferSize(int efficientBufferSize); |
144 | |
145 | int bufferSize() const; |
146 | void setBufferSize(int bufferSize); |
147 | |
148 | public Q_SLOTS: |
149 | // Start receiving values from the sensor |
150 | bool start(); |
151 | |
152 | // Stop receiving values from the sensor |
153 | void stop(); |
154 | |
155 | Q_SIGNALS: |
156 | void busyChanged(); |
157 | void activeChanged(); |
158 | void readingChanged(); |
159 | void sensorError(int error); |
160 | void availableSensorsChanged(); |
161 | void alwaysOnChanged(); |
162 | void dataRateChanged(); |
163 | void skipDuplicatesChanged(bool skipDuplicates); |
164 | void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode); |
165 | void currentOrientationChanged(int currentOrientation); |
166 | void userOrientationChanged(int userOrientation); |
167 | void maxBufferSizeChanged(int maxBufferSize); |
168 | void efficientBufferSizeChanged(int efficientBufferSize); |
169 | void bufferSizeChanged(int bufferSize); |
170 | void identifierChanged(); |
171 | |
172 | protected: |
173 | explicit QSensor(const QByteArray &type, QSensorPrivate &dd, QObject* parent = nullptr); |
174 | QSensorBackend *backend() const; |
175 | |
176 | private: |
177 | void registerInstance(); |
178 | |
179 | Q_DISABLE_COPY(QSensor) |
180 | Q_DECLARE_PRIVATE(QSensor) |
181 | }; |
182 | |
183 | class Q_SENSORS_EXPORT QSensorFilter |
184 | { |
185 | friend class QSensor; |
186 | public: |
187 | virtual bool filter(QSensorReading *reading) = 0; |
188 | protected: |
189 | QSensorFilter(); |
190 | virtual ~QSensorFilter(); |
191 | virtual void setSensor(QSensor *sensor); |
192 | QSensor *m_sensor; |
193 | }; |
194 | |
195 | class Q_SENSORS_EXPORT QSensorReading : public QObject |
196 | { |
197 | friend class QSensorBackend; |
198 | |
199 | Q_OBJECT |
200 | Q_PROPERTY(quint64 timestamp READ timestamp) |
201 | public: |
202 | virtual ~QSensorReading(); |
203 | |
204 | quint64 timestamp() const; |
205 | void setTimestamp(quint64 timestamp); |
206 | |
207 | // Access properties of sub-classes by numeric index |
208 | // For name-based access use QObject::property() |
209 | int valueCount() const; |
210 | QVariant value(int index) const; |
211 | |
212 | protected: |
213 | explicit QSensorReading(QObject *parent, QSensorReadingPrivate *d); |
214 | QScopedPointer<QSensorReadingPrivate> *d_ptr() { return &d; } |
215 | virtual void copyValuesFrom(QSensorReading *other); |
216 | |
217 | private: |
218 | QScopedPointer<QSensorReadingPrivate> d; |
219 | Q_DISABLE_COPY(QSensorReading) |
220 | }; |
221 | |
222 | #define DECLARE_READING(classname)\ |
223 | DECLARE_READING_D(classname, classname ## Private) |
224 | |
225 | #define DECLARE_READING_D(classname, pclassname)\ |
226 | public:\ |
227 | classname(QObject *parent = nullptr);\ |
228 | virtual ~classname();\ |
229 | void copyValuesFrom(QSensorReading *other) override;\ |
230 | private:\ |
231 | QScopedPointer<pclassname> d; |
232 | |
233 | #define IMPLEMENT_READING(classname)\ |
234 | IMPLEMENT_READING_D(classname, classname ## Private) |
235 | |
236 | #define IMPLEMENT_READING_D(classname, pclassname)\ |
237 | classname::classname(QObject *parent)\ |
238 | : QSensorReading(parent, nullptr)\ |
239 | , d(new pclassname)\ |
240 | {}\ |
241 | classname::~classname() {}\ |
242 | void classname::copyValuesFrom(QSensorReading *_other)\ |
243 | {\ |
244 | /* No need to verify types, only called by QSensorBackend */\ |
245 | classname *other = static_cast<classname *>(_other);\ |
246 | pclassname *my_ptr = d.data();\ |
247 | pclassname *other_ptr = other->d.data();\ |
248 | /* Do a direct copy of the private class */\ |
249 | *(my_ptr) = *(other_ptr);\ |
250 | /* We need to copy the parent too */\ |
251 | QSensorReading::copyValuesFrom(_other);\ |
252 | } |
253 | |
254 | |
255 | QT_END_NAMESPACE |
256 | |
257 | Q_DECLARE_METATYPE(qrange) |
258 | Q_DECLARE_METATYPE(qrangelist) |
259 | Q_DECLARE_METATYPE(qoutputrangelist) |
260 | |
261 | #endif |
262 | |