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 */ 45typedefenum 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. */ 65typedefstruct{ 66union{ 67float v[3]; 68struct{ 69float x; 70float y; 71float z; 72}; 73/* Orientation sensors */ 74struct{ 75float roll;/**< Rotation around the longitudinal axis (the plane body, 'X axis'). Roll is positive and increasing when moving downward. -90°<=roll<=90° */ 76float pitch;/**< Rotation around the lateral axis (the wing span, 'Y axis'). Pitch is positive and increasing when moving upwards. -180°<=pitch<=180°) */ 77float 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}; 80int8_t status; 81uint8_t reserved[3]; 82} sensors_vec_t; 83 84/** struct sensors_color_s is used to return color data in a common format. */ 85typedefstruct{ 86union{ 87float c[3]; 88/* RGB color space */ 89struct{ 90float r;/**< Red component */ 91float g;/**< Green component */ 92float b;/**< Blue component */ 93}; 94}; 95uint32_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. */ 100typedefstruct 101{ 102int32_t version;/**< must be sizeof(struct sensors_event_t) */ 103int32_t sensor_id;/**< unique sensor identifier */ 104int32_t type;/**< sensor type */ 105int32_t reserved0;/**< reserved */ 106int32_t timestamp;/**< time is in milliseconds */ 107union 108{ 109float 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 */ 114float temperature;/**< temperature is in degrees centigrade (Celsius) */ 115float distance;/**< distance in centimeters */ 116float light;/**< light in SI lux units */ 117float pressure;/**< pressure in hectopascal (hPa) */ 118float relative_humidity;/**< relative humidity in percent */ 119float current;/**< current in milliamps (mA) */ 120float 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. */ 127typedefstruct 128{ 129char name[12];/**< sensor name */ 130int32_t version;/**< version of the hardware + driver */ 131int32_t sensor_id;/**< unique sensor identifier */ 132int32_t type;/**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */ 133float max_value;/**< maximum value of this sensor's value in SI units */ 134float min_value;/**< minimum value of this sensor's value in SI units */ 135float resolution;/**< smallest difference between two values reported by this sensor */ 136int32_t min_delay;/**< min delay in microseconds between events. zero = not a constant rate */ 137} sensor_t; 138 139class Adafruit_Sensor { 140public: 141// Constructor(s) 142Adafruit_Sensor() {} 143virtual~Adafruit_Sensor() {} 144 145// These must be defined by the subclass 146virtualvoidenableAutoRange(bool enabled) {}; 147virtualboolgetEvent(sensors_event_t*) =0; 148virtualvoidgetSensor(sensor_t*) =0; 149 150private: 151bool _autoRange; 152}; 153 154#endif