forked from adafruit/Adafruit_Sensor
Converted to C++ and esp-idf
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
#include "Adafruit_Sensor.h"
|
#include "Adafruit_Sensor.h"
|
||||||
|
|
||||||
|
#undef ARDUINO
|
||||||
|
#ifdef ARDUINO
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Prints sensor information to serial console
|
@brief Prints sensor information to serial console
|
||||||
@ -76,3 +78,4 @@ void Adafruit_Sensor::printSensorDetails(void) {
|
|||||||
Serial.println(sensor.resolution);
|
Serial.println(sensor.resolution);
|
||||||
Serial.println(F("------------------------------------\n"));
|
Serial.println(F("------------------------------------\n"));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
@ -20,14 +20,17 @@
|
|||||||
#ifndef _ADAFRUIT_SENSOR_H
|
#ifndef _ADAFRUIT_SENSOR_H
|
||||||
#define _ADAFRUIT_SENSOR_H
|
#define _ADAFRUIT_SENSOR_H
|
||||||
|
|
||||||
#ifndef ARDUINO
|
//#ifndef ARDUINO
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#elif ARDUINO >= 100
|
//#elif ARDUINO >= 100
|
||||||
#include "Arduino.h"
|
//#include "Arduino.h"
|
||||||
#include "Print.h"
|
//#include "Print.h"
|
||||||
#else
|
//#else
|
||||||
#include "WProgram.h"
|
//#include "WProgram.h"
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <espchrono.h>
|
||||||
|
|
||||||
/* Constants */
|
/* Constants */
|
||||||
#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
|
#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
|
||||||
@ -49,7 +52,7 @@
|
|||||||
(100) /**< Gauss to micro-Tesla multiplier */
|
(100) /**< Gauss to micro-Tesla multiplier */
|
||||||
|
|
||||||
/** Sensor types */
|
/** Sensor types */
|
||||||
typedef enum {
|
enum sensors_type_t {
|
||||||
SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
|
SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
|
||||||
SENSOR_TYPE_MAGNETIC_FIELD = (2),
|
SENSOR_TYPE_MAGNETIC_FIELD = (2),
|
||||||
SENSOR_TYPE_ORIENTATION = (3),
|
SENSOR_TYPE_ORIENTATION = (3),
|
||||||
@ -67,10 +70,10 @@ typedef enum {
|
|||||||
SENSOR_TYPE_VOLTAGE = (15),
|
SENSOR_TYPE_VOLTAGE = (15),
|
||||||
SENSOR_TYPE_CURRENT = (16),
|
SENSOR_TYPE_CURRENT = (16),
|
||||||
SENSOR_TYPE_COLOR = (17)
|
SENSOR_TYPE_COLOR = (17)
|
||||||
} sensors_type_t;
|
};
|
||||||
|
|
||||||
/** struct sensors_vec_s is used to return a vector in a common format. */
|
/** struct sensors_vec_s is used to return a vector in a common format. */
|
||||||
typedef struct {
|
struct sensors_vec_t {
|
||||||
union {
|
union {
|
||||||
float v[3]; ///< 3D vector elements
|
float v[3]; ///< 3D vector elements
|
||||||
struct {
|
struct {
|
||||||
@ -94,10 +97,10 @@ typedef struct {
|
|||||||
///< roll/pitch/heading
|
///< roll/pitch/heading
|
||||||
int8_t status; ///< Status byte
|
int8_t status; ///< Status byte
|
||||||
uint8_t reserved[3]; ///< Reserved
|
uint8_t reserved[3]; ///< Reserved
|
||||||
} sensors_vec_t;
|
};
|
||||||
|
|
||||||
/** struct sensors_color_s is used to return color data in a common format. */
|
/** struct sensors_color_s is used to return color data in a common format. */
|
||||||
typedef struct {
|
struct sensors_color_t {
|
||||||
union {
|
union {
|
||||||
float c[3]; ///< Raw 3-element data
|
float c[3]; ///< Raw 3-element data
|
||||||
/* RGB color space */
|
/* RGB color space */
|
||||||
@ -108,17 +111,17 @@ typedef struct {
|
|||||||
}; ///< RGB data in floating point notation
|
}; ///< RGB data in floating point notation
|
||||||
}; ///< Union of various ways to describe RGB colorspace
|
}; ///< Union of various ways to describe RGB colorspace
|
||||||
uint32_t rgba; /**< 24-bit RGBA value */
|
uint32_t rgba; /**< 24-bit RGBA value */
|
||||||
} sensors_color_t;
|
};
|
||||||
|
|
||||||
/* Sensor event (36 bytes) */
|
/* Sensor event (36 bytes) */
|
||||||
/** struct sensor_event_s is used to provide a single sensor event in a common
|
/** struct sensor_event_s is used to provide a single sensor event in a common
|
||||||
* format. */
|
* format. */
|
||||||
typedef struct {
|
struct sensors_event_t {
|
||||||
int32_t version; /**< must be sizeof(struct sensors_event_t) */
|
int32_t version; /**< must be sizeof(struct sensors_event_t) */
|
||||||
int32_t sensor_id; /**< unique sensor identifier */
|
int32_t sensor_id; /**< unique sensor identifier */
|
||||||
int32_t type; /**< sensor type */
|
int32_t type; /**< sensor type */
|
||||||
int32_t reserved0; /**< reserved */
|
int32_t reserved0; /**< reserved */
|
||||||
int32_t timestamp; /**< time is in milliseconds */
|
espchrono::millis_clock::time_point timestamp; /**< time is in milliseconds */
|
||||||
union {
|
union {
|
||||||
float data[4]; ///< Raw data
|
float data[4]; ///< Raw data
|
||||||
sensors_vec_t acceleration; /**< acceleration values are in meter per second
|
sensors_vec_t acceleration; /**< acceleration values are in meter per second
|
||||||
@ -136,12 +139,12 @@ typedef struct {
|
|||||||
float voltage; /**< voltage in volts (V) */
|
float voltage; /**< voltage in volts (V) */
|
||||||
sensors_color_t color; /**< color in RGB component values */
|
sensors_color_t color; /**< color in RGB component values */
|
||||||
}; ///< Union for the wide ranges of data we can carry
|
}; ///< Union for the wide ranges of data we can carry
|
||||||
} sensors_event_t;
|
};
|
||||||
|
|
||||||
/* Sensor details (40 bytes) */
|
/* Sensor details (40 bytes) */
|
||||||
/** struct sensor_s is used to describe basic information about a specific
|
/** struct sensor_s is used to describe basic information about a specific
|
||||||
* sensor. */
|
* sensor. */
|
||||||
typedef struct {
|
struct sensor_t {
|
||||||
char name[12]; /**< sensor name */
|
char name[12]; /**< sensor name */
|
||||||
int32_t version; /**< version of the hardware + driver */
|
int32_t version; /**< version of the hardware + driver */
|
||||||
int32_t sensor_id; /**< unique sensor identifier */
|
int32_t sensor_id; /**< unique sensor identifier */
|
||||||
@ -152,7 +155,7 @@ typedef struct {
|
|||||||
sensor */
|
sensor */
|
||||||
int32_t min_delay; /**< min delay in microseconds between events. zero = not a
|
int32_t min_delay; /**< min delay in microseconds between events. zero = not a
|
||||||
constant rate */
|
constant rate */
|
||||||
} sensor_t;
|
};
|
||||||
|
|
||||||
/** @brief Common sensor interface to unify various sensors.
|
/** @brief Common sensor interface to unify various sensors.
|
||||||
* Intentionally modeled after sensors.h in the Android API:
|
* Intentionally modeled after sensors.h in the Android API:
|
||||||
@ -161,8 +164,8 @@ typedef struct {
|
|||||||
class Adafruit_Sensor {
|
class Adafruit_Sensor {
|
||||||
public:
|
public:
|
||||||
// Constructor(s)
|
// Constructor(s)
|
||||||
Adafruit_Sensor() {}
|
Adafruit_Sensor() = default;
|
||||||
virtual ~Adafruit_Sensor() {}
|
virtual ~Adafruit_Sensor() = default;
|
||||||
|
|
||||||
// These must be defined by the subclass
|
// These must be defined by the subclass
|
||||||
|
|
||||||
@ -175,11 +178,13 @@ public:
|
|||||||
|
|
||||||
/*! @brief Get the latest sensor event
|
/*! @brief Get the latest sensor event
|
||||||
@returns True if able to fetch an event */
|
@returns True if able to fetch an event */
|
||||||
virtual bool getEvent(sensors_event_t *) = 0;
|
virtual std::optional<sensors_event_t> getEvent() = 0;
|
||||||
/*! @brief Get info about the sensor itself */
|
/*! @brief Get info about the sensor itself */
|
||||||
virtual void getSensor(sensor_t *) = 0;
|
virtual sensor_t getSensor() = 0;
|
||||||
|
|
||||||
void printSensorDetails(void);
|
//#ifdef ARDUINO
|
||||||
|
//void printSensorDetails(void);
|
||||||
|
//#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _autoRange;
|
bool _autoRange;
|
||||||
|
32
CMakeLists.txt
Normal file
32
CMakeLists.txt
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
set(headers
|
||||||
|
Adafruit_Sensor.h
|
||||||
|
)
|
||||||
|
|
||||||
|
set(sources
|
||||||
|
Adafruit_Sensor.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set(dependencies
|
||||||
|
arduino-esp32
|
||||||
|
espchrono
|
||||||
|
)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
INCLUDE_DIRS
|
||||||
|
.
|
||||||
|
SRCS
|
||||||
|
${headers}
|
||||||
|
${sources}
|
||||||
|
REQUIRES
|
||||||
|
${dependencies}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_options(${COMPONENT_TARGET}
|
||||||
|
PRIVATE
|
||||||
|
-fstack-reuse=all
|
||||||
|
-fstack-protector-all
|
||||||
|
-Wno-unused-function
|
||||||
|
-Wno-deprecated-declarations
|
||||||
|
-Wno-missing-field-initializers
|
||||||
|
-Wno-parentheses
|
||||||
|
)
|
Reference in New Issue
Block a user