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