sensoresp/src/sensor_tcs34725.cpp

161 lines
4.9 KiB
C++

#ifdef SENSOR_TCS34725
#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();
}
}
#endif