1
0

sensor.h 34 KB

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