qsensor.h 9.8 KB

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