Skip to content

Commit 69414b2

Browse files
Adrian McEwenpeter-pycom
authored andcommitted
Fix division-by-zero bug in the lux() calculation
1 parent 08f2f85 commit 69414b2

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

shields/lib/LTR329ALS01.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, pysense = None, sda = 'P22', scl = 'P21', gain = ALS_GAIN_1X,
7171

7272
self.gain = gain
7373
self.integration = integration
74-
74+
7575
contr = self._getContr(gain)
7676
self.i2c.writeto_mem(ALS_I2CADDR, ALS_CONTR_REG, bytearray([contr]))
7777

@@ -101,14 +101,17 @@ def light(self):
101101
return (data0, data1)
102102

103103
def lux(self):
104-
# Calculate Lux value from formula in Appendix A of the datasheet
104+
# Calculate Lux value from formular in Appendix A of the datasheet
105105
light_level = self.light()
106-
ratio = light_level[1]/(light_level[0]+light_level[1])
107-
if ratio < 0.45:
108-
return (1.7743 * light_level[0] + 1.1059 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
109-
elif ratio < 0.64 and ratio >= 0.45:
110-
return (4.2785 * light_level[0] - 1.9548 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
111-
elif ratio < 0.85 and ratio >= 0.64:
112-
return (0.5926 * light_level[0] + 0.1185 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
106+
if light_level[0]+light_level[1] > 0:
107+
ratio = light_level[1]/(light_level[0]+light_level[1])
108+
if ratio < 0.45:
109+
return (1.7743 * light_level[0] + 1.1059 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
110+
elif ratio < 0.64 and ratio >= 0.45:
111+
return (4.2785 * light_level[0] - 1.9548 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
112+
elif ratio < 0.85 and ratio >= 0.64:
113+
return (0.5926 * light_level[0] + 0.1185 * light_level[1]) / self.ALS_GAIN_VALUES[self.gain] / self.ALS_INT_VALUES[self.integration]
114+
else:
115+
return 0
113116
else:
114117
return 0

0 commit comments

Comments
 (0)