From cfe7d2f2cf15b16f6b9f9575b135dbb71daf4e65 Mon Sep 17 00:00:00 2001 From: TD-er Date: Wed, 28 Oct 2020 15:58:04 +0100 Subject: [PATCH] [TSL2591] Check sensor before reading + speedup The old implementation did perform 3 readings, which depending on the set integration time could take a while. But since all output values should be based on the same reading anyway, it is simply wrong to perform separate readings. Also added a call to `begin()` of the library to make sure the gain and timing are correctly set. This could cause incorrect readings if the sensor somehow got reset. Fixes: #3347 --- src/_P074_TSL2591.ino | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/_P074_TSL2591.ino b/src/_P074_TSL2591.ino index 52ee372b6..6d8316f75 100644 --- a/src/_P074_TSL2591.ino +++ b/src/_P074_TSL2591.ino @@ -197,15 +197,30 @@ boolean Plugin_074(byte function, struct EventStruct *event, String& string) { static_cast(getPluginTaskData(event->TaskIndex)); if (nullptr != P074_data) { + + // Fix to re-set the gain/timing before every read. + // See https://github.com/letscontrolit/ESPEasy/issues/3347 + if (!P074_data->tsl.begin()) { + break; + } + // Simple data read example. Just read the infrared, fullspecrtrum diode // or 'visible' (difference between the two) channels. - // This can take 100-600 milliseconds! Uncomment whichever of the - // following you want to read float lux, full, visible, ir; - visible = P074_data->tsl.getLuminosity(TSL2591_VISIBLE); - ir = P074_data->tsl.getLuminosity(TSL2591_INFRARED); - full = P074_data->tsl.getLuminosity(TSL2591_FULLSPECTRUM); - lux = P074_data->tsl.calculateLuxf(full, ir); // get LUX + { + uint32_t fullLuminosity = P074_data->tsl.getFullLuminosity(); + + // TSL2591_FULLSPECTRUM: Reads two byte value from channel 0 (visible + infrared) + full = (fullLuminosity & 0xFFFF); + + // TSL2591_INFRARED: Reads two byte value from channel 1 (infrared) + ir = (fullLuminosity >> 16); + + // TSL2591_VISIBLE: Reads all and subtracts out just the visible! + visible = ( (fullLuminosity & 0xFFFF) - (fullLuminosity >> 16)); + + lux = P074_data->tsl.calculateLuxf(full, ir); // get LUX + } UserVar[event->BaseVarIndex + 0] = lux; UserVar[event->BaseVarIndex + 1] = full;