Adafruit_Sensor.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software< /span>
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Update by K. Townsend (Adafruit Industries) for lighter typedefs, and
  17. * extended sensor support to include color, voltage and current */
  18. #ifndef _ADAFRUIT_SENSOR_H
  19. #define _ADAFRUIT_SENSOR_H
  20. #ifndef ARDUINO
  21. #include <stdint.h>
  22. #elif ARDUINO >= 100
  23. #include "Arduino.h"
  24. #include "Print.h"
  25. #else
  26. #include "WProgram.h"
  27. #endif
  28. /* Constants */
  29. #define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
  30. #define SENSORS_GRAVITY_MOON (1.6F) /**< The moon's gravity in m/s^2 */
  31. #define SENSORS_GRAVITY_SUN (275.0F) /**< The sun's gravity in m/s^2 */
  32. #define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
  33. #define SENSORS_MAGFIELD_EARTH_MAX \
  34. (60.0F) /**< Maximum magnetic field on Earth's surface */
  35. #define SENSORS_MAGFIELD_EARTH_MIN \
  36. (30.0F) /**< Minimum magnetic field on Earth's surface */
  37. #define SENSORS_PRESSURE_SEALEVELHPA \
  38. (1013.25F) /**< Average sea level pressure is 1013.25 hPa */
  39. #define SENSORS_DPS_TO_RADS \
  40. (0.017453293F) /**< Degrees/s to rad/s multiplier \
  41. */
  42. #define SENSORS_RADS_TO_DPS \
  43. (57.29577793F) /**< Rad/s to degrees/s multiplier */
  44. #define SENSORS_GAUSS_TO_MICROTESLA \
  45. (100) /**< Gauss to micro-Tesla multiplier */
  46. /** Sensor types */
  47. typedef enum {
  48. SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
  49. SENSOR_TYPE_MAGNETIC_FIELD = (2),
  50. SENSOR_TYPE_ORIENTATION = (3),
  51. SENSOR_TYPE_GYROSCOPE = (4),
  52. SENSOR_TYPE_LIGHT = (5),
  53. SENSOR_TYPE_PRESSURE = (6),
  54. SENSOR_TYPE_PROXIMITY = (8),
  55. SENSOR_TYPE_GRAVITY = (9),
  56. SENSOR_TYPE_LINEAR_ACCELERATION =
  57. (10), /**< Acceleration not including gravity */
  58. SENSOR_TYPE_ROTATION_VECTOR = (11),
  59. SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
  60. SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
  61. SENSOR_TYPE_OBJECT_TEMPERATURE = (14),
  62. SENSOR_TYPE_VOLTAGE = (15),
  63. SENSOR_TYPE_CURRENT = (16),
  64. SENSOR_TYPE_COLOR = (17),
  65. SENSOR_TYPE_TVOC = (18),
  66. SENSOR_TYPE_VOC_INDEX = (19),
  67. SENSOR_TYPE_NOX_INDEX = (20),
  68. SENSOR_TYPE_CO2 = (21),
  69. SENSOR_TYPE_ECO2 = (22),
  70. SENSOR_TYPE_PM10_STD = (23),
  71. SENSOR_TYPE_PM25_STD = (24),
  72. SENSOR_TYPE_PM100_STD = (25),
  73. SENSOR_TYPE_PM10_ENV = (26),
  74. SENSOR_TYPE_PM25_ENV = (27),
  75. SENSOR_TYPE_PM100_ENV = (28),
  76. SENSOR_TYPE_GAS_RESISTANCE = (29),
  77. SENSOR_TYPE_UNITLESS_PERCENT = (30),
  78. SENSOR_TYPE_ALTITUDE = (31)
  79. } sensors_type_t;
  80. /** struct sensors_vec_s is used to return a vector in a common format. */
  81. typedef struct {
  82. union {
  83. float v[3]; ///< 3D vector elements
  84. struct {
  85. float x; ///< X component of vector
  86. float y; ///< Y component of vector
  87. float z; ///< Z component of vector
  88. }; ///< Struct for holding XYZ component
  89. /* Orientation sensors */
  90. struct {
  91. float roll; /**< Rotation around the longitudinal axis (the plane body, 'X
  92. axis'). Roll is positive and increasing when moving
  93. downward. -90 degrees <= roll <= 90 degrees */
  94. float pitch; /**< Rotation around the lateral axis (the wing span, 'Y
  95. axis'). Pitch is positive and increasing when moving
  96. upwards. -180 degrees <= pitch <= 180 degrees) */
  97. float heading; /**< Angle between the longitudinal axis (the plane body)
  98. and magnetic north, measured clockwise when viewing from
  99. the top of the device. 0-359 degrees */
  100. }; ///< Struct for holding roll/pitch/heading
  101. }; ///< Union that can hold 3D vector array, XYZ components or
  102. ///< roll/pitch/heading
  103. int8_t status; ///< Status byte
  104. uint8_t reserved[3]; ///< Reserved
  105. } sensors_vec_t;
  106. /** struct sensors_color_s is used to return color data in a common format. */
  107. typedef struct {
  108. union {
  109. float c[3]; ///< Raw 3-element data
  110. /* RGB color space */
  111. struct {
  112. float r; /**< Red component */
  113. float g; /**< Green component */
  114. float b; /**< Blue component */
  115. }; ///< RGB data in floating point notation
  116. }; ///< Union of various ways to describe RGB colorspace
  117. uint32_t rgba; /**< 24-bit RGBA value */
  118. } sensors_color_t;
  119. /* Sensor event (36 bytes) */
  120. /** struct sensor_event_s is used to provide a single sensor event in a common
  121. * format. */
  122. typedef struct {
  123. int32_t version; /**< must be sizeof(struct sensors_event_t) */
  124. int32_t sensor_id; /**< unique sensor identifier */
  125. int32_t type; /**< sensor type */
  126. int32_t reserved0; /**< reserved */
  127. int32_t timestamp; /**< time is in milliseconds */
  128. union {
  129. float data[4]; ///< Raw data */
  130. sensors_vec_t acceleration; /**< acceleration values are in meter per second
  131. per second (m/s^2) */
  132. sensors_vec_t
  133. magnetic; /**< magnetic vector values are in micro-Tesla (uT) */
  134. sensors_vec_t orientation; /**< orientation values are in degrees */
  135. sensors_vec_t gyro; /**< gyroscope values are in rad/s */
  136. float temperature; /**< temperature is in degrees centigrade (Celsius) */
  137. float distance; /**< distance in centimeters */
  138. float light; /**< light in SI lux units */
  139. float pressure; /**< pressure in hectopascal (hPa) */
  140. float relative_humidity; /**< relative humidity in percent */
  141. float current; /**< current in milliamps (mA) */
  142. float voltage; /**< voltage in volts (V) */
  143. float tvoc; /**< Total Volatile Organic Compounds, in ppb */
  144. float voc_index; /**< VOC (Volatile Organic Compound) index where 100 is
  145. normal (unitless) */
  146. float nox_index; /**< NOx (Nitrogen Oxides) index where 1 is normal
  147. (unitless) */
  148. float CO2; /**< Measured CO2 in parts per million (ppm) */
  149. float eCO2; /**< equivalent/estimated CO2 in parts per million (ppm
  150. estimated from some other measurement) */
  151. float pm10_std; /**< Standard Particulate Matter <=1.0 in parts per million
  152. (ppm) */
  153. float pm25_std; /**< Standard Particulate Matter <=2.5 in parts per million
  154. (ppm) */
  155. float pm100_std; /**< Standard Particulate Matter <=10.0 in parts per
  156. million (ppm) */
  157. float pm10_env; /**< Environmental Particulate Matter <=1.0 in parts per
  158. million (ppm) */
  159. float pm25_env; /**< Environmental Particulate Matter <=2.5 in parts per
  160. million (ppm) */
  161. float pm100_env; /**< Environmental Particulate Matter <=10.0 in parts per
  162. million (ppm) */
  163. float gas_resistance; /**< Proportional to the amount of VOC particles in
  164. the air (Ohms) */
  165. float unitless_percent; /**<Percentage, unit-less (%) */
  166. sensors_color_t color; /**< color in RGB component values */
  167. float altitude; /**< Distance between a reference datum and a point or
  168. object, in meters. */
  169. }; ///< Union for the wide ranges of data we can carry
  170. } sensors_event_t;
  171. /* Sensor details (40 bytes) */
  172. /** struct sensor_s is used to describe basic information about a specific
  173. * sensor. */
  174. typedef struct {
  175. char name[12]; /**< sensor name */
  176. int32_t version; /**< version of the hardware + driver */
  177. int32_t sensor_id; /**< unique sensor identifier */
  178. int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
  179. float max_value; /**< maximum value of this sensor's value in SI units */
  180. float min_value; /**< minimum value of this sensor's value in SI units */
  181. float resolution; /**< smallest difference between two values reported by this
  182. sensor */
  183. int32_t min_delay; /**< min delay in microseconds between events. zero = not a
  184. constant rate */
  185. } sensor_t;
  186. /** @brief Common sensor interface to unify various sensors.
  187. * Intentionally modeled after sensors.h in the Android API:
  188. * https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h
  189. */
  190. class Adafruit_Sensor {
  191. public:
  192. // Constructor(s)
  193. Adafruit_Sensor() {}
  194. virtual ~Adafruit_Sensor() {}
  195. // These must be defined by the subclass
  196. /*! @brief Whether we should automatically change the range (if possible) for
  197. higher precision
  198. @param enabled True if we will try to autorange */
  199. virtual void enableAutoRange(bool enabled) {
  200. (void)enabled; /* suppress unused warning */
  201. };
  202. /*! @brief Get the latest sensor event
  203. @returns True if able to fetch an event */
  204. virtual bool getEvent(sensors_event_t *) = 0;
  205. /*! @brief Get info about the sensor itself */
  206. virtual void getSensor(sensor_t *) = 0;
  207. void printSensorDetails(void);
  208. };
  209. #endif