Adafruit_Sensor.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. } sensors_type_t;
  69. /** struct sensors_vec_s is used to return a vector in a common format. */
  70. typedef struct {
  71. union {
  72. float v[3]; ///< 3D vector elements
  73. struct {
  74. float x; ///< X component of vector
  75. float y; ///< Y component of vector
  76. float z; ///< Z component of vector
  77. }; ///< Struct for holding XYZ component
  78. /* Orientation sensors */
  79. struct {
  80. float roll; /**< Rotation around the longitudinal axis (the plane body, 'X
  81. axis'). Roll is positive and increasing when moving
  82. downward. -90 degrees <= roll <= 90 degrees */
  83. float pitch; /**< Rotation around the lateral axis (the wing span, 'Y
  84. axis'). Pitch is positive and increasing when moving
  85. upwards. -180 degrees <= pitch <= 180 degrees) */
  86. float heading; /**< Angle between the longitudinal axis (the plane body)
  87. and magnetic north, measured clockwise when viewing from
  88. the top of the device. 0-359 degrees */
  89. }; ///< Struct for holding roll/pitch/heading
  90. }; ///< Union that can hold 3D vector array, XYZ components or
  91. ///< roll/pitch/heading
  92. int8_t status; ///< Status byte
  93. uint8_t reserved[3]; ///< Reserved
  94. } sensors_vec_t;
  95. /** struct sensors_color_s is used to return color data in a common format. */
  96. typedef struct {
  97. union {
  98. float c[3]; ///< Raw 3-element data
  99. /* RGB color space */
  100. struct {
  101. float r; /**< Red component */
  102. float g; /**< Green component */
  103. float b; /**< Blue component */
  104. }; ///< RGB data in floating point notation
  105. }; ///< Union of various ways to describe RGB colorspace
  106. uint32_t rgba; /**< 24-bit RGBA value */
  107. } sensors_color_t;
  108. /* Sensor event (36 bytes) */
  109. /** struct sensor_event_s is used to provide a single sensor event in a common
  110. * format. */
  111. typedef struct {
  112. int32_t version; /**< must be sizeof(struct sensors_event_t) */
  113. int32_t sensor_id; /**< unique sensor identifier */
  114. int32_t type; /**< sensor type */
  115. int32_t reserved0; /**< reserved */
  116. int32_t timestamp; /**< time is in milliseconds */
  117. union {
  118. float data[4]; ///< Raw data
  119. sensors_vec_t acceleration; /**< acceleration values are in meter per second
  120. per second (m/s^2) */
  121. sensors_vec_t
  122. magnetic; /**< magnetic vector values are in micro-Tesla (uT) */
  123. sensors_vec_t orientation; /**< orientation values are in degrees */
  124. sensors_vec_t gyro; /**< gyroscope values are in rad/s */
  125. float temperature; /**< temperature is in degrees centigrade (Celsius) */
  126. float distance; /**< distance in centimeters */
  127. float light; /**< light in SI lux units */
  128. float pressure; /**< pressure in hectopascal (hPa) */
  129. float relative_humidity; /**< relative humidity in percent */
  130. float current; /**< current in milliamps (mA) */
  131. float voltage; /**< voltage in volts (V) */
  132. float tvoc; /**< Total Volatile Organic Compounds, in ppb */
  133. float voc_index; /**< VOC (Volatile Organic Compound) index where 100 is
  134. normal (unitless) */
  135. float nox_index; /**< NOx (Nitrogen Oxides) index where 100 is normal
  136. (unitless) */
  137. sensors_color_t color; /**< color in RGB component values */
  138. }; ///< Union for the wide ranges of data we can carry
  139. } sensors_event_t;
  140. /* Sensor details (40 bytes) */
  141. /** struct sensor_s is used to describe basic information about a specific
  142. * sensor. */
  143. typedef struct {
  144. char name[12]; /**< sensor name */
  145. int32_t version; /**< version of the hardware + driver */
  146. int32_t sensor_id; /**< unique sensor identifier */
  147. int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
  148. float max_value; /**< maximum value of this sensor's value in SI units */
  149. float min_value; /**< minimum value of this sensor's value in SI units */
  150. float resolution; /**< smallest difference between two values reported by this
  151. sensor */
  152. int32_t min_delay; /**< min delay in microseconds between events. zero = not a
  153. constant rate */
  154. } sensor_t;
  155. /** @brief Common sensor interface to unify various sensors.
  156. * Intentionally modeled after sensors.h in the Android API:
  157. * https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h
  158. */
  159. class Adafruit_Sensor {
  160. public:
  161. // Constructor(s)
  162. Adafruit_Sensor() {}
  163. virtual ~Adafruit_Sensor() {}
  164. // These must be defined by the subclass
  165. /*! @brief Whether we should automatically change the range (if possible) for
  166. higher precision
  167. @param enabled True if we will try to autorange */
  168. virtual void enableAutoRange(bool enabled) {
  169. (void)enabled; /* suppress unused warning */
  170. };
  171. /*! @brief Get the latest sensor event
  172. @returns True if able to fetch an event */
  173. virtual bool getEvent(sensors_event_t *) = 0;
  174. /*! @brief Get info about the sensor itself */
  175. virtual void getSensor(sensor_t *) = 0;
  176. void printSensorDetails(void);
  177. private:
  178. bool _autoRange;
  179. };
  180. #endif