Adafruit Library
Adafruit_CPlay_Sensor.h
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)
35 #define SENSORS_GRAVITY_MOON (1.6F)
36 #define SENSORS_GRAVITY_SUN (275.0F)
37 #define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
38 #define SENSORS_MAGFIELD_EARTH_MAX (60.0F)
39 #define SENSORS_MAGFIELD_EARTH_MIN (30.0F)
40 #define SENSORS_PRESSURE_SEALEVELHPA (1013.25F)
41 #define SENSORS_DPS_TO_RADS (0.017453293F)
42 #define SENSORS_GAUSS_TO_MICROTESLA (100)
43 
44 
45 typedef enum
46 {
47  SENSOR_TYPE_ACCELEROMETER = (1),
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),
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 
66 typedef struct {
67  union {
68  float v[3];
69  struct {
70  float x;
71  float y;
72  float z;
73  };
74  struct {
75  float roll;
76  float pitch;
77  float heading;
78  };
79  };
80  int8_t status;
81  uint8_t reserved[3];
83 
85 typedef struct {
86  union {
87  float c[3];
88  struct {
89  float r;
90  float g;
91  float b;
92  };
93  };
94  uint32_t rgba;
96 
97 /* Sensor event (36 bytes) */
99 typedef struct
100 {
101  int32_t version;
102  int32_t sensor_id;
103  int32_t type;
104  int32_t reserved0;
105  int32_t timestamp;
106  union
107  {
108  float data[4];
113  float temperature;
114  float distance;
115  float light;
116  float pressure;
118  float current;
119  float voltage;
121  };
123 
124 /* Sensor details (40 bytes) */
126 typedef struct
127 {
128  char name[12];
129  int32_t version;
130  int32_t sensor_id;
131  int32_t type;
132  float max_value;
133  float min_value;
134  float resolution;
135  int32_t min_delay;
136 } sensor_t;
137 
138 /**************************************************************************/
142 /**************************************************************************/
144  public:
145  // Constructor(s)
146  Adafruit_Sensor() {}
147  virtual ~Adafruit_Sensor() {}
148 
149  // These must be defined by the subclass
150 
151 /**************************************************************************/
156 /**************************************************************************/
157  virtual void enableAutoRange(bool enabled) {};
158 
159 /**************************************************************************/
164 /**************************************************************************/
165  virtual bool getEvent(sensors_event_t*) = 0;
166 
167 /**************************************************************************/
171 /**************************************************************************/
172  virtual void getSensor(sensor_t*) = 0;
173 
174  private:
175  bool _autoRange;
176 };
177 
178 #endif
sensors_vec_t orientation
orientation values are in degrees */
Definition: Adafruit_CPlay_Sensor.h:111
Definition: Adafruit_CPlay_Sensor.h:66
Sensor API class for CircuitPlayground board.
Definition: Adafruit_CPlay_Sensor.h:143
float b
Blue component */.
Definition: Adafruit_CPlay_Sensor.h:91
float x
x-axis G data
Definition: Adafruit_CPlay_Sensor.h:70
float light
light in SI lux units */
Definition: Adafruit_CPlay_Sensor.h:115
virtual bool getEvent(sensors_event_t *)=0
enable auto-ranging for the sensor
float r
Red component */.
Definition: Adafruit_CPlay_Sensor.h:89
float g
Green component */.
Definition: Adafruit_CPlay_Sensor.h:90
Definition: Adafruit_CPlay_Sensor.h:126
float voltage
voltage in volts (V) */
Definition: Adafruit_CPlay_Sensor.h:119
int32_t version
version of the hardware + driver */
Definition: Adafruit_CPlay_Sensor.h:129
float pressure
pressure in hectopascal (hPa) */
Definition: Adafruit_CPlay_Sensor.h:116
int32_t type
this sensor&#39;s type (ex. SENSOR_TYPE_LIGHT) */
Definition: Adafruit_CPlay_Sensor.h:131
float distance
distance in centimeters */
Definition: Adafruit_CPlay_Sensor.h:114
sensors_vec_t acceleration
acceleration values are in meter per second per second (m/s^2) */
Definition: Adafruit_CPlay_Sensor.h:109
sensors_vec_t magnetic
magnetic vector values are in micro-Tesla (uT) */
Definition: Adafruit_CPlay_Sensor.h:110
int32_t sensor_id
unique sensor identifier */
Definition: Adafruit_CPlay_Sensor.h:130
float min_value
minimum value of this sensor&#39;s value in SI units */
Definition: Adafruit_CPlay_Sensor.h:133
int8_t status
sensor status
Definition: Adafruit_CPlay_Sensor.h:80
uint32_t rgba
24-bit RGBA value */
Definition: Adafruit_CPlay_Sensor.h:94
int32_t sensor_id
unique sensor identifier */
Definition: Adafruit_CPlay_Sensor.h:102
int32_t timestamp
time is in milliseconds */
Definition: Adafruit_CPlay_Sensor.h:105
Definition: Adafruit_CPlay_Sensor.h:99
virtual void getSensor(sensor_t *)=0
get information on the sensor
int32_t reserved0
reserved */
Definition: Adafruit_CPlay_Sensor.h:104
float current
current in milliamps (mA) */
Definition: Adafruit_CPlay_Sensor.h:118
Definition: Adafruit_CPlay_Sensor.h:85
sensors_color_t color
color in RGB component values */
Definition: Adafruit_CPlay_Sensor.h:120
float roll
Rotation around the longitudinal axis (the plane body, &#39;X axis&#39;). Roll is positive and increasing whe...
Definition: Adafruit_CPlay_Sensor.h:75
float z
z-axis G data
Definition: Adafruit_CPlay_Sensor.h:72
float heading
Angle between the longitudinal axis (the plane body) and magnetic north, measured clockwise when view...
Definition: Adafruit_CPlay_Sensor.h:77
float y
y-axis G data
Definition: Adafruit_CPlay_Sensor.h:71
float relative_humidity
relative humidity in percent */
Definition: Adafruit_CPlay_Sensor.h:117
float temperature
temperature is in degrees centigrade (Celsius) */
Definition: Adafruit_CPlay_Sensor.h:113
virtual void enableAutoRange(bool enabled)
enable or disable auto-ranging for the sensor
Definition: Adafruit_CPlay_Sensor.h:157
int32_t version
must be sizeof(struct sensors_event_t) */
Definition: Adafruit_CPlay_Sensor.h:101
sensors_vec_t gyro
gyroscope values are in rad/s */
Definition: Adafruit_CPlay_Sensor.h:112
float max_value
maximum value of this sensor&#39;s value in SI units */
Definition: Adafruit_CPlay_Sensor.h:132
int32_t type
sensor type */
Definition: Adafruit_CPlay_Sensor.h:103
float resolution
smallest difference between two values reported by this sensor */
Definition: Adafruit_CPlay_Sensor.h:134
int32_t min_delay
min delay in microseconds between events. zero = not a constant rate */
Definition: Adafruit_CPlay_Sensor.h:135
float pitch
Rotation around the lateral axis (the wing span, &#39;Y axis&#39;). Pitch is positive and increasing when mov...
Definition: Adafruit_CPlay_Sensor.h:76