Adafruit_Sensor.h 7.5 KB

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