Adafruit_Sensor.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if ARDUINO >= 100
  21. #include "Arduino.h"
  22. #include "Print.h"
  23. #else
  24. #include "WProgram.h"
  25. #endif
  26. /* Intentionally modeled after sensors.h in the Android API:
  27. * https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */
  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 (60.0F) /**< Maximum magnetic field on Earth's surface */
  34. #define SENSORS_MAGFIELD_EARTH_MIN (30.0F) /**< Minimum magnetic field on Earth's surface */
  35. #define SENSORS_PRESSURE_SEALEVELHPA (1013.25F) /**< Average sea level pressure is 1013.25 hPa */
  36. #define SENSORS_DPS_TO_RADS (0.017453293F) /**< Degrees/s to rad/s multiplier */
  37. #define SENSORS_GAUSS_TO_MICROTESLA (100) /**< Gauss to micro-Tesla multiplier */
  38. /** Sensor types */
  39. typedef enum
  40. {
  41. SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
  42. SENSOR_TYPE_MAGNETIC_FIELD = (2),
  43. SENSOR_TYPE_ORIENTATION = (3),
  44. SENSOR_TYPE_GYROSCOPE = (4),
  45. SENSOR_TYPE_LIGHT = (5),
  46. SENSOR_TYPE_PRESSURE = (6),
  47. SENSOR_TYPE_PROXIMITY = (8),
  48. SENSOR_TYPE_GRAVITY = (9),
  49. SENSOR_TYPE_LINEAR_ACCELERATION = (10), /**< Acceleration not including gravity */
  50. SENSOR_TYPE_ROTATION_VECTOR = (11),
  51. SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
  52. SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
  53. SENSOR_TYPE_VOLTAGE = (15),
  54. SENSOR_TYPE_CURRENT = (16),
  55. SENSOR_TYPE_COLOR = (17)
  56. } sensors_type_t;
  57. /** struct sensors_vec_s is used to return a vector in a common format. */
  58. typedef struct {
  59. union {
  60. float v[3];
  61. struct {
  62. float x;
  63. float y;
  64. float z;
  65. };
  66. /* Orientation sensors */
  67. struct {
  68. float roll; /**< Rotation around the longitudinal axis (the plane body, 'X axis'). Roll is positive and increasing when moving downward. -90°<=roll<=90° */
  69. float pitch; /**< Rotation around the lateral axis (the wing span, 'Y axis'). Pitch is positive and increasing when moving upwards. -180°<=pitch<=180°) */
  70. float heading; /**< Angle between the longitudinal axis (the plane body) and magnetic north, measured clockwise when viewing from the top of the device. 0-359° */
  71. };
  72. };
  73. int8_t status;
  74. uint8_t reserved[3];
  75. } sensors_vec_t;
  76. /** struct sensors_color_s is used to return color data in a common format. */
  77. typedef struct {
  78. union {
  79. float c[3];
  80. /* RGB color space */
  81. struct {
  82. float r; /**< Red component */
  83. float g; /**< Green component */
  84. float b; /**< Blue component */
  85. };
  86. };
  87. uint32_t rgba; /**< 24-bit RGBA value */
  88. } sensors_color_t;
  89. /* Sensor event (36 bytes) */
  90. /** struct sensor_event_s is used to provide a single sensor event in a common format. */
  91. typedef struct
  92. {
  93. int32_t version; /**< must be sizeof(struct sensors_event_t) */
  94. int32_t sensor_id; /**< unique sensor identifier */
  95. int32_t type; /**< sensor type */
  96. int32_t reserved0; /**< reserved */
  97. int32_t timestamp; /**< time is in milliseconds */
  98. union
  99. {
  100. float data[4];
  101. sensors_vec_t acceleration; /**< acceleration values are in meter per second per second (m/s^2) */
  102. sensors_vec_t magnetic; /**< magnetic vector values are in micro-Tesla (uT) */
  103. sensors_vec_t orientation; /**< orientation values are in degrees */
  104. sensors_vec_t gyro; /**< gyroscope values are in rad/s */
  105. float temperature; /**< temperature is in degrees centigrade (Celsius) */
  106. float distance; /**< distance in centimeters */
  107. float light; /**< light in SI lux units */
  108. float pressure; /**< pressure in hectopascal (hPa) */
  109. float relative_humidity; /**< relative humidity in percent */
  110. float current; /**< current in milliamps (mA) */
  111. float voltage; /**< voltage in volts (V) */
  112. sensors_color_t color; /**< color in RGB component values */
  113. };
  114. } sensors_event_t;
  115. /* Sensor details (40 bytes) */
  116. /** struct sensor_s is used to describe basic information about a specific sensor. */
  117. typedef struct
  118. {
  119. char name[12]; /**< sensor name */
  120. int32_t version; /**< version of the hardware + driver */
  121. int32_t sensor_id; /**< unique sensor identifier */
  122. int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
  123. float max_value; /**< maximum value of this sensor's value in SI units */
  124. float min_value; /**< minimum value of this sensor's value in SI units */
  125. float resolution; /**< smallest difference between two values reported by this sensor */
  126. int32_t min_delay; /**< min delay in microseconds between events. zero = not a constant rate */
  127. } sensor_t;
  128. class Adafruit_Sensor {
  129. public:
  130. // Constructor(s)
  131. Adafruit_Sensor() {}
  132. virtual ~Adafruit_Sensor() {}
  133. // These must be defined by the subclass
  134. virtual void enableAutoRange(bool enabled) {};
  135. virtual bool getEvent(sensors_event_t*) = 0;
  136. virtual void getSensor(sensor_t*) = 0;
  137. private:
  138. bool _autoRange;
  139. };
  140. #endif