From 91ed1ea2c669ee012612b3fd1331390557d6637c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 4 Sep 2022 14:48:58 +0200 Subject: [PATCH] Add filter for larger deviations Add filter for larger deviations due to I2C misreads --- .../tasmota_xsns_sensor/xsns_99_luxv30b.ino | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tasmota/tasmota_xsns_sensor/xsns_99_luxv30b.ino b/tasmota/tasmota_xsns_sensor/xsns_99_luxv30b.ino index b833a5c07..3c3cd89e9 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_99_luxv30b.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_99_luxv30b.ino @@ -126,6 +126,7 @@ class LuxV30b { float Lux(); bool _found; uint32_t _lux; + uint32_t _lux_last; }; LuxV30b::LuxV30b() { @@ -145,13 +146,18 @@ bool LuxV30b::Found() { } void LuxV30b::Read() { - _lux = I2cRead8(LUXV30B_ADDR, 0); - delay(8); - _lux |= I2cRead8(LUXV30B_ADDR, 1) << 8; - delay(8); - _lux |= I2cRead8(LUXV30B_ADDR, 2) << 16; - delay(8); - _lux |= I2cRead8(LUXV30B_ADDR, 3) << 24; + uint32_t lux = I2cRead8(LUXV30B_ADDR, 0); +// delay(8); + lux |= I2cRead8(LUXV30B_ADDR, 1) << 8; +// delay(8); + lux |= I2cRead8(LUXV30B_ADDR, 2) << 16; +// delay(8); + lux |= I2cRead8(LUXV30B_ADDR, 3) << 24; + + _lux = (lux > (_lux_last << 4)) ? _lux_last : lux; // Filter large deviations due to misreads + _lux_last = lux; + +// AddLog(LOG_LEVEL_DEBUG, PSTR("SCD: Raw %d/%d"), lux, _lux); } float LuxV30b::Lux() {