From badaa57240c00de7d72c631461fcfe8614531c44 Mon Sep 17 00:00:00 2001 From: arendst Date: Thu, 8 Feb 2018 16:03:17 +0100 Subject: [PATCH] v5.11.1j - Update TSL2561 driver (#1825) --- sonoff/_releasenotes.ino | 1 + sonoff/xsns_16_tsl2561.ino | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index f3f07a15b..b8638ad89 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -4,6 +4,7 @@ * Fix Arilux RF induced exception by moving interrupt handler to iram on non esp/arduino lib v2.3.0 * Add NTP sync based on chip id (#1773) * Fix regression from 5.11.1h web console and http input max length from 100 to 254 characters (#1819) + * Update TSL2561 driver (#1825) * * 5.11.1i * Update TasmotaSerial library to 1.1.0 diff --git a/sonoff/xsns_16_tsl2561.ino b/sonoff/xsns_16_tsl2561.ino index 1c4e3cc98..79f112dda 100644 --- a/sonoff/xsns_16_tsl2561.ino +++ b/sonoff/xsns_16_tsl2561.ino @@ -31,14 +31,13 @@ uint8_t tsl2561_address; uint8_t tsl2561_addresses[] = { TSL2561_ADDR_LOW, TSL2561_ADDR_FLOAT, TSL2561_ADDR_HIGH }; -uint8_t tsl2561_type = 0; //TSL2561 tsl(TSL2561_ADDR_FLOAT); -TSL2561 *tsl; +TSL2561 *tsl = 0; void Tsl2561Detect() { - if (tsl2561_type) { + if (tsl) { return; } @@ -49,10 +48,12 @@ void Tsl2561Detect() if (tsl->begin()) { tsl->setGain(TSL2561_GAIN_16X); tsl->setTiming(TSL2561_INTEGRATIONTIME_101MS); - tsl2561_type = 1; snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "TSL2561", tsl2561_address); AddLog(LOG_LEVEL_DEBUG); break; + } else { + delete tsl; + tsl = 0; } } } @@ -65,8 +66,23 @@ const char HTTP_SNS_TSL2561[] PROGMEM = void Tsl2561Show(boolean json) { - if (tsl2561_type) { - uint16_t illuminance = tsl->getLuminosity(TSL2561_VISIBLE); + if (tsl) { + union { + uint32_t full; + struct { uint16_t both, ir; }; + } light; + light.full = tsl->getFullLuminosity(); + uint32_t illuminance = 0; + if ((light.full == 0 || light.full == 0xffffffff)) { + if (!I2cDevice(tsl2561_address)) { + delete tsl; + tsl = 0; + } + } else { + illuminance = tsl->calculateLux(light.both, light.ir); + } + snprintf(log_data, sizeof(log_data), PSTR(D_ILLUMINANCE " 0x%08lx = b 0x%04x, i 0x%04x -> %lu " D_UNIT_LUX), light.full, light.both, light.ir, illuminance); + AddLog(LOG_LEVEL_DEBUG); if (json) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%d}"), mqtt_data, illuminance); @@ -110,4 +126,4 @@ boolean Xsns16(byte function) } #endif // USE_TSL2561 -#endif // USE_I2C +#endif // USE_I2C \ No newline at end of file