Port of Adafruit Arduino code

Dependencies:   mbed

Committer:
bmanga95
Date:
Sat Mar 21 12:33:05 2015 +0000
Revision:
0:772bf4786416
First version

Who changed what in which revision?

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