diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 72293c30d..908fd262a 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,6 +1,7 @@ /* 6.2.1.5 20180921 * Add authentication to HTTP web pages * Add energy monitoring to Shelly2 (#2789) + * Rewrite TSL2561 driver to fix some issues (#3681) * * 6.2.1.4 20180916 * Add command SerialSend5 to send raw serial data like "A5074100545293" diff --git a/sonoff/xsns_16_tsl2561.ino b/sonoff/xsns_16_tsl2561.ino index ac0536c6a..9f785e3e4 100644 --- a/sonoff/xsns_16_tsl2561.ino +++ b/sonoff/xsns_16_tsl2561.ino @@ -31,17 +31,67 @@ Tsl2561 Tsl(Wire); +uint8_t tsl2561_type = 0; +uint8_t tsl2561_valid = 0; +uint32_t tsl2561_milliLux = 0; +char tsl2561_types[] = "TSL2561"; + +bool Tsl2561Read() +{ + if (tsl2561_valid) { tsl2561_valid--; } + + uint8_t id; + bool gain; + Tsl2561::exposure_t exposure; + uint16_t scaledFull, scaledIr; + uint32_t full, ir; + + if (Tsl.available()) { + if (Tsl.on()) { + if (Tsl.id(id) + && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) + && Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) + && Tsl2561Util::milliLux(full, ir, tsl2561_milliLux, Tsl2561::packageCS(id))) { + } else{ + tsl2561_milliLux = 0; + } + } + } + tsl2561_valid = SENSOR_MAX_MISS; + return true; +} + void Tsl2561Detect() { + if (tsl2561_type) { return; } + if (!Tsl.available()) { Tsl.begin(); if (Tsl.available()) { - snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "TSL2561", Tsl.address()); + tsl2561_type = 1; + snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, tsl2561_types, Tsl.address()); AddLog(LOG_LEVEL_DEBUG); } } } +void Tsl2561EverySecond() +{ + if (90 == (uptime %100)) { + // 1mS + Tsl2561Detect(); + } + else if (uptime &3) { + // 1mS + if (tsl2561_type) { + if (!Tsl2561Read()) { + AddLogMissed(tsl2561_types, tsl2561_valid); +// if (!tsl2561_valid) { tsl2561_type = 0; } + } + } + } +} + #ifdef USE_WEBSERVER const char HTTP_SNS_TSL2561[] PROGMEM = "%s{s}TSL2561 " D_ILLUMINANCE "{m}%u.%03u " D_UNIT_LUX "{e}"; // {s} = , {m} = , {e} = @@ -49,37 +99,17 @@ const char HTTP_SNS_TSL2561[] PROGMEM = void Tsl2561Show(boolean json) { - uint8_t id; - bool gain; - Tsl2561::exposure_t exposure; - uint16_t scaledFull, scaledIr; - uint32_t full, ir; - uint32_t milliLux; - - if (Tsl.available()) { - if (Tsl.on()) { - if( Tsl.id(id) - && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) - && Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) - && Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id))) { - -// snprintf_P(log_data, sizeof(log_data), PSTR(D_ILLUMINANCE " g:%d, e:%d, f:%u, i:%u -> %u.%03u " D_UNIT_LUX), -// gain, exposure, full, ir, milliLux/1000, milliLux%1000); -// AddLog(LOG_LEVEL_DEBUG); - - if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"), - mqtt_data, milliLux/1000, milliLux%1000); + if (tsl2561_valid) { + if (json) { + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"), + mqtt_data, tsl2561_milliLux / 1000, tsl2561_milliLux % 1000); #ifdef USE_DOMOTICZ - if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, (milliLux+500)/1000); + if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, (tsl2561_milliLux + 500) / 1000); } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER - } else { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TSL2561, mqtt_data, milliLux/1000, milliLux%1000); + } else { + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TSL2561, mqtt_data, tsl2561_milliLux / 1000, tsl2561_milliLux % 1000); #endif // USE_WEBSERVER - } - } - Tsl.off(); } } } @@ -96,9 +126,12 @@ boolean Xsns16(byte function) if (i2c_flg) { switch (function) { - case FUNC_PREP_BEFORE_TELEPERIOD: + case FUNC_INIT: Tsl2561Detect(); break; + case FUNC_EVERY_SECOND: + Tsl2561EverySecond(); + break; case FUNC_JSON_APPEND: Tsl2561Show(1); break;