Converted to C++ and esp-idf

This commit is contained in:
2021-07-13 12:53:36 +02:00
parent 6cea0d9c55
commit 08c70104ef
3 changed files with 63 additions and 23 deletions

View File

@ -1,5 +1,7 @@
#include "Adafruit_Sensor.h"
#undef ARDUINO
#ifdef ARDUINO
/**************************************************************************/
/*!
@brief Prints sensor information to serial console
@ -76,3 +78,4 @@ void Adafruit_Sensor::printSensorDetails(void) {
Serial.println(sensor.resolution);
Serial.println(F("------------------------------------\n"));
}
#endif

View File

@ -20,14 +20,17 @@
#ifndef _ADAFRUIT_SENSOR_H
#define _ADAFRUIT_SENSOR_H
#ifndef ARDUINO
//#ifndef ARDUINO
#include <stdint.h>
#elif ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
//#elif ARDUINO >= 100
//#include "Arduino.h"
//#include "Print.h"
//#else
//#include "WProgram.h"
//#endif
#include <optional>
#include <espchrono.h>
/* Constants */
#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
@ -49,7 +52,7 @@
(100) /**< Gauss to micro-Tesla multiplier */
/** Sensor types */
typedef enum {
enum sensors_type_t {
SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
SENSOR_TYPE_MAGNETIC_FIELD = (2),
SENSOR_TYPE_ORIENTATION = (3),
@ -67,10 +70,10 @@ typedef enum {
SENSOR_TYPE_VOLTAGE = (15),
SENSOR_TYPE_CURRENT = (16),
SENSOR_TYPE_COLOR = (17)
} sensors_type_t;
};
/** struct sensors_vec_s is used to return a vector in a common format. */
typedef struct {
struct sensors_vec_t {
union {
float v[3]; ///< 3D vector elements
struct {
@ -94,10 +97,10 @@ typedef struct {
///< roll/pitch/heading
int8_t status; ///< Status byte
uint8_t reserved[3]; ///< Reserved
} sensors_vec_t;
};
/** struct sensors_color_s is used to return color data in a common format. */
typedef struct {
struct sensors_color_t {
union {
float c[3]; ///< Raw 3-element data
/* RGB color space */
@ -108,17 +111,17 @@ typedef struct {
}; ///< RGB data in floating point notation
}; ///< Union of various ways to describe RGB colorspace
uint32_t rgba; /**< 24-bit RGBA value */
} sensors_color_t;
};
/* Sensor event (36 bytes) */
/** struct sensor_event_s is used to provide a single sensor event in a common
* format. */
typedef struct {
struct sensors_event_t {
int32_t version; /**< must be sizeof(struct sensors_event_t) */
int32_t sensor_id; /**< unique sensor identifier */
int32_t type; /**< sensor type */
int32_t reserved0; /**< reserved */
int32_t timestamp; /**< time is in milliseconds */
espchrono::millis_clock::time_point timestamp; /**< time is in milliseconds */
union {
float data[4]; ///< Raw data
sensors_vec_t acceleration; /**< acceleration values are in meter per second
@ -136,12 +139,12 @@ typedef struct {
float voltage; /**< voltage in volts (V) */
sensors_color_t color; /**< color in RGB component values */
}; ///< Union for the wide ranges of data we can carry
} sensors_event_t;
};
/* Sensor details (40 bytes) */
/** struct sensor_s is used to describe basic information about a specific
* sensor. */
typedef struct {
struct sensor_t {
char name[12]; /**< sensor name */
int32_t version; /**< version of the hardware + driver */
int32_t sensor_id; /**< unique sensor identifier */
@ -152,7 +155,7 @@ typedef struct {
sensor */
int32_t min_delay; /**< min delay in microseconds between events. zero = not a
constant rate */
} sensor_t;
};
/** @brief Common sensor interface to unify various sensors.
* Intentionally modeled after sensors.h in the Android API:
@ -161,8 +164,8 @@ typedef struct {
class Adafruit_Sensor {
public:
// Constructor(s)
Adafruit_Sensor() {}
virtual ~Adafruit_Sensor() {}
Adafruit_Sensor() = default;
virtual ~Adafruit_Sensor() = default;
// These must be defined by the subclass
@ -175,11 +178,13 @@ public:
/*! @brief Get the latest sensor 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 */
virtual void getSensor(sensor_t *) = 0;
virtual sensor_t getSensor() = 0;
void printSensorDetails(void);
//#ifdef ARDUINO
//void printSensorDetails(void);
//#endif
private:
bool _autoRange;

32
CMakeLists.txt Normal file
View 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
)