sensor.h 34 KB

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