BH1750 icon indicating copy to clipboard operation
BH1750 copied to clipboard

Improve error handling

Open claws opened this issue 7 years ago • 4 comments

The problem highlighted in this issue is pretty unlikely and has never been reported before so is likely very low priority.

When reading a light value there is a possibility that the data read is not the expected 2 bytes. The implementation assumes that 2 bytes are read, even if they are not, and continues progressing through the function which could then use garbage data in the uninitialised level variable.

Only a happy path is really accommodated for in the implementation and Arduino does not seem to really support error handling (e.g. exceptions). Perhaps we could consider returning a negative number to signal an error - though the existing return value type does not support that. Perhaps in this condition it could return 65535 which is well above the maximum possible lux value (which is raw/1.2 = 54612).

claws avatar Dec 29 '17 01:12 claws

Maybe there is another flaw: If we use H-Resolution Mode2 we get a 0.5 resolution. Therefore we need a float as return value. And in that case we could use negative numbers

coelner avatar Jan 05 '18 11:01 coelner

I added an else-path at BH1750.cpp:183 with

Wire.reset();

Without this the I2C bus was held down forever. It took me some hours because the problem was not easily reproducible.

Here is the PR https://github.com/claws/BH1750/pull/41

bjung82 avatar Feb 26 '18 18:02 bjung82

@bjung82 explanation @ #41

coelner avatar Feb 28 '18 08:02 coelner

Any issue left or should this topic be closed?

coelner avatar Oct 25 '18 10:10 coelner