Converted to C++ and esp-idf

This commit is contained in:
2021-07-13 13:54:41 +02:00
parent 0856f62c1d
commit f31bf7762e
3 changed files with 77 additions and 38 deletions

View File

@ -41,6 +41,8 @@
#include "Adafruit_TSL2561_U.h"
#include <espchrono.h>
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
@ -74,8 +76,9 @@ 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;
if (!skipWireBegin)
_i2c->begin();
return init();
}
@ -88,8 +91,9 @@ 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;
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;
/* 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();
std::optional<sensors_event_t> 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();
{
/* Calculate the actual lux value */
uint16_t broadband, ir;
getLuminosity(&broadband, &ir);
event->light = calculateLux(broadband, ir);
if (event->light == 65536) {
return false;
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;
}
/*========================================================================*/

View File

@ -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<sensors_event_t> getEvent() override;
sensor_t getSensor() override;
private:
TwoWire *_i2c;

32
CMakeLists.txt Normal file
View File

@ -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
)