Skip to content

Commit fe63020

Browse files
committed
Throws an OverflowError when sensor is saturated, put in handling blocks in example and main method in TSL2561 module
1 parent f89b06b commit fe63020

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Adafruit_TSL2561/Adafruit_TSL2561.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Bug #2: The read8 and read16 methods (functions) call the I2C readS8 and readS16 methods respectively.
2323
They should call the readU8 and readU16 (i.e. unsigned) methods.
2424
Minor fixes and changes due to Pycharm and SonarQube recommendations, it looks like Python more than C++ now
25+
Added Exception thrown on sensor saturation
2526
1.0 - Initial release - Iain Colledge
2627
Removed commented out C++ code
2728
Added calculate_avg_lux
@@ -35,11 +36,11 @@
3536
import time
3637
from Adafruit_I2C import Adafruit_I2C
3738

38-
# Logging needs to be set at top
39-
#logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
40-
39+
# Logging needs to be set at top after imports
40+
# logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
4141

4242

43+
# TODO: Not sure why this is inheriting the Adafruit I2C class, might need some refactoring
4344
class AdafruitTSL2561(Adafruit_I2C):
4445
TSL2561_VISIBLE = 2 # channel 0 - channel 1
4546
TSL2561_INFRARED = 1 # channel 1
@@ -354,6 +355,8 @@ def get_luminosity(self):
354355
# This is a hack to ensure that when looping with autogain the gain can go up and down as without
355356
# setting the gain to 1X before every reading it doesn't seem able to go from 16X
356357
# back to 1X again. Going from 1X to 16X works fine. - IC
358+
# TODO: Grok the algorithm and fix it
359+
357360
if self._tsl2561AutoGain:
358361
self.set_gain(self.TSL2561_GAIN_1X)
359362

@@ -419,6 +422,8 @@ def calculate_lux(self):
419422
Returns 0 if the sensor is saturated and the values are unreliable.
420423
421424
:return: lux value, unsigned 16bit word (0 - 65535)
425+
:raises: OverflowError when TSL2561 sensor is saturated
426+
422427
"""
423428
logging.debug('calculate_lux')
424429
self.get_luminosity()
@@ -430,10 +435,8 @@ def calculate_lux(self):
430435
else:
431436
clip_threshold = self.TSL2561_CLIPPING_402MS
432437

433-
# Return 0 lux if the sensor is saturated */
434-
# TODO: Throw an exception rather than return 0
435438
if (self._broadband > clip_threshold) or (self._ir > clip_threshold):
436-
return 0
439+
raise OverflowError('TSL2561 Sensor Saturated')
437440

438441
# Get the correct scale depending on the intergration time */
439442
if self._tsl2561IntegrationTime ==self.TSL2561_INTEGRATIONTIME_13MS:
@@ -558,10 +561,12 @@ def calculate_avg_lux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
558561
if arg == "loop":
559562
while True:
560563
try:
561-
print (int(LightSensor.calculate_avg_lux()))
564+
print(int(LightSensor.calculate_avg_lux()))
565+
except OverflowError as e:
566+
print(e)
562567
except KeyboardInterrupt:
563568
quit()
564569
else:
565-
print ("Invalid arg(s):", sys.argv)
570+
print("Invalid arg(s):", sys.argv)
566571
except IndexError:
567-
print (int(LightSensor.calculate_avg_lux()))
572+
print(int(LightSensor.calculate_avg_lux()))

Adafruit_TSL2561/Adafruit_TSL2561_example.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
LightSensor.enable_auto_gain(True)
1111

1212
# Get the calculated lux value, this is a spot reading so if you're under light
13-
lux = LightSensor.calculate_lux()
14-
15-
print('Lux value is %d',lux)
16-
17-
13+
try:
14+
lux = LightSensor.calculate_lux()
15+
except OverflowError as e:
16+
print(e)
17+
else:
18+
print('Lux value is %d', lux)

0 commit comments

Comments
 (0)