Skip to content

Commit 8a9647b

Browse files
authored
Merge pull request #6 from Rvice/master
Sensor Overflow Logic fix
2 parents 0fa3c2e + 584a24f commit 8a9647b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

adafruit_tsl2591.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
_TSL2591_LUX_COEFB = 1.64
7070
_TSL2591_LUX_COEFC = 0.59
7171
_TSL2591_LUX_COEFD = 0.86
72+
_TSL2591_MAX_COUNT_100MS = const(36863) # 0x8FFF
73+
_TSL2591_MAX_COUNT = const(65535) # 0xFFFF
7274

7375
# User-facing constants:
7476
GAIN_LOW = 0x00 # low gain (1x)
@@ -248,12 +250,21 @@ def lux(self):
248250
and visible light channels.
249251
"""
250252
channel_0, channel_1 = self.raw_luminosity
253+
254+
# Compute the atime in milliseconds
255+
atime = 100.0 * self._integration_time + 100.0
256+
257+
# Set the maximum sensor counts based on the integration time (atime) setting
258+
if self._integration_time == INTEGRATIONTIME_100MS:
259+
max_counts = _TSL2591_MAX_COUNT_100MS
260+
else:
261+
max_counts = _TSL2591_MAX_COUNT
262+
251263
# Handle overflow.
252-
if channel_0 == 0xFFFF or channel_1 == 0xFFFF:
264+
if channel_0 >= max_counts or channel_1 >= max_counts:
253265
raise RuntimeError('Overflow reading light channels!')
254266
# Calculate lux using same equation as Arduino library:
255267
# https://github.com/adafruit/Adafruit_TSL2591_Library/blob/master/Adafruit_TSL2591.cpp
256-
atime = 100.0 * self._integration_time + 100.0
257268
again = 1.0
258269
if self._gain == GAIN_MED:
259270
again = 25.0

0 commit comments

Comments
 (0)