Skip to content

Commit b8921b9

Browse files
committed
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)
1 parent bea8508 commit b8921b9

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

code/espurna/sensors/BH1750Sensor.h

+22-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
2020
// Device is automatically set to Power Down after measurement.
2121

22-
class BH1750Sensor : public I2CSensor<> {
22+
static constexpr bool bh1750_is_mode2(unsigned char mode) {
23+
return (mode == BH1750_CONTINUOUS_HIGH_RES_MODE_2) || (mode == BH1750_ONE_TIME_HIGH_RES_MODE_2);
24+
}
2325

26+
class BH1750Sensor : public I2CSensor<> {
2427
public:
2528

2629
void setMode(unsigned char mode) {
@@ -48,7 +51,9 @@ class BH1750Sensor : public I2CSensor<> {
4851
// Initialization method, must be idempotent
4952
void begin() override {
5053

51-
if (!_dirty) return;
54+
if (!_dirty) {
55+
return;
56+
}
5257

5358
// I2C auto-discover
5459
static constexpr uint8_t addresses[] {0x23, 0x5C};
@@ -90,10 +95,18 @@ class BH1750Sensor : public I2CSensor<> {
9095
return 0;
9196
}
9297

98+
// Number of decimals for a unit (or -1 for default)
99+
signed char decimals(espurna::sensor::Unit unit) const {
100+
if (bh1750_is_mode2(_mode)) {
101+
return 2;
102+
}
103+
104+
return 1;
105+
}
106+
93107
protected:
94108

95109
double _read(uint8_t address) {
96-
97110
// For one-shot modes reconfigure sensor & wait for conversion
98111
if (_run_configure) {
99112

@@ -108,7 +121,7 @@ class BH1750Sensor : public I2CSensor<> {
108121
espurna::duration::Milliseconds { (_mode & 0x02) ? 24 : 180 });
109122

110123
// Keep on running configure each time if one-shot mode
111-
_run_configure = _mode & 0x20;
124+
_run_configure = (_mode & 0x20) > 0;
112125

113126
}
114127

@@ -119,12 +132,16 @@ class BH1750Sensor : public I2CSensor<> {
119132
return 0;
120133
}
121134

122-
return ((double) level) / 1.2;
135+
// When using HIGH Mode2, value is halved
136+
const auto multiplier = bh1750_is_mode2(_mode) ? 0.5 : 1.0;
123137

138+
// TODO also * MTreg?
139+
return ((double)level / 1.2) * multiplier;
124140
}
125141

126142
unsigned char _mode;
127143
bool _run_configure = false;
144+
128145
double _lux = 0;
129146

130147
};

0 commit comments

Comments
 (0)