From b8921b9a89d36cf71bd80dfd76a1d947ce5d7f58 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 4 Dec 2022 05:50:34 +0300 Subject: [PATCH] bh1750: more decimals for mode2 return half of raw value in both modes involving it resolve #2550 (just for the sensor, no global setting yet) --- code/espurna/sensors/BH1750Sensor.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/code/espurna/sensors/BH1750Sensor.h b/code/espurna/sensors/BH1750Sensor.h index 02a8bad8..d6eb64e4 100644 --- a/code/espurna/sensors/BH1750Sensor.h +++ b/code/espurna/sensors/BH1750Sensor.h @@ -19,8 +19,11 @@ #define BH1750_ONE_TIME_LOW_RES_MODE 0x23 // Start measurement at 1lx resolution. Measurement time is approx 120ms. // Device is automatically set to Power Down after measurement. -class BH1750Sensor : public I2CSensor<> { +static constexpr bool bh1750_is_mode2(unsigned char mode) { + return (mode == BH1750_CONTINUOUS_HIGH_RES_MODE_2) || (mode == BH1750_ONE_TIME_HIGH_RES_MODE_2); +} +class BH1750Sensor : public I2CSensor<> { public: void setMode(unsigned char mode) { @@ -48,7 +51,9 @@ class BH1750Sensor : public I2CSensor<> { // Initialization method, must be idempotent void begin() override { - if (!_dirty) return; + if (!_dirty) { + return; + } // I2C auto-discover static constexpr uint8_t addresses[] {0x23, 0x5C}; @@ -90,10 +95,18 @@ class BH1750Sensor : public I2CSensor<> { return 0; } + // Number of decimals for a unit (or -1 for default) + signed char decimals(espurna::sensor::Unit unit) const { + if (bh1750_is_mode2(_mode)) { + return 2; + } + + return 1; + } + protected: double _read(uint8_t address) { - // For one-shot modes reconfigure sensor & wait for conversion if (_run_configure) { @@ -108,7 +121,7 @@ class BH1750Sensor : public I2CSensor<> { espurna::duration::Milliseconds { (_mode & 0x02) ? 24 : 180 }); // Keep on running configure each time if one-shot mode - _run_configure = _mode & 0x20; + _run_configure = (_mode & 0x20) > 0; } @@ -119,12 +132,16 @@ class BH1750Sensor : public I2CSensor<> { return 0; } - return ((double) level) / 1.2; + // When using HIGH Mode2, value is halved + const auto multiplier = bh1750_is_mode2(_mode) ? 0.5 : 1.0; + // TODO also * MTreg? + return ((double)level / 1.2) * multiplier; } unsigned char _mode; bool _run_configure = false; + double _lux = 0; };