22
22
Bug #2: The read8 and read16 methods (functions) call the I2C readS8 and readS16 methods respectively.
23
23
They should call the readU8 and readU16 (i.e. unsigned) methods.
24
24
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
25
26
1.0 - Initial release - Iain Colledge
26
27
Removed commented out C++ code
27
28
Added calculate_avg_lux
35
36
import time
36
37
from Adafruit_I2C import Adafruit_I2C
37
38
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)
41
41
42
42
43
+ # TODO: Not sure why this is inheriting the Adafruit I2C class, might need some refactoring
43
44
class AdafruitTSL2561 (Adafruit_I2C ):
44
45
TSL2561_VISIBLE = 2 # channel 0 - channel 1
45
46
TSL2561_INFRARED = 1 # channel 1
@@ -354,6 +355,8 @@ def get_luminosity(self):
354
355
# This is a hack to ensure that when looping with autogain the gain can go up and down as without
355
356
# setting the gain to 1X before every reading it doesn't seem able to go from 16X
356
357
# back to 1X again. Going from 1X to 16X works fine. - IC
358
+ # TODO: Grok the algorithm and fix it
359
+
357
360
if self ._tsl2561AutoGain :
358
361
self .set_gain (self .TSL2561_GAIN_1X )
359
362
@@ -419,6 +422,8 @@ def calculate_lux(self):
419
422
Returns 0 if the sensor is saturated and the values are unreliable.
420
423
421
424
:return: lux value, unsigned 16bit word (0 - 65535)
425
+ :raises: OverflowError when TSL2561 sensor is saturated
426
+
422
427
"""
423
428
logging .debug ('calculate_lux' )
424
429
self .get_luminosity ()
@@ -430,10 +435,8 @@ def calculate_lux(self):
430
435
else :
431
436
clip_threshold = self .TSL2561_CLIPPING_402MS
432
437
433
- # Return 0 lux if the sensor is saturated */
434
- # TODO: Throw an exception rather than return 0
435
438
if (self ._broadband > clip_threshold ) or (self ._ir > clip_threshold ):
436
- return 0
439
+ raise OverflowError ( 'TSL2561 Sensor Saturated' )
437
440
438
441
# Get the correct scale depending on the intergration time */
439
442
if self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_13MS :
@@ -558,10 +561,12 @@ def calculate_avg_lux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
558
561
if arg == "loop" :
559
562
while True :
560
563
try :
561
- print (int (LightSensor .calculate_avg_lux ()))
564
+ print (int (LightSensor .calculate_avg_lux ()))
565
+ except OverflowError as e :
566
+ print (e )
562
567
except KeyboardInterrupt :
563
568
quit ()
564
569
else :
565
- print ("Invalid arg(s):" , sys .argv )
570
+ print ("Invalid arg(s):" , sys .argv )
566
571
except IndexError :
567
- print (int (LightSensor .calculate_avg_lux ()))
572
+ print (int (LightSensor .calculate_avg_lux ()))
0 commit comments