From f31bf7762e9ac970cb51d9eea890c40f5035d2f1 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 13 Jul 2021 13:54:41 +0200 Subject: [PATCH] Converted to C++ and esp-idf --- Adafruit_TSL2561_U.cpp | 75 +++++++++++++++++++++++------------------- Adafruit_TSL2561_U.h | 8 ++--- CMakeLists.txt | 32 ++++++++++++++++++ 3 files changed, 77 insertions(+), 38 deletions(-) create mode 100644 CMakeLists.txt diff --git a/Adafruit_TSL2561_U.cpp b/Adafruit_TSL2561_U.cpp index 0b746fc..0d89b6f 100644 --- a/Adafruit_TSL2561_U.cpp +++ b/Adafruit_TSL2561_U.cpp @@ -41,6 +41,8 @@ #include "Adafruit_TSL2561_U.h" +#include + /*========================================================================*/ /* CONSTRUCTORS */ /*========================================================================*/ @@ -74,9 +76,10 @@ Adafruit_TSL2561_Unified::Adafruit_TSL2561_Unified(uint8_t addr, @returns True if sensor is found and initialized, false otherwise. */ /**************************************************************************/ -boolean Adafruit_TSL2561_Unified::begin() { +boolean Adafruit_TSL2561_Unified::begin(bool skipWireBegin) { _i2c = &Wire; - _i2c->begin(); + if (!skipWireBegin) + _i2c->begin(); return init(); } @@ -88,9 +91,10 @@ boolean Adafruit_TSL2561_Unified::begin() { @returns True if sensor is found and initialized, false otherwise. */ /**************************************************************************/ -boolean Adafruit_TSL2561_Unified::begin(TwoWire *theWire) { +boolean Adafruit_TSL2561_Unified::begin(TwoWire *theWire, bool skipWireBegin) { _i2c = theWire; - _i2c->begin(); + if (!skipWireBegin) + _i2c->begin(); return init(); } @@ -442,6 +446,9 @@ uint32_t Adafruit_TSL2561_Unified::calculateLux(uint16_t broadband, } else if (ratio > TSL2561_LUX_K8T) { b = TSL2561_LUX_B8T; m = TSL2561_LUX_M8T; + } else { + b = 0; + m = 0; } #endif @@ -473,25 +480,25 @@ uint32_t Adafruit_TSL2561_Unified::calculateLux(uint16_t broadband, false if sensor is saturated */ /**************************************************************************/ -bool Adafruit_TSL2561_Unified::getEvent(sensors_event_t *event) { - uint16_t broadband, ir; +std::optional Adafruit_TSL2561_Unified::getEvent() { + sensors_event_t event; + event.version = sizeof(sensors_event_t); + event.sensor_id = _tsl2561SensorID; + event.type = SENSOR_TYPE_LIGHT; + event.reserved0 = 0; + event.timestamp = espchrono::millis_clock::now(); - /* Clear the event */ - memset(event, 0, sizeof(sensors_event_t)); - - event->version = sizeof(sensors_event_t); - event->sensor_id = _tsl2561SensorID; - event->type = SENSOR_TYPE_LIGHT; - event->timestamp = millis(); - - /* Calculate the actual lux value */ - getLuminosity(&broadband, &ir); - event->light = calculateLux(broadband, ir); - - if (event->light == 65536) { - return false; + { + /* Calculate the actual lux value */ + uint16_t broadband, ir; + getLuminosity(&broadband, &ir); + event.light = calculateLux(broadband, ir); } - return true; + + if (event.light == 65536) { + return std::nullopt; + } + return event; } /**************************************************************************/ @@ -501,20 +508,20 @@ bool Adafruit_TSL2561_Unified::getEvent(sensors_event_t *event) { details about the TSL2561 and its capabilities */ /**************************************************************************/ -void Adafruit_TSL2561_Unified::getSensor(sensor_t *sensor) { - /* Clear the sensor_t object */ - memset(sensor, 0, sizeof(sensor_t)); - +sensor_t Adafruit_TSL2561_Unified::getSensor() { + sensor_t sensor; /* Insert the sensor name in the fixed length char array */ - strncpy(sensor->name, "TSL2561", sizeof(sensor->name) - 1); - sensor->name[sizeof(sensor->name) - 1] = 0; - sensor->version = 1; - sensor->sensor_id = _tsl2561SensorID; - sensor->type = SENSOR_TYPE_LIGHT; - sensor->min_delay = 0; - sensor->max_value = 17000.0; /* Based on trial and error ... confirm! */ - sensor->min_value = 1.0; - sensor->resolution = 1.0; + strncpy(sensor.name, "TSL2561", sizeof(sensor.name) - 1); + sensor.name[sizeof(sensor.name) - 1] = 0; + + sensor.version = 1; + sensor.sensor_id = _tsl2561SensorID; + sensor.type = SENSOR_TYPE_LIGHT; + sensor.max_value = 17000.0; /* Based on trial and error ... confirm! */ + sensor.min_value = 1.0; + sensor.resolution = 1.0; + sensor.min_delay = 0; + return sensor; } /*========================================================================*/ diff --git a/Adafruit_TSL2561_U.h b/Adafruit_TSL2561_U.h index 202ce32..18ce92b 100644 --- a/Adafruit_TSL2561_U.h +++ b/Adafruit_TSL2561_U.h @@ -167,8 +167,8 @@ typedef enum { class Adafruit_TSL2561_Unified : public Adafruit_Sensor { public: Adafruit_TSL2561_Unified(uint8_t addr, int32_t sensorID = -1); - boolean begin(void); - boolean begin(TwoWire *theWire); + boolean begin(bool skipWireBegin = false); + boolean begin(TwoWire *theWire, bool skipWireBegin = false); boolean init(); /* TSL2561 Functions */ @@ -179,8 +179,8 @@ public: uint32_t calculateLux(uint16_t broadband, uint16_t ir); /* Unified Sensor API Functions */ - bool getEvent(sensors_event_t *); - void getSensor(sensor_t *); + std::optional getEvent() override; + sensor_t getSensor() override; private: TwoWire *_i2c; diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e5fcf4d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,32 @@ +set(headers + Adafruit_TSL2561_U.h +) + +set(sources + Adafruit_TSL2561_U.cpp +) + +set(dependencies + arduino-esp32 + Adafruit_Sensor +) + +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 +)