From 08c70104ef9d756b775faa95f25337574324354d Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 13 Jul 2021 12:53:36 +0200 Subject: [PATCH] Converted to C++ and esp-idf --- Adafruit_Sensor.cpp | 3 +++ Adafruit_Sensor.h | 51 +++++++++++++++++++++++++-------------------- CMakeLists.txt | 32 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 CMakeLists.txt diff --git a/Adafruit_Sensor.cpp b/Adafruit_Sensor.cpp index 2a4513e..d95109c 100644 --- a/Adafruit_Sensor.cpp +++ b/Adafruit_Sensor.cpp @@ -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 diff --git a/Adafruit_Sensor.h b/Adafruit_Sensor.h index 087eda5..103dba4 100755 --- a/Adafruit_Sensor.h +++ b/Adafruit_Sensor.h @@ -20,14 +20,17 @@ #ifndef _ADAFRUIT_SENSOR_H #define _ADAFRUIT_SENSOR_H -#ifndef ARDUINO +//#ifndef ARDUINO #include -#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 +#include /* 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 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; diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7ed3acf --- /dev/null +++ b/CMakeLists.txt @@ -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 +)