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