Adafruit Unified Sensor Library
Adafruit_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 #ifndef ARDUINO
24 #include <stdint.h>
25 #elif ARDUINO >= 100
26 #include "Arduino.h"
27 #include "Print.h"
28 #else
29 #include "WProgram.h"
30 #endif
31 
32 /* Constants */
33 #define SENSORS_GRAVITY_EARTH (9.80665F)
34 #define SENSORS_GRAVITY_MOON (1.6F)
35 #define SENSORS_GRAVITY_SUN (275.0F)
36 #define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
37 #define SENSORS_MAGFIELD_EARTH_MAX \
38  (60.0F)
39 #define SENSORS_MAGFIELD_EARTH_MIN \
40  (30.0F)
41 #define SENSORS_PRESSURE_SEALEVELHPA \
42  (1013.25F)
43 #define SENSORS_DPS_TO_RADS \
44  (0.017453293F)
46 #define SENSORS_GAUSS_TO_MICROTESLA \
47  (100)
50 typedef enum {
51  SENSOR_TYPE_ACCELEROMETER = (1),
52  SENSOR_TYPE_MAGNETIC_FIELD = (2),
53  SENSOR_TYPE_ORIENTATION = (3),
54  SENSOR_TYPE_GYROSCOPE = (4),
55  SENSOR_TYPE_LIGHT = (5),
56  SENSOR_TYPE_PRESSURE = (6),
57  SENSOR_TYPE_PROXIMITY = (8),
58  SENSOR_TYPE_GRAVITY = (9),
59  SENSOR_TYPE_LINEAR_ACCELERATION =
60  (10),
61  SENSOR_TYPE_ROTATION_VECTOR = (11),
62  SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
63  SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
64  SENSOR_TYPE_OBJECT_TEMPERATURE = (14),
65  SENSOR_TYPE_VOLTAGE = (15),
66  SENSOR_TYPE_CURRENT = (16),
67  SENSOR_TYPE_COLOR = (17)
68 } sensors_type_t;
69 
71 typedef struct {
72  union {
73  float v[3];
74  struct {
75  float x;
76  float y;
77  float z;
78  };
79  /* Orientation sensors */
80  struct {
81  float roll;
84  float pitch;
87  float heading;
90  };
91  };
92  int8_t status;
94  uint8_t reserved[3];
96 
98 typedef struct {
99  union {
100  float c[3];
101  /* RGB color space */
102  struct {
103  float r;
104  float g;
105  float b;
106  };
107  };
108  uint32_t rgba;
110 
111 /* Sensor event (36 bytes) */
114 typedef struct {
115  int32_t version;
116  int32_t sensor_id;
117  int32_t type;
118  int32_t reserved0;
119  int32_t timestamp;
120  union {
121  float data[4];
122  sensors_vec_t acceleration;
125  magnetic;
126  sensors_vec_t orientation;
128  float temperature;
129  float distance;
130  float light;
131  float pressure;
132  float relative_humidity;
133  float current;
134  float voltage;
136  };
138 
139 /* Sensor details (40 bytes) */
142 typedef struct {
143  char name[12];
144  int32_t version;
145  int32_t sensor_id;
146  int32_t type;
147  float max_value;
148  float min_value;
149  float resolution;
151  int32_t min_delay;
153 } sensor_t;
154 
159 class Adafruit_Sensor {
160 public:
161  // Constructor(s)
162  Adafruit_Sensor() {}
163  virtual ~Adafruit_Sensor() {}
164 
165  // These must be defined by the subclass
166 
170  virtual void enableAutoRange(bool enabled) {
171  (void)enabled; /* suppress unused warning */
172  };
173 
176  virtual bool getEvent(sensors_event_t *) = 0;
178  virtual void getSensor(sensor_t *) = 0;
179 
180  void printSensorDetails(void);
181 
182 private:
183  bool _autoRange;
184 };
185 
186 #endif
Definition: Adafruit_Sensor.h:72
Common sensor interface to unify various sensors. Intentionally modeled after sensors.h in the Android API: https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h.
Definition: Adafruit_Sensor.h:160
virtual bool getEvent(sensors_event_t *)=0
Get the latest sensor event.
void printSensorDetails(void)
Prints sensor information to serial console.
Definition: Adafruit_Sensor.cpp:8
Definition: Adafruit_Sensor.h:143
Definition: Adafruit_Sensor.h:115
virtual void getSensor(sensor_t *)=0
Get info about the sensor itself.
Definition: Adafruit_Sensor.h:99
virtual void enableAutoRange(bool enabled)
Whether we should automatically change the range (if possible) for higher precision.
Definition: Adafruit_Sensor.h:171