Adafruit driver converted to Mbed OS 6.x.

Dependents:   Adafruit-BNO055-test

Committer:
MACRUM
Date:
Tue Mar 16 05:41:43 2021 +0000
Revision:
3:7db662f5d402
Parent:
0:22c544c8741a
Initial commit

Who changed what in which revision?

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