sensor.h 37 KB

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