From 4dbc5b186c826c2e00952c8fc149cae7b300a397 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 7 Nov 2021 21:56:42 +0100 Subject: [PATCH] move tcs34725 to class --- include/sensor_tcs34725.cpp | 159 +++++++++++++++++++++++++++++++++++ include/sensor_tcs34725.h | 41 +++++++++ platformio.ini | 14 ++-- src/main.cpp | 162 ++++++------------------------------ 4 files changed, 231 insertions(+), 145 deletions(-) create mode 100644 include/sensor_tcs34725.cpp create mode 100644 include/sensor_tcs34725.h diff --git a/include/sensor_tcs34725.cpp b/include/sensor_tcs34725.cpp new file mode 100644 index 0000000..abe75a3 --- /dev/null +++ b/include/sensor_tcs34725.cpp @@ -0,0 +1,159 @@ + +#include "sensor_tcs34725.h" +//#include "Adafruit_TCS34725.h" +//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class + +//Connect SCL to D1, SDA to D2, GND and 3v3 +//Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux) + +Sensor_TCS34725::Sensor_TCS34725() +{ + tcs = new tcs34725(); + #ifndef TCS34725_MINLUXFORCT + #define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high + #endif + #ifndef TCS34725_LUXFACTOR + #define TCS34725_LUXFACTOR 1 + #endif +} + +void Sensor_TCS34725::init() //Things to be done during setup() +{ + Serial.println("initializing tcs34725"); + if (!tcs->begin()) { + Serial.println("No TCS34725 found!"); + }else{ + init_ok=true; + } +} + +//Also called during setup() +void Sensor_TCS34725::setSettings_Lux(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_lux.minchange=minchange; + data_lux.senddelaymax=senddelaymax; + data_lux.readdelay=readdelay; +} + +//Also called during setup() +void Sensor_TCS34725::setSettings_Colortemp(float minchange, unsigned long senddelaymax, unsigned long readdelay) +{ + data_colortemp.minchange=minchange; + data_colortemp.senddelaymax=senddelaymax; + data_colortemp.readdelay=readdelay; +} + +//Called during setup +void Sensor_TCS34725::advertise(HomieNode& p_sensorNode) +{ + sensorNode = &p_sensorNode; + #if defined(SENSOR_LDR) || defined(SENSOR_BH1750) + sensorNode->advertise("light_tcs"); + #else + sensorNode->advertise("light"); + #endif + sensorNode->advertise("colortemp"); +} + +void Sensor_TCS34725::sensorloop() +{ + loop_lux(); + loop_colortemp(); +} + + +void Sensor_TCS34725::loop_lux() +{ + sensordata &d=data_lux; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time + //tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c); + tcs->getData(); + lastread_tcs34725=millis(); + if (tcs->isSaturated){ + Serial.println("Warning: tcs34725 is saturated"); + #ifdef STATUSNODE + sensorNode->setProperty("status").send("TCS34725 is saturated"); + #endif + } + } + //value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b); + uint16_t _value_tcs_lux = tcs->lux*TCS34725_LUXFACTOR; + if (!tcs->isSaturated && _value_tcs_lux<65535){ //sometimes false high reading accur around 65535 sometimes less. with isSaturated check only 65535 values appeared. + d.value = _value_tcs_lux; + } + + if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending TCS Lux. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "light tcs " << ": " << d.value << endl; + #if defined(SENSOR_LDR) || defined(SENSOR_BH1750) + sensorNode.setProperty("light_tcs").send(String(d.value)); + #else + sensorNode->setProperty("light").send(String(d.value)); + #endif + + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } +} +void Sensor_TCS34725::loop_colortemp() +{ + sensordata &d=data_colortemp; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time + //tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c); + tcs->getData(); + lastread_tcs34725=millis(); + if (tcs->isSaturated){ + Serial.println("Warning: tcs34725 is saturated"); + } + } + // colorTemp = tcs.calculateColorTemperature(r, g, b); + //value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c); + if (!tcs->isSaturated){ + d.value = tcs->ct; //with agc + } + + + + if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending TCS colortemp. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + + Homie.getLogger() << "colortemp tcs " << ": " << d.value << endl; + if (tcs->lux>=TCS34725_MINLUXFORCT) { + if (d.value > 1) { + sensorNode->setProperty("colortemp").send(String(d.value)); + }else{ + Homie.getLogger() << "didn't send tcs ct because value is too low" << endl; + sensorNode->setProperty("colortemp").send(String(-1)); + } + }else{ + Homie.getLogger() << "didn't send tcs ct because light too low: " << tcs->lux << "lux" << endl; + sensorNode->setProperty("colortemp").send(String(-1)); + } + + d.lastsentvalue=d.value; + + d.lastsent=millis(); + } +} diff --git a/include/sensor_tcs34725.h b/include/sensor_tcs34725.h new file mode 100644 index 0000000..416c3bf --- /dev/null +++ b/include/sensor_tcs34725.h @@ -0,0 +1,41 @@ +#ifndef SENSOR_TCS34725_H +#define SENSOR_TCS34725_H + + +#include "sensordata.h" +#include + +#include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino + +class Sensor_TCS34725 +{ + +private: + tcs34725 *tcs; //wrapper class with agc + HomieNode *sensorNode; //reference to HomieNode + + struct sensordata data_lux; //struct values are changed in setup() + struct sensordata data_colortemp; //struct values are changed in setup() + + uint16_t value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c; + + unsigned long lastread_tcs34725=0; + + bool init_ok; + +public: + Sensor_TCS34725(); + + void loop_lux(); + void loop_colortemp(); + + void init(); + void setSettings_Lux(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void setSettings_Colortemp(float minchange, unsigned long senddelaymax, unsigned long readdelay); + void advertise(HomieNode& p_sensorNode); + void sensorloop(); + +}; + +#endif + diff --git a/platformio.ini b/platformio.ini index 7f81637..2b20dbd 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,10 +37,10 @@ build_flags = -D dataBMP180_pressure_minchange=0.5 -D SENSOR_TCS34725 - -D dataTCS34725_lux_minchange=500 - -D dataTCS34725_lux_senddelaymax=1000*60*1 - -D dataTCS34725_colortemp_minchange=100 - -D TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078 + -D SENSOR_TCS34725_LUX_minchange=500 + -D SENSOR_TCS34725_LUX_senddelaymax=1000*60*1 + -D SENSOR_TCS34725_COLORTEMP_minchange=100 + -D SENSOR_TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078 -D SENSOR_HS1101 -D SENSOR_HS1101_PIN=D5 @@ -284,10 +284,10 @@ monitor_speed = 115200 build_flags = -D SENSOR_HTU21D - -D dataHTU21D_temperature_minchange=0.2 - -D dataHTU21D_temperature_senddelaymax=1000*60*20 + -D dataHTU21D_temperature_minchange=0.1 + -D dataHTU21D_temperature_senddelaymax=1000*60*60 -D dataHTU21D_humidity_minchange=1.0 - -D dataHTU21D_humidity_senddelaymax=1000*60*30 + -D dataHTU21D_humidity_senddelaymax=1000*60*60 diff --git a/src/main.cpp b/src/main.cpp index 3b88641..803c7b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -228,20 +228,25 @@ #endif #ifdef SENSOR_TCS34725 - //#include "Adafruit_TCS34725.h" - #include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino - //Connect SCL to D1, SDA to D2, GND and 3v3 - //Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux) - //Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class - tcs34725 tcs; //wrapper class with agc - bool tcs34725init_ok=false; - struct sensordata dataTCS34725_lux; - struct sensordata dataTCS34725_colortemp; - uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c; - unsigned long lastread_tcs34725=0; - #define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high - #ifndef TCS34725_LUXFACTOR - #define TCS34725_LUXFACTOR 1 + #include "sensor_tcs34725.cpp" + Sensor_TCS34725 sensor_tcs34725; + #ifndef SENSOR_TCS34725_LUX_minchange + #define SENSOR_TCS34725_LUX_minchange 500 + #endif + #ifndef SENSOR_TCS34725_LUX_senddelaymax + #define SENSOR_TCS34725_LUX_senddelaymax 1000*60*5 + #endif + #ifndef SENSOR_TCS34725_LUX_readdelay + #define SENSOR_TCS34725_LUX_readdelay 1000*10 + #endif + #ifndef SENSOR_TCS34725_COLORTEMP_minchange + #define SENSOR_TCS34725_COLORTEMP_minchange 100 + #endif + #ifndef SENSOR_TCS34725_COLORTEMP_senddelaymax + #define SENSOR_TCS34725_COLORTEMP_senddelaymax 1000*60*5 + #endif + #ifndef SENSOR_TCS34725_COLORTEMP_readdelay + #define SENSOR_TCS34725_COLORTEMP_readdelay 1000*10 #endif #endif @@ -387,21 +392,9 @@ void setup() { #endif #ifdef SENSOR_TCS34725 - Serial.println("initializing tcs34725"); - if (!tcs.begin()) { - Serial.println("No TCS34725 found!"); - }else{ - tcs34725init_ok=true; - } - #ifdef dataTCS34725_lux_minchange - dataTCS34725_lux.minchange=dataTCS34725_lux_minchange; - #endif - #ifdef dataTCS34725_lux_senddelaymax - dataTCS34725_lux.senddelaymax=dataTCS34725_lux_senddelaymax; - #endif - #ifdef dataTCS34725_colortemp_minchange - dataTCS34725_colortemp.minchange=dataTCS34725_colortemp_minchange; - #endif + sensor_tcs34725.init(); + sensor_tcs34725.setSettings_Lux(SENSOR_TCS34725_LUX_minchange,SENSOR_TCS34725_LUX_senddelaymax,SENSOR_TCS34725_LUX_readdelay); + sensor_tcs34725.setSettings_Colortemp(SENSOR_TCS34725_COLORTEMP_minchange,SENSOR_TCS34725_COLORTEMP_senddelaymax,SENSOR_TCS34725_COLORTEMP_readdelay); #endif #ifdef SENSOR_VL53L1X @@ -512,12 +505,7 @@ void setup() { #endif #ifdef SENSOR_TCS34725 - #if defined(SENSOR_LDR) || defined(SENSOR_BH1750) - sensorNode.advertise("light_tcs"); - #else - sensorNode.advertise("light"); - #endif - sensorNode.advertise("colortemp"); + sensor_tcs34725.advertise(sensorNode); #endif #ifdef SENSOR_VL53L1X @@ -546,105 +534,6 @@ void loop() { Homie.loop(); } -#ifdef SENSOR_TCS34725 -void loop_TCS34725_lux() -{ - sensordata &d=dataTCS34725_lux; - - bool _changed=false; - if (millis() >= (d.lastreadtime+d.readdelay)) { - if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time - //tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c); - tcs.getData(); - lastread_tcs34725=millis(); - if (tcs.isSaturated){ - Serial.println("Warning: tcs34725 is saturated"); - #ifdef STATUSNODE - sensorNode.setProperty("status").send("TCS34725 is saturated"); - #endif - } - } - //value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b); - uint16_t _value_tcs_lux = tcs.lux*TCS34725_LUXFACTOR; - if (!tcs.isSaturated && _value_tcs_lux<65535){ //sometimes false high reading accur around 65535 sometimes less. with isSaturated check only 65535 values appeared. - value_tcs_lux = _value_tcs_lux; - } - - if (abs((int)d.lastsentvalue-value_tcs_lux)>=d.minchange){ //int abs - _changed=true; - } - d.lastreadtime=millis(); - } - - if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { - Serial.print("Sending TCS Lux. reason="); - if (_changed) Serial.println("change"); else Serial.println("time"); - checkESPStatus(); - - Homie.getLogger() << "light tcs " << ": " << value_tcs_lux << endl; - #if defined(SENSOR_LDR) || defined(SENSOR_BH1750) - sensorNode.setProperty("light_tcs").send(String(value_tcs_lux)); - #else - sensorNode.setProperty("light").send(String(value_tcs_lux)); - #endif - - d.lastsentvalue=value_tcs_lux; - - d.lastsent=millis(); - } -} -void loop_TCS34725_colortemp() -{ - sensordata &d=dataTCS34725_colortemp; - - bool _changed=false; - if (millis() >= (d.lastreadtime+d.readdelay)) { - if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time - //tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c); - tcs.getData(); - lastread_tcs34725=millis(); - if (tcs.isSaturated){ - Serial.println("Warning: tcs34725 is saturated"); - } - } - // colorTemp = tcs.calculateColorTemperature(r, g, b); - //value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c); - if (!tcs.isSaturated){ - value_colortemp = tcs.ct; //with agc - } - - - - if (abs((int)d.lastsentvalue-value_colortemp)>=d.minchange){ //int abs - _changed=true; - } - d.lastreadtime=millis(); - } - - if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { - Serial.print("Sending TCS colortemp. reason="); - if (_changed) Serial.println("change"); else Serial.println("time"); - checkESPStatus(); - - Homie.getLogger() << "colortemp tcs " << ": " << value_colortemp << endl; - if (tcs.lux>=TCS34725_MINLUXFORCT) { - if (value_colortemp > 1) { - sensorNode.setProperty("colortemp").send(String(value_colortemp)); - }else{ - Homie.getLogger() << "didn't send tcs ct because value is too low" << endl; - sensorNode.setProperty("colortemp").send(String(-1)); - } - }else{ - Homie.getLogger() << "didn't send tcs ct because light too low: " << tcs.lux << "lux" << endl; - sensorNode.setProperty("colortemp").send(String(-1)); - } - - d.lastsentvalue=value_colortemp; - - d.lastsent=millis(); - } -} -#endif @@ -841,10 +730,7 @@ void loopHandler() { #endif #ifdef SENSOR_TCS34725 - if (tcs34725init_ok) { - loop_TCS34725_lux(); - loop_TCS34725_colortemp(); - } + sensor_tcs34725.sensorloop(); #endif #ifdef SENSOR_VL53L1X