You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor pressure property to handle ZeroDivision case
Recap: in the `pressure` property there is a test `if var1 == 0:` in order to avoid a division-by-zero exception. Current code return a pressure of 0 in that case.
It was suggested that my PR makes this a little more pythonic by usage of exception raise.
Analysis of the root cause: because last calculation is
```var1 = (1.0 + var1 / 32768.0) * self._pressure_calib[0]```
the only reason for `var1` to be 0 is `self._pressure_calib[0]` is 0. Calibration values are read at init time from registers in the chip. So the root cause probably relates to a register read problem.
Analysis of other similar codes
Bosch bme280.c (Github): if var1 == 0, return minimum valid pressure (300hPa).
Adafruit CiPy BMP280 driver:
=> initially, no tests were performed
=> In Feb, jraber implemeted return 0 in a commit named "Adopt changes from the BME280 library"
=> In Mar, jraber implemeted return minimum value in a commit named "Remove unnecessary 'if' and 'else' in the pressure property"
Adafruit CiPy BMP680 driver: not tested => ZeroDivisionError
I feel that returning silently the minimun valid pressure is not correct to draw attention on a possible hardware reliability problem. Returning 0 could make sense only if we were sure that the user was testing the return value against 0.
Alternatively, letting the ZeroDivisionError occur will not provide proper clue to the user as to the root cause of the problem and will probably lead to this issue to be raised again as a bug in GitHub.
Proposed solution:
Test against 0 and raise a ArithmeticError with message "Invalid calculation possibly related to error while reading the calibration registers"
0 commit comments