sensor.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * File : sensors.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2014, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2014-08-03 Bernard the first version
  13. */
  14. /* Modified from: https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */
  15. /*
  16. * Copyright (C) 2012 The Android Open Source Project
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. #ifndef SENSORS_H__
  31. #define SENSORS_H__
  32. #include <rtdevice.h>
  33. #include <stdint.h>
  34. #pragma anon_unions
  35. /**
  36. * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
  37. * A Handle identifies a given sensors. The handle is used to activate
  38. * and/or deactivate sensors.
  39. * In this version of the API there can only be 256 handles.
  40. */
  41. #define SENSORS_HANDLE_BASE 0
  42. #define SENSORS_HANDLE_BITS 8
  43. #define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS)
  44. /*
  45. * flags for (*batch)()
  46. * Availability: SENSORS_DEVICE_API_VERSION_1_0
  47. * see (*batch)() documentation for details
  48. */
  49. enum
  50. {
  51. SENSORS_BATCH_DRY_RUN = 0x00000001,
  52. SENSORS_BATCH_WAKE_UPON_FIFO_FULL = 0x00000002
  53. };
  54. /*
  55. * what field for meta_data_event_t
  56. */
  57. enum
  58. {
  59. /* a previous flush operation has completed */
  60. META_DATA_FLUSH_COMPLETE = 1,
  61. META_DATA_VERSION /* always last, leave auto-assigned */
  62. };
  63. /**
  64. * Definition of the axis used by the sensor HAL API
  65. *
  66. * This API is relative to the screen of the device in its default orientation,
  67. * that is, if the device can be used in portrait or landscape, this API
  68. * is only relative to the NATURAL orientation of the screen. In other words,
  69. * the axis are not swapped when the device's screen orientation changes.
  70. * Higher level services /may/ perform this transformation.
  71. *
  72. * x<0 x>0
  73. * ^
  74. * |
  75. * +-----------+--> y>0
  76. * | |
  77. * | |
  78. * | |
  79. * | | / z<0
  80. * | | /
  81. * | | /
  82. * O-----------+/
  83. * |[] [ ] []/
  84. * +----------/+ y<0
  85. * /
  86. * /
  87. * |/ z>0 (toward the sky)
  88. *
  89. * O: Origin (x=0,y=0,z=0)
  90. *
  91. */
  92. /*
  93. * Interaction with suspend mode
  94. *
  95. * Unless otherwise noted, an enabled sensor shall not prevent the
  96. * SoC to go into suspend mode. It is the responsibility of applications
  97. * to keep a partial wake-lock should they wish to receive sensor
  98. * events while the screen is off. While in suspend mode, and unless
  99. * otherwise noted (batch mode, sensor particularities, ...), enabled sensors'
  100. * events are lost.
  101. *
  102. * Note that conceptually, the sensor itself is not de-activated while in
  103. * suspend mode -- it's just that the data it returns are lost. As soon as
  104. * the SoC gets out of suspend mode, operations resume as usual. Of course,
  105. * in practice sensors shall be disabled while in suspend mode to
  106. * save power, unless batch mode is active, in which case they must
  107. * continue fill their internal FIFO (see the documentation of batch() to
  108. * learn how suspend interacts with batch mode).
  109. *
  110. * In batch mode, and only when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is
  111. * set and supported, the specified sensor must be able to wake-up the SoC and
  112. * be able to buffer at least 10 seconds worth of the requested sensor events.
  113. *
  114. * There are notable exceptions to this behavior, which are sensor-dependent
  115. * (see sensor types definitions below)
  116. *
  117. *
  118. * The sensor type documentation below specifies the wake-up behavior of
  119. * each sensor:
  120. * wake-up: yes this sensor must wake-up the SoC to deliver events
  121. * wake-up: no this sensor shall not wake-up the SoC, events are dropped
  122. *
  123. */
  124. /*
  125. * Sensor type
  126. *
  127. * Each sensor has a type which defines what this sensor measures and how
  128. * measures are reported. All types are defined below.
  129. *
  130. * Device manufacturers (OEMs) can define their own sensor types, for
  131. * their private use by applications or services provided by them. Such
  132. * sensor types are specific to an OEM and can't be exposed in the SDK.
  133. * These types must start at SENSOR_TYPE_DEVICE_PRIVATE_BASE.
  134. */
  135. /*
  136. * Base for device manufacturers private sensor types.
  137. * These sensor types can't be exposed in the SDK.
  138. */
  139. #define SENSOR_TYPE_DEVICE_PRIVATE_BASE 0x10000
  140. /*
  141. * Sensor fusion and virtual sensors
  142. *
  143. * Many sensor types are or can be implemented as virtual sensors from
  144. * physical sensors on the device. For instance the rotation vector sensor,
  145. * orientation sensor, step-detector, step-counter, etc...
  146. *
  147. * From the point of view of this API these virtual sensors MUST appear as
  148. * real, individual sensors. It is the responsibility of the driver and HAL
  149. * to make sure this is the case.
  150. *
  151. * In particular, all sensors must be able to function concurrently.
  152. * For example, if defining both an accelerometer and a step counter,
  153. * then both must be able to work concurrently.
  154. */
  155. /*
  156. * Trigger modes
  157. *
  158. * Sensors can report events in different ways called trigger modes,
  159. * each sensor type has one and only one trigger mode associated to it.
  160. * Currently there are four trigger modes defined:
  161. *
  162. * continuous: events are reported at a constant rate defined by setDelay().
  163. * eg: accelerometers, gyroscopes.
  164. * on-change: events are reported only if the sensor's value has changed.
  165. * setDelay() is used to set a lower limit to the reporting
  166. * period (minimum time between two events).
  167. * The HAL must return an event immediately when an on-change
  168. * sensor is activated.
  169. * eg: proximity, light sensors
  170. * one-shot: upon detection of an event, the sensor deactivates itself and
  171. * then sends a single event. Order matters to avoid race
  172. * conditions. No other event is sent until the sensor get
  173. * reactivated. setDelay() is ignored.
  174. * eg: significant motion sensor
  175. * special: see details in the sensor type specification below
  176. *
  177. */
  178. /*
  179. * SENSOR_TYPE_META_DATA
  180. * trigger-mode: n/a
  181. * wake-up sensor: n/a
  182. *
  183. * NO SENSOR OF THAT TYPE MUST BE RETURNED (*get_sensors_list)()
  184. *
  185. * SENSOR_TYPE_META_DATA is a special token used to populate the
  186. * sensors_meta_data_event structure. It doesn't correspond to a physical
  187. * sensor. sensors_meta_data_event are special, they exist only inside
  188. * the HAL and are generated spontaneously, as opposed to be related to
  189. * a physical sensor.
  190. *
  191. * sensors_meta_data_event_t.version must be META_DATA_VERSION
  192. * sensors_meta_data_event_t.sensor must be 0
  193. * sensors_meta_data_event_t.type must be SENSOR_TYPE_META_DATA
  194. * sensors_meta_data_event_t.reserved must be 0
  195. * sensors_meta_data_event_t.timestamp must be 0
  196. *
  197. * The payload is a meta_data_event_t, where:
  198. * meta_data_event_t.what can take the following values:
  199. *
  200. * META_DATA_FLUSH_COMPLETE
  201. * This event indicates that a previous (*flush)() call has completed for the sensor
  202. * handle specified in meta_data_event_t.sensor.
  203. * see (*flush)() for more details
  204. *
  205. * All other values for meta_data_event_t.what are reserved and
  206. * must not be used.
  207. *
  208. */
  209. #define SENSOR_TYPE_META_DATA (0)
  210. /*
  211. * SENSOR_TYPE_ACCELEROMETER
  212. * trigger-mode: continuous
  213. * wake-up sensor: no
  214. *
  215. * All values are in SI units (m/s^2) and measure the acceleration of the
  216. * device minus the force of gravity.
  217. *
  218. * Acceleration sensors return sensor events for all 3 axes at a constant
  219. * rate defined by setDelay().
  220. *
  221. * x: Acceleration on the x-axis
  222. * y: Acceleration on the y-axis
  223. * z: Acceleration on the z-axis
  224. *
  225. * Note that the readings from the accelerometer include the acceleration
  226. * due to gravity (which is opposite to the direction of the gravity vector).
  227. *
  228. * Examples:
  229. * The norm of <x, y, z> should be close to 0 when in free fall.
  230. *
  231. * When the device lies flat on a table and is pushed on its left side
  232. * toward the right, the x acceleration value is positive.
  233. *
  234. * When the device lies flat on a table, the acceleration value is +9.81,
  235. * which correspond to the acceleration of the device (0 m/s^2) minus the
  236. * force of gravity (-9.81 m/s^2).
  237. *
  238. * When the device lies flat on a table and is pushed toward the sky, the
  239. * acceleration value is greater than +9.81, which correspond to the
  240. * acceleration of the device (+A m/s^2) minus the force of
  241. * gravity (-9.81 m/s^2).
  242. */
  243. #define SENSOR_TYPE_ACCELEROMETER (1)
  244. /*
  245. * SENSOR_TYPE_GEOMAGNETIC_FIELD
  246. * trigger-mode: continuous
  247. * wake-up sensor: no
  248. *
  249. * All values are in micro-Tesla (uT) and measure the geomagnetic
  250. * field in the X, Y and Z axis.
  251. *
  252. * Returned values include calibration mechanisms such that the vector is
  253. * aligned with the magnetic declination and heading of the earth's
  254. * geomagnetic field.
  255. *
  256. * Magnetic Field sensors return sensor events for all 3 axes at a constant
  257. * rate defined by setDelay().
  258. */
  259. #define SENSOR_TYPE_GEOMAGNETIC_FIELD (2)
  260. #define SENSOR_TYPE_MAGNETIC_FIELD SENSOR_TYPE_GEOMAGNETIC_FIELD
  261. /*
  262. * SENSOR_TYPE_ORIENTATION
  263. * trigger-mode: continuous
  264. * wake-up sensor: no
  265. *
  266. * All values are angles in degrees.
  267. *
  268. * Orientation sensors return sensor events for all 3 axes at a constant
  269. * rate defined by setDelay().
  270. *
  271. * azimuth: angle between the magnetic north direction and the Y axis, around
  272. * the Z axis (0<=azimuth<360).
  273. * 0=North, 90=East, 180=South, 270=West
  274. *
  275. * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
  276. * the z-axis moves toward the y-axis.
  277. *
  278. * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
  279. * the x-axis moves towards the z-axis.
  280. *
  281. * Note: For historical reasons the roll angle is positive in the clockwise
  282. * direction (mathematically speaking, it should be positive in the
  283. * counter-clockwise direction):
  284. *
  285. * Z
  286. * ^
  287. * (+roll) .--> |
  288. * / |
  289. * | | roll: rotation around Y axis
  290. * X <-------(.)
  291. * Y
  292. * note that +Y == -roll
  293. *
  294. *
  295. *
  296. * Note: This definition is different from yaw, pitch and roll used in aviation
  297. * where the X axis is along the long side of the plane (tail to nose).
  298. */
  299. #define SENSOR_TYPE_ORIENTATION (3)
  300. /*
  301. * SENSOR_TYPE_GYROSCOPE
  302. * trigger-mode: continuous
  303. * wake-up sensor: no
  304. *
  305. * All values are in radians/second and measure the rate of rotation
  306. * around the X, Y and Z axis. The coordinate system is the same as is
  307. * used for the acceleration sensor. Rotation is positive in the
  308. * counter-clockwise direction (right-hand rule). That is, an observer
  309. * looking from some positive location on the x, y or z axis at a device
  310. * positioned on the origin would report positive rotation if the device
  311. * appeared to be rotating counter clockwise. Note that this is the
  312. * standard mathematical definition of positive rotation and does not agree
  313. * with the definition of roll given earlier.
  314. * The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
  315. *
  316. * automatic gyro-drift compensation is allowed but not required.
  317. */
  318. #define SENSOR_TYPE_GYROSCOPE (4)
  319. /*
  320. * SENSOR_TYPE_LIGHT
  321. * trigger-mode: on-change
  322. * wake-up sensor: no
  323. *
  324. * The light sensor value is returned in SI lux units.
  325. */
  326. #define SENSOR_TYPE_LIGHT (5)
  327. /*
  328. * SENSOR_TYPE_PRESSURE
  329. * trigger-mode: continuous
  330. * wake-up sensor: no
  331. *
  332. * The pressure sensor return the athmospheric pressure in hectopascal (hPa)
  333. */
  334. #define SENSOR_TYPE_PRESSURE (6)
  335. /* SENSOR_TYPE_TEMPERATURE is deprecated in the HAL */
  336. #define SENSOR_TYPE_TEMPERATURE (7)
  337. /*
  338. * SENSOR_TYPE_PROXIMITY
  339. * trigger-mode: on-change
  340. * wake-up sensor: yes
  341. *
  342. * The distance value is measured in centimeters. Note that some proximity
  343. * sensors only support a binary "close" or "far" measurement. In this case,
  344. * the sensor should report its maxRange value in the "far" state and a value
  345. * less than maxRange in the "near" state.
  346. */
  347. #define SENSOR_TYPE_PROXIMITY (8)
  348. /*
  349. * SENSOR_TYPE_GRAVITY
  350. * trigger-mode: continuous
  351. * wake-up sensor: no
  352. *
  353. * A gravity output indicates the direction of and magnitude of gravity in
  354. * the devices's coordinates. On Earth, the magnitude is 9.8 m/s^2.
  355. * Units are m/s^2. The coordinate system is the same as is used for the
  356. * acceleration sensor. When the device is at rest, the output of the
  357. * gravity sensor should be identical to that of the accelerometer.
  358. */
  359. #define SENSOR_TYPE_GRAVITY (9)
  360. /*
  361. * SENSOR_TYPE_LINEAR_ACCELERATION
  362. * trigger-mode: continuous
  363. * wake-up sensor: no
  364. *
  365. * Indicates the linear acceleration of the device in device coordinates,
  366. * not including gravity.
  367. *
  368. * The output is conceptually:
  369. * output of TYPE_ACCELERATION - output of TYPE_GRAVITY
  370. *
  371. * Readings on all axes should be close to 0 when device lies on a table.
  372. * Units are m/s^2.
  373. * The coordinate system is the same as is used for the acceleration sensor.
  374. */
  375. #define SENSOR_TYPE_LINEAR_ACCELERATION (10)
  376. /*
  377. * SENSOR_TYPE_ROTATION_VECTOR
  378. * trigger-mode: continuous
  379. * wake-up sensor: no
  380. *
  381. * The rotation vector symbolizes the orientation of the device relative to the
  382. * East-North-Up coordinates frame. It is usually obtained by integration of
  383. * accelerometer, gyroscope and magnetometer readings.
  384. *
  385. * The East-North-Up coordinate system is defined as a direct orthonormal basis
  386. * where:
  387. * - X points east and is tangential to the ground.
  388. * - Y points north and is tangential to the ground.
  389. * - Z points towards the sky and is perpendicular to the ground.
  390. *
  391. * The orientation of the phone is represented by the rotation necessary to
  392. * align the East-North-Up coordinates with the phone's coordinates. That is,
  393. * applying the rotation to the world frame (X,Y,Z) would align them with the
  394. * phone coordinates (x,y,z).
  395. *
  396. * The rotation can be seen as rotating the phone by an angle theta around
  397. * an axis rot_axis to go from the reference (East-North-Up aligned) device
  398. * orientation to the current device orientation.
  399. *
  400. * The rotation is encoded as the 4 (reordered) components of a unit quaternion:
  401. * sensors_event_t.data[0] = rot_axis.x*sin(theta/2)
  402. * sensors_event_t.data[1] = rot_axis.y*sin(theta/2)
  403. * sensors_event_t.data[2] = rot_axis.z*sin(theta/2)
  404. * sensors_event_t.data[3] = cos(theta/2)
  405. * where
  406. * - rot_axis.x,y,z are the North-East-Up coordinates of a unit length vector
  407. * representing the rotation axis
  408. * - theta is the rotation angle
  409. *
  410. * The quaternion must be of norm 1 (it is a unit quaternion). Failure to ensure
  411. * this will cause erratic client behaviour.
  412. *
  413. * In addition, this sensor reports an estimated heading accuracy.
  414. * sensors_event_t.data[4] = estimated_accuracy (in radians)
  415. * The heading error must be less than estimated_accuracy 95% of the time
  416. *
  417. * This sensor must use a gyroscope and an accelerometer as main orientation
  418. * change input.
  419. *
  420. * This sensor can also include magnetometer input to make up for gyro drift,
  421. * but it cannot be implemented using only a magnetometer.
  422. */
  423. #define SENSOR_TYPE_ROTATION_VECTOR (11)
  424. /*
  425. * SENSOR_TYPE_RELATIVE_HUMIDITY
  426. * trigger-mode: on-change
  427. * wake-up sensor: no
  428. *
  429. * A relative humidity sensor measures relative ambient air humidity and
  430. * returns a value in percent.
  431. */
  432. #define SENSOR_TYPE_RELATIVE_HUMIDITY (12)
  433. /*
  434. * SENSOR_TYPE_AMBIENT_TEMPERATURE
  435. * trigger-mode: on-change
  436. * wake-up sensor: no
  437. *
  438. * The ambient (room) temperature in degree Celsius.
  439. */
  440. #define SENSOR_TYPE_AMBIENT_TEMPERATURE (13)
  441. /*
  442. * SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED
  443. * trigger-mode: continuous
  444. * wake-up sensor: no
  445. *
  446. * Similar to SENSOR_TYPE_MAGNETIC_FIELD, but the hard iron calibration is
  447. * reported separately instead of being included in the measurement.
  448. * Factory calibration and temperature compensation should still be applied to
  449. * the "uncalibrated" measurement.
  450. * Separating away the hard iron calibration estimation allows the system to
  451. * better recover from bad hard iron estimation.
  452. *
  453. * All values are in micro-Tesla (uT) and measure the ambient magnetic
  454. * field in the X, Y and Z axis. Assumptions that the the magnetic field
  455. * is due to the Earth's poles should be avoided.
  456. *
  457. * The uncalibrated_magnetic event contains
  458. * - 3 fields for uncalibrated measurement: x_uncalib, y_uncalib, z_uncalib.
  459. * Each is a component of the measured magnetic field, with soft iron
  460. * and temperature compensation applied, but not hard iron calibration.
  461. * These values should be continuous (no re-calibration should cause a jump).
  462. * - 3 fields for hard iron bias estimates: x_bias, y_bias, z_bias.
  463. * Each field is a component of the estimated hard iron calibration.
  464. * They represent the offsets to apply to the calibrated readings to obtain
  465. * uncalibrated readings (x_uncalib ~= x_calibrated + x_bias)
  466. * These values are expected to jump as soon as the estimate of the hard iron
  467. * changes, and they should be stable the rest of the time.
  468. *
  469. * If this sensor is present, then the corresponding
  470. * SENSOR_TYPE_MAGNETIC_FIELD must be present and both must return the
  471. * same sensor_t::name and sensor_t::vendor.
  472. *
  473. * Minimum filtering should be applied to this sensor. In particular, low pass
  474. * filters should be avoided.
  475. *
  476. * See SENSOR_TYPE_MAGNETIC_FIELD for more information
  477. */
  478. #define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED (14)
  479. /*
  480. * SENSOR_TYPE_GAME_ROTATION_VECTOR
  481. * trigger-mode: continuous
  482. * wake-up sensor: no
  483. *
  484. * Similar to SENSOR_TYPE_ROTATION_VECTOR, but not using the geomagnetic
  485. * field. Therefore the Y axis doesn't point north, but instead to some other
  486. * reference. That reference is allowed to drift by the same order of
  487. * magnitude than the gyroscope drift around the Z axis.
  488. *
  489. * This sensor does not report an estimated heading accuracy:
  490. * sensors_event_t.data[4] is reserved and should be set to 0
  491. *
  492. * In the ideal case, a phone rotated and returning to the same real-world
  493. * orientation should report the same game rotation vector
  494. * (without using the earth's geomagnetic field).
  495. *
  496. * This sensor must be based on a gyroscope. It cannot be implemented using
  497. * a magnetometer.
  498. *
  499. * see SENSOR_TYPE_ROTATION_VECTOR for more details
  500. */
  501. #define SENSOR_TYPE_GAME_ROTATION_VECTOR (15)
  502. /*
  503. * SENSOR_TYPE_GYROSCOPE_UNCALIBRATED
  504. * trigger-mode: continuous
  505. * wake-up sensor: no
  506. *
  507. * All values are in radians/second and measure the rate of rotation
  508. * around the X, Y and Z axis. An estimation of the drift on each axis is
  509. * reported as well.
  510. *
  511. * No gyro-drift compensation shall be performed.
  512. * Factory calibration and temperature compensation should still be applied
  513. * to the rate of rotation (angular speeds).
  514. *
  515. * The coordinate system is the same as is
  516. * used for the acceleration sensor. Rotation is positive in the
  517. * counter-clockwise direction (right-hand rule). That is, an observer
  518. * looking from some positive location on the x, y or z axis at a device
  519. * positioned on the origin would report positive rotation if the device
  520. * appeared to be rotating counter clockwise. Note that this is the
  521. * standard mathematical definition of positive rotation and does not agree
  522. * with the definition of roll given earlier.
  523. * The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
  524. *
  525. * Content of an uncalibrated_gyro event: (units are rad/sec)
  526. * x_uncalib : angular speed (w/o drift compensation) around the X axis
  527. * y_uncalib : angular speed (w/o drift compensation) around the Y axis
  528. * z_uncalib : angular speed (w/o drift compensation) around the Z axis
  529. * x_bias : estimated drift around X axis in rad/s
  530. * y_bias : estimated drift around Y axis in rad/s
  531. * z_bias : estimated drift around Z axis in rad/s
  532. *
  533. * IMPLEMENTATION NOTES:
  534. *
  535. * If the implementation is not able to estimate the drift, then this
  536. * sensor MUST NOT be reported by this HAL. Instead, the regular
  537. * SENSOR_TYPE_GYROSCOPE is used without drift compensation.
  538. *
  539. * If this sensor is present, then the corresponding
  540. * SENSOR_TYPE_GYROSCOPE must be present and both must return the
  541. * same sensor_t::name and sensor_t::vendor.
  542. */
  543. #define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED (16)
  544. /*
  545. * SENSOR_TYPE_SIGNIFICANT_MOTION
  546. * trigger-mode: one-shot
  547. * wake-up sensor: yes
  548. *
  549. * A sensor of this type triggers an event each time significant motion
  550. * is detected and automatically disables itself.
  551. * The only allowed value to return is 1.0.
  552. *
  553. * A significant motion is a motion that might lead to a change in the user
  554. * location.
  555. * Examples of such motions are:
  556. * walking, biking, sitting in a moving car, coach or train.
  557. * Examples of situations that should not trigger significant motion:
  558. * - phone in pocket and person is not moving
  559. * - phone is on a table, even if the table shakes a bit due to nearby traffic
  560. * or washing machine
  561. *
  562. * A note on false positive / false negative / power consumption tradeoff
  563. * - The goal of this sensor is to save power.
  564. * - Triggering an event when the user is not moving (false positive) is costly
  565. * in terms of power, so it should be avoided.
  566. * - Not triggering an event when the user is moving (false negative) is
  567. * acceptable as long as it is not done repeatedly. If the user has been
  568. * walking for 10 seconds, not triggering an event within those 10 seconds
  569. * is not acceptable.
  570. *
  571. * IMPORTANT NOTE: this sensor type is very different from other types
  572. * in that it must work when the screen is off without the need of
  573. * holding a partial wake-lock and MUST allow the SoC to go into suspend.
  574. * When significant motion is detected, the sensor must awaken the SoC and
  575. * the event be reported.
  576. *
  577. * If a particular hardware cannot support this mode of operation then this
  578. * sensor type MUST NOT be reported by the HAL. ie: it is not acceptable
  579. * to "emulate" this sensor in the HAL.
  580. *
  581. * The whole point of this sensor type is to save power by keeping the
  582. * SoC in suspend mode when the device is at rest.
  583. *
  584. * When the sensor is not activated, it must also be deactivated in the
  585. * hardware: it must not wake up the SoC anymore, even in case of
  586. * significant motion.
  587. *
  588. * setDelay() has no effect and is ignored.
  589. * Once a "significant motion" event is returned, a sensor of this type
  590. * must disables itself automatically, as if activate(..., 0) had been called.
  591. */
  592. #define SENSOR_TYPE_SIGNIFICANT_MOTION (17)
  593. /*
  594. * SENSOR_TYPE_STEP_DETECTOR
  595. * trigger-mode: special
  596. * wake-up sensor: no
  597. *
  598. * A sensor of this type triggers an event each time a step is taken
  599. * by the user. The only allowed value to return is 1.0 and an event is
  600. * generated for each step. Like with any other event, the timestamp
  601. * indicates when the event (here the step) occurred, this corresponds to when
  602. * the foot hit the ground, generating a high variation in acceleration.
  603. *
  604. * While this sensor operates, it shall not disrupt any other sensors, in
  605. * particular, but not limited to, the accelerometer; which might very well
  606. * be in use as well.
  607. *
  608. * This sensor must be low power. That is, if the step detection cannot be
  609. * done in hardware, this sensor should not be defined. Also, when the
  610. * step detector is activated and the accelerometer is not, only steps should
  611. * trigger interrupts (not accelerometer data).
  612. *
  613. * setDelay() has no impact on this sensor type
  614. */
  615. #define SENSOR_TYPE_STEP_DETECTOR (18)
  616. /*
  617. * SENSOR_TYPE_STEP_COUNTER
  618. * trigger-mode: on-change
  619. * wake-up sensor: no
  620. *
  621. * A sensor of this type returns the number of steps taken by the user since
  622. * the last reboot while activated. The value is returned as a uint64_t and is
  623. * reset to zero only on a system / android reboot.
  624. *
  625. * The timestamp of the event is set to the time when the first step
  626. * for that event was taken.
  627. * See SENSOR_TYPE_STEP_DETECTOR for the signification of the time of a step.
  628. *
  629. * The minimum size of the hardware's internal counter shall be 16 bits
  630. * (this restriction is here to avoid too frequent wake-ups when the
  631. * delay is very large).
  632. *
  633. * IMPORTANT NOTE: this sensor type is different from other types
  634. * in that it must work when the screen is off without the need of
  635. * holding a partial wake-lock and MUST allow the SoC to go into suspend.
  636. * Unlike other sensors, while in suspend mode this sensor must stay active,
  637. * no events are reported during that time but, steps continue to be
  638. * accounted for; an event will be reported as soon as the SoC resumes if
  639. * the timeout has expired.
  640. *
  641. * In other words, when the screen is off and the device allowed to
  642. * go into suspend mode, we don't want to be woken up, regardless of the
  643. * setDelay() value, but the steps shall continue to be counted.
  644. *
  645. * The driver must however ensure that the internal step count never
  646. * overflows. It is allowed in this situation to wake the SoC up so the
  647. * driver can do the counter maintenance.
  648. *
  649. * While this sensor operates, it shall not disrupt any other sensors, in
  650. * particular, but not limited to, the accelerometer; which might very well
  651. * be in use as well.
  652. *
  653. * If a particular hardware cannot support these modes of operation then this
  654. * sensor type MUST NOT be reported by the HAL. ie: it is not acceptable
  655. * to "emulate" this sensor in the HAL.
  656. *
  657. * This sensor must be low power. That is, if the step detection cannot be
  658. * done in hardware, this sensor should not be defined. Also, when the
  659. * step counter is activated and the accelerometer is not, only steps should
  660. * trigger interrupts (not accelerometer data).
  661. *
  662. * The whole point of this sensor type is to save power by keeping the
  663. * SoC in suspend mode when the device is at rest.
  664. */
  665. #define SENSOR_TYPE_STEP_COUNTER (19)
  666. /*
  667. * SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR
  668. * trigger-mode: continuous
  669. * wake-up sensor: no
  670. *
  671. * Similar to SENSOR_TYPE_ROTATION_VECTOR, but using a magnetometer instead
  672. * of using a gyroscope.
  673. *
  674. * This sensor must be based on a magnetometer. It cannot be implemented using
  675. * a gyroscope, and gyroscope input cannot be used by this sensor, as the
  676. * goal of this sensor is to be low power.
  677. * The accelerometer can be (and usually is) used.
  678. *
  679. * Just like SENSOR_TYPE_ROTATION_VECTOR, this sensor reports an estimated
  680. * heading accuracy:
  681. * sensors_event_t.data[4] = estimated_accuracy (in radians)
  682. * The heading error must be less than estimated_accuracy 95% of the time
  683. *
  684. * see SENSOR_TYPE_ROTATION_VECTOR for more details
  685. */
  686. #define SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR (20)
  687. /**
  688. * Values returned by the accelerometer in various locations in the universe.
  689. * all values are in SI units (m/s^2)
  690. */
  691. #define SENSORS_GRAVITY_SUN (275.0f)
  692. #define SENSORS_GRAVITY_MOON (1.6f)
  693. #define SENSORS_GRAVITY_EARTH (9.80665f)
  694. #define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
  695. /** Maximum magnetic field on Earth's surface */
  696. #define MAGNETIC_FIELD_EARTH_MAX (60.0f)
  697. /** Minimum magnetic field on Earth's surface */
  698. #define MAGNETIC_FIELD_EARTH_MIN (30.0f)
  699. /** Average sea level pressure is 1013.25 hPa */
  700. #define SENSORS_PRESSURE_SEALEVELHPA (1013.25F)
  701. /** Degrees/s to rad/s multiplier */
  702. #define SENSORS_DPS_TO_RADS (0.017453293F)
  703. /** Gauss to micro-Tesla multiplier */
  704. #define SENSORS_GAUSS_TO_MICROTESLA (100)
  705. /**
  706. * status of orientation sensor
  707. */
  708. #define SENSOR_STATUS_UNRELIABLE 0
  709. #define SENSOR_STATUS_ACCURACY_LOW 1
  710. #define SENSOR_STATUS_ACCURACY_MEDIUM 2
  711. #define SENSOR_STATUS_ACCURACY_HIGH 3
  712. /**
  713. * sensor event data
  714. */
  715. typedef struct
  716. {
  717. union
  718. {
  719. float v[3];
  720. struct
  721. {
  722. float x;
  723. float y;
  724. float z;
  725. };
  726. struct
  727. {
  728. float azimuth;
  729. float pitch;
  730. float roll;
  731. };
  732. };
  733. int8_t status;
  734. uint8_t reserved[3];
  735. } sensors_vec_t;
  736. /**
  737. * sensor raw vector data
  738. */
  739. typedef struct
  740. {
  741. struct
  742. {
  743. int16_t x;
  744. int16_t y;
  745. int16_t z;
  746. };
  747. int8_t status;
  748. uint8_t reserved[1];
  749. } sensors_raw_vec_t;
  750. /**
  751. * uncalibrated gyroscope and magnetometer event data
  752. */
  753. typedef struct
  754. {
  755. union
  756. {
  757. float uncalib[3];
  758. struct
  759. {
  760. float x_uncalib;
  761. float y_uncalib;
  762. float z_uncalib;
  763. };
  764. };
  765. union
  766. {
  767. float bias[3];
  768. struct
  769. {
  770. float x_bias;
  771. float y_bias;
  772. float z_bias;
  773. };
  774. };
  775. } uncalibrated_event_t;
  776. typedef struct meta_data_event
  777. {
  778. int32_t what;
  779. int32_t sensor;
  780. } meta_data_event_t;
  781. /**
  782. * Union of the various types of sensor data
  783. * that can be returned.
  784. */
  785. typedef struct sensors_event_t
  786. {
  787. /* must be sizeof(struct sensors_event_t) */
  788. int32_t version;
  789. /* sensor identifier */
  790. int32_t sensor;
  791. /* sensor type */
  792. int32_t type;
  793. /* reserved */
  794. int32_t reserved0;
  795. /* time is in nanosecond */
  796. int64_t timestamp;
  797. union
  798. {
  799. union
  800. {
  801. float data[16];
  802. /* acceleration values are in meter per second per second (m/s^2) */
  803. sensors_vec_t acceleration;
  804. /* raw acceleration data */
  805. sensors_raw_vec_t raw_acceleration;
  806. /* magnetic vector values are in micro-Tesla (uT) */
  807. sensors_vec_t magnetic;
  808. /* raw magnetic data */
  809. sensors_raw_vec_t raw_magnetic;
  810. /* orientation values are in degrees */
  811. sensors_vec_t orientation;
  812. /* gyroscope values are in rad/s */
  813. sensors_vec_t gyro;
  814. /* raw gyroscope data */
  815. sensors_raw_vec_t raw_gyro;
  816. /* temperature is in degrees centigrade (Celsius) */
  817. float temperature;
  818. /* distance in centimeters */
  819. float distance;
  820. /* light in SI lux units */
  821. float light;
  822. /* pressure in hectopascal (hPa) */
  823. float pressure;
  824. /* relative humidity in percent */
  825. float relative_humidity;
  826. /* uncalibrated gyroscope values are in rad/s */
  827. uncalibrated_event_t uncalibrated_gyro;
  828. /* uncalibrated magnetometer values are in micro-Teslas */
  829. uncalibrated_event_t uncalibrated_magnetic;
  830. /* this is a special event. see SENSOR_TYPE_META_DATA above.
  831. * sensors_meta_data_event_t events are all reported with a type of
  832. * SENSOR_TYPE_META_DATA. The handle is ignored and must be zero.
  833. */
  834. meta_data_event_t meta_data;
  835. };
  836. union
  837. {
  838. uint64_t data[8];
  839. /* step-counter */
  840. uint64_t step_counter;
  841. } u64;
  842. };
  843. uint32_t reserved1[4];
  844. } sensors_event_t;
  845. /* see SENSOR_TYPE_META_DATA */
  846. typedef sensors_event_t sensors_meta_data_event_t;
  847. typedef struct sensor_t
  848. {
  849. /* Name of this sensor.
  850. * All sensors of the same "type" must have a different "name".
  851. */
  852. const char *name;
  853. /* vendor of the hardware part */
  854. const char *vendor;
  855. /* version of the hardware part + driver. The value of this field
  856. * must increase when the driver is updated in a way that changes the
  857. * output of this sensor. This is important for fused sensors when the
  858. * fusion algorithm is updated.
  859. */
  860. int version;
  861. /* handle that identifies this sensors. This handle is used to reference
  862. * this sensor throughout the HAL API.
  863. */
  864. int handle;
  865. /* this sensor's type. */
  866. int type;
  867. /* maximum range of this sensor's value in SI units */
  868. float maxRange;
  869. /* smallest difference between two values reported by this sensor */
  870. float resolution;
  871. /* rough estimate of this sensor's power consumption in mA */
  872. float power;
  873. /* this value depends on the trigger mode:
  874. *
  875. * continuous: minimum sample period allowed in microseconds
  876. * on-change : 0
  877. * one-shot :-1
  878. * special : 0, unless otherwise noted
  879. */
  880. int32_t minDelay;
  881. /* number of events reserved for this sensor in the batch mode FIFO.
  882. * If there is a dedicated FIFO for this sensor, then this is the
  883. * size of this FIFO. If the FIFO is shared with other sensors,
  884. * this is the size reserved for that sensor and it can be zero.
  885. */
  886. uint32_t fifoReservedEventCount;
  887. /* maximum number of events of this sensor that could be batched.
  888. * This is especially relevant when the FIFO is shared between
  889. * several sensors; this value is then set to the size of that FIFO.
  890. */
  891. uint32_t fifoMaxEventCount;
  892. /* reserved fields, must be zero */
  893. void *reserved[6];
  894. } sensor_t;
  895. enum SensorMode
  896. {
  897. SENSOR_MODE_RAW,
  898. SENSOR_MODE_CALIBRATED,
  899. SENSOR_MODE_NORMAL,
  900. };
  901. enum SensorAccelRange
  902. {
  903. SENSOR_ACCEL_RANGE_2G,
  904. SENSOR_ACCEL_RANGE_4G,
  905. SENSOR_ACCEL_RANGE_8G,
  906. SENSOR_ACCEL_RANGE_16G,
  907. };
  908. #define SENSOR_ACCEL_SENSITIVITY_2G (0.001F)
  909. #define SENSOR_ACCEL_SENSITIVITY_4G (0.002F)
  910. #define SENSOR_ACCEL_SENSITIVITY_8G (0.004F)
  911. #define SENSOR_ACCEL_SENSITIVITY_16G (0.012F)
  912. enum SensorGyroRange
  913. {
  914. SENSOR_GYRO_RANGE_250DPS,
  915. SENSOR_GYRO_RANGE_500DPS,
  916. SENSOR_GYRO_RANGE_1000DPS,
  917. SENSOR_GYRO_RANGE_2000DPS,
  918. };
  919. #define SENSOR_GYRO_SENSITIVITY_250DPS (0.00875F)
  920. #define SENSOR_GYRO_SENSITIVITY_500DPS (0.0175F)
  921. #define SENSOR_GYRO_SENSITIVITY_1000DPS (0.035F)
  922. #define SENSOR_GYRO_SENSITIVITY_2000DPS (0.070F)
  923. enum SensorDataRate
  924. {
  925. SENSOR_DATARATE_3200HZ,
  926. SENSOR_DATARATE_1600HZ,
  927. SENSOR_DATARATE_800HZ,
  928. SENSOR_DATARATE_400HZ,
  929. SENSOR_DATARATE_200HZ,
  930. SENSOR_DATARATE_100HZ,
  931. SENSOR_DATARATE_50HZ,
  932. SENSOR_DATARATE_25HZ,
  933. SENSOR_DATARATE_12_5HZ,
  934. SENSOR_DATARATE_6_25HZ,
  935. SENSOR_DATARATE_3_13HZ,
  936. SENSOR_DATARATE_1_56HZ,
  937. SENSOR_DATARATE_0_78HZ,
  938. SENSOR_DATARATE_0_39HZ,
  939. SENSOR_DATARATE_0_20HZ,
  940. SENSOR_DATARATE_0_10HZ,
  941. };
  942. class SensorBase;
  943. class SensorManager;
  944. typedef void (*SensorEventHandler_t)(SensorBase *sensor, sensors_event_t *event, void *user_data);
  945. /**
  946. * Sensor Configuration
  947. */
  948. typedef struct SensorConfig
  949. {
  950. int mode;
  951. enum SensorDataRate data_rate;
  952. union range
  953. {
  954. enum SensorAccelRange accel_range;
  955. enum SensorGyroRange gyro_range;
  956. } range;
  957. }SensorConfig;
  958. /**
  959. * Sensor Base Class
  960. */
  961. class SensorBase
  962. {
  963. private:
  964. int type;
  965. public:
  966. SensorBase(int type);
  967. ~SensorBase();
  968. virtual int configure(SensorConfig *config) = 0;
  969. virtual int activate(int enable) = 0;
  970. virtual int poll(sensors_event_t *events) = 0;
  971. virtual void getSensor(struct sensor_t *sensor) = 0;
  972. int getType(void);
  973. int setConfig(SensorConfig *config);
  974. int getConfig(SensorConfig *config);
  975. int subscribe(SensorEventHandler_t *handler, void *user_data);
  976. int publish(sensors_event_t *event);
  977. protected:
  978. SensorBase *next;
  979. SensorBase *prev;
  980. /* sensor configuration */
  981. SensorConfig config;
  982. SensorEventHandler_t *evtHandler;
  983. void *userData;
  984. friend class SensorManager;
  985. };
  986. /**
  987. * Sensor Manager
  988. */
  989. class SensorManager
  990. {
  991. public:
  992. SensorManager();
  993. ~SensorManager();
  994. static int registerSensor(SensorBase *sensor);
  995. static int unregisterSensor(SensorBase *sensor);
  996. static SensorBase *getDefaultSensor(int type);
  997. static int subscribe(int type, SensorEventHandler_t *handler, void *user_data);
  998. static int sensorEventReady(SensorBase *sensor);
  999. static int pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration);
  1000. };
  1001. #endif