CanGrow/include/Sensor/06_TCS34725.h

72 lines
2.5 KiB
C

/*
*
* include/Sensor/06_TCS34725.h - header for I2C color sensor TCS34725
*
*
*
*/
//#include "TCS34725.h"
#include "Adafruit_TCS34725.h"
#define SENSOR_06_NAME "TCS34725"
const byte Sensor_06_TCS34725_Addr[] = { 0x29 };
Adafruit_TCS34725 TCS34725[sizeof(Sensor_06_TCS34725_Addr)];
/* This library causes a 240ms (or greater if chosen) delay when reading the values from the sensor
* this is not optimal, and there are libs workarounding this behaviour.
* But unfortunatelly the other libs wont connect successful by i2c to the sensor,
* which only the adafruit lib does reliably 240MS integration time and 4x gain
* seems to be the sweet spot between delay and value resolutin */
/* Create main data array specifying max amount of readings */
float Sensor_06_TCS34725[sizeof(Sensor_06_TCS34725_Addr)][5];
void Sensor_06_TCS34725_Update(const byte AddrId) {
uint16_t r, g, b, c, colorTemp, lux;
TCS34725[AddrId].getRawData(&r, &g, &b, &c);
colorTemp = TCS34725[AddrId].calculateColorTemperature_dn40(r, g, b, c);
lux = TCS34725[AddrId].calculateLux(r, g, b);
Sensor_06_TCS34725[AddrId][0] = colorTemp;
Sensor_06_TCS34725[AddrId][1] = lux;
Sensor_06_TCS34725[AddrId][2] = r;
Sensor_06_TCS34725[AddrId][3] = g;
Sensor_06_TCS34725[AddrId][4] = b;
}
bool Sensor_06_TCS34725_Init(const byte AddrId) {
/* Sensor Init function
*
* returns true (1) when Init was successful
* returns false (0) if not.
*/
const static char LogLoc[] PROGMEM = "[Sensor:06_TCS34725:Init]";
bool returnCode;
if(TCS34725[AddrId].begin()) {
/* Here I hardcoded here the values for Integration time and Gain.
* For calibration I used my desk lamp and a lux smartphone app.
* I fooled around until the smartphone app reading was kinda the
* same as the TCS34725 ones. Yay!
*
* Comes out TCS34725_INTEGRATIONTIME_240MS and TCS34725_GAIN_16X
* seem to be good values. Smartphone reading of my desk lamp is
* 3507lx and on the exakt same spot, height, angle and so on the
* TCS34725 measures 3487lx. I guess this is fine. */
Log.notice(F("%s found at addr 0x%x - Integration time: 240ms Gain: 16x" CR), LogLoc, Sensor_06_TCS34725_Addr[AddrId]);
TCS34725[AddrId].setIntegrationTime(TCS34725_INTEGRATIONTIME_240MS);
TCS34725[AddrId].setGain(TCS34725_GAIN_16X);
Sensor_06_TCS34725_Update(AddrId);
returnCode = true;
} else {
Log.error(F("%s FAILED! Not found at addr 0x%x" CR), LogLoc, Sensor_06_TCS34725_Addr[AddrId]);
returnCode = false;
}
return returnCode;
}