24
24
Minor fixes and changes
25
25
1.0 - Initial release - Iain Colledge
26
26
Removed commented out C++ code
27
- Added calculateAvgLux
28
- Changed main method to use calculateAvgLux and loop argument support added.
27
+ Added calculate_avg_lux
28
+ Changed main method to use calculate_avg_lux and loop argument support added.
29
29
Ported "Extended delays to take into account loose timing with 'delay'" update from CPP code
30
30
Added hack so that with autogain every sample goes from 1x to 16x as going from 16x to 1x does not work
31
31
"""
34
34
import time
35
35
from Adafruit_I2C import Adafruit_I2C
36
36
37
- class Adafruit_TSL2561 (Adafruit_I2C ):
37
+
38
+ class AdafruitTSL2561 (Adafruit_I2C ):
38
39
TSL2561_VISIBLE = 2 # channel 0 - channel 1
39
40
TSL2561_INFRARED = 1 # channel 1
40
41
TSL2561_FULLSPECTRUM = 0 # channel 0
@@ -87,7 +88,7 @@ class Adafruit_TSL2561(Adafruit_I2C):
87
88
TSL2561_LUX_K8T = 0x029a # 1.3 * 2^RATIO_SCALE
88
89
TSL2561_LUX_B8T = 0x0000 # 0.000 * 2^LUX_SCALE
89
90
TSL2561_LUX_M8T = 0x0000 # 0.000 * 2^LUX_SCALE
90
-
91
+
91
92
# CS package values
92
93
TSL2561_LUX_K1C = 0x0043 # 0.130 * 2^RATIO_SCALE
93
94
TSL2561_LUX_B1C = 0x0204 # 0.0315 * 2^LUX_SCALE
@@ -140,21 +141,21 @@ class Adafruit_TSL2561(Adafruit_I2C):
140
141
TSL2561_REGISTER_CHAN0_HIGH = 0x0D
141
142
TSL2561_REGISTER_CHAN1_LOW = 0x0E
142
143
TSL2561_REGISTER_CHAN1_HIGH = 0x0F
143
-
144
+
144
145
TSL2561_INTEGRATIONTIME_13MS = 0x00 # 13.7ms
145
146
TSL2561_INTEGRATIONTIME_101MS = 0x01 # 101ms
146
147
TSL2561_INTEGRATIONTIME_402MS = 0x02 # 402ms
147
148
148
149
TSL2561_DELAY_INTTIME_13MS = 0.015
149
150
TSL2561_DELAY_INTTIME_101MS = 0.120
150
151
TSL2561_DELAY_INTTIME_402MS = 0.450
151
-
152
+
152
153
TSL2561_GAIN_1X = 0x00 # No gain
153
154
TSL2561_GAIN_16X = 0x10 # 16x gain
154
155
155
156
TSL2561_NO_OF_AVG_SAMPLES = 25 # How many samples to make an average reading
156
-
157
- def write8 (self , reg , value ):
157
+
158
+ def write8 (self , reg , value ):
158
159
"""
159
160
Writes a register and an 8 bit value over I2C
160
161
@@ -205,32 +206,31 @@ def disable(self):
205
206
self ._i2c .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_CONTROL , self .TSL2561_CONTROL_POWEROFF )
206
207
if (self ._debug == True ): print "disable_end"
207
208
208
- def getData (self ):
209
+ def get_data (self ):
209
210
"""
210
211
Private function to read luminosity on both channels
211
212
"""
212
- if (self ._debug == True ): print "getData "
213
+ if (self ._debug == True ): print "get_data "
213
214
214
- #Enables the device by setting the control bit to 0x03
215
- self .enable ();
215
+ # Enables the device by setting the control bit to 0x03
216
+ self .enable ()
216
217
217
218
# Wait x ms for ADC to complete */
218
219
if self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_13MS :
219
220
time .sleep (self .TSL2561_DELAY_INTTIME_13MS )
220
221
elif self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_101MS :
221
- time .sleep (self .TSL2561_DELAY_INTTIME_101MS )
222
+ time .sleep (self .TSL2561_DELAY_INTTIME_101MS )
222
223
else :
223
- time .sleep (self .TSL2561_DELAY_INTTIME_402MS )
224
-
224
+ time .sleep (self .TSL2561_DELAY_INTTIME_402MS )
225
225
226
226
# Reads a two byte value from channel 0 (visible + infrared) */
227
- self ._broadband = self .read16 (self .TSL2561_COMMAND_BIT | self .TSL2561_WORD_BIT | self .TSL2561_REGISTER_CHAN0_LOW );
227
+ self ._broadband = self .read16 (self .TSL2561_COMMAND_BIT | self .TSL2561_WORD_BIT | self .TSL2561_REGISTER_CHAN0_LOW )
228
228
229
229
# Reads a two byte value from channel 1 (infrared) */
230
- self ._ir = self .read16 (self .TSL2561_COMMAND_BIT | self .TSL2561_WORD_BIT | self .TSL2561_REGISTER_CHAN1_LOW );
230
+ self ._ir = self .read16 (self .TSL2561_COMMAND_BIT | self .TSL2561_WORD_BIT | self .TSL2561_REGISTER_CHAN1_LOW )
231
231
232
232
# Turn the device off to save power */
233
- self .disable ();
233
+ self .disable ()
234
234
if (self ._debug == True ): print "getData_end"
235
235
236
236
def __init__ (self , addr = TSL2561_ADDR_FLOAT , debug = False ):
@@ -264,48 +264,48 @@ def begin(self):
264
264
"""
265
265
if (self ._debug == True ): print "begin"
266
266
# Make sure we're actually connected */
267
- x = self .read8 (self .TSL2561_REGISTER_ID );
267
+ x = self .read8 (self .TSL2561_REGISTER_ID )
268
268
if not (x & 0x0A ):
269
269
return False
270
270
self ._tsl2561Initialised = True
271
271
272
272
# Set default integration time and gain */
273
- self .setIntegrationTime (self ._tsl2561IntegrationTime )
274
- self .setGain (self ._tsl2561Gain )
273
+ self .set_integration_time (self ._tsl2561IntegrationTime )
274
+ self .set_gain (self ._tsl2561Gain )
275
275
276
276
# Note: by default, the device is in power down mode on bootup */
277
277
self .disable ()
278
278
if (self ._debug == True ): print "begin_end"
279
279
280
280
return True
281
-
282
- def enableAutoGain (self , enable ):
281
+
282
+ def enable_auto_gain (self , enable ):
283
283
"""
284
284
Enables or disables the auto-gain settings when reading
285
285
data from the sensor
286
286
287
287
:param enable: True to enable
288
288
"""
289
- if (self ._debug == True ): print "enableAutoGain "
289
+ if (self ._debug == True ): print "enable_auto_gain "
290
290
if (enable == True ):
291
291
self ._tsl2561AutoGain = enable
292
292
else :
293
293
self ._tsl2561AutoGain = False
294
294
if (self ._debug == True ): print "enableAutoGain_end"
295
295
296
- def setIntegrationTime (self , time ):
296
+ def set_integration_time (self , time ):
297
297
"""
298
298
Sets the integration time for the TSL2561
299
299
300
300
:param time:
301
301
:return:
302
302
"""
303
- if (self ._debug == True ): print "setIntegrationTime "
303
+ if (self ._debug == True ): print "set_integration_time "
304
304
if (not self ._tsl2561Initialised ):
305
305
self .begin
306
306
307
307
# Enable the device by setting the control bit to 0x03 */
308
- self .enable ();
308
+ self .enable ()
309
309
310
310
# Update the timing register */
311
311
self .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_TIMING , time | self ._tsl2561Gain )
@@ -316,14 +316,14 @@ def setIntegrationTime(self, time):
316
316
# Turn the device off to save power */
317
317
self .disable ()
318
318
if (self ._debug == True ): print "setIntegrationTime_end"
319
-
320
- def setGain (self , gain ):
319
+
320
+ def set_gain (self , gain ):
321
321
"""
322
322
Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
323
323
324
324
:param gain:
325
325
"""
326
- if (self ._debug == True ): print "setGain "
326
+ if (self ._debug == True ): print "set_gain "
327
327
if (not self ._tsl2561Initialised ):
328
328
self .begin
329
329
@@ -340,7 +340,7 @@ def setGain(self, gain):
340
340
self .disable ()
341
341
if (self ._debug == True ): print "setGain_end"
342
342
343
- def getLuminosity (self ):
343
+ def get_luminosity (self ):
344
344
"""
345
345
Gets the broadband (mixed lighting) and IR only values from
346
346
the TSL2561, adjusting gain if auto-gain is enabled
@@ -350,23 +350,23 @@ def getLuminosity (self):
350
350
# setting the gain to 1X before every reading it doesn't seem able to go from 16X
351
351
# back to 1X again. Going from 1X to 16X works fine. - IC
352
352
if (self ._tsl2561AutoGain ):
353
- self .setGain (self .TSL2561_GAIN_1X )
353
+ self .set_gain (self .TSL2561_GAIN_1X )
354
354
355
- if (self ._debug == True ): print "getLuminosity "
355
+ if (self ._debug == True ): print "get_luminosity "
356
356
valid = False
357
357
358
358
if (not self ._tsl2561Initialised ):
359
- self .begin
359
+ self .begin
360
360
361
361
# If Auto gain disabled get a single reading and continue */
362
362
if (not self ._tsl2561AutoGain ):
363
- self .getData ()
363
+ self .get_data ()
364
364
return
365
365
366
366
# Read data until we find a valid range */
367
367
_agcCheck = False
368
368
while (not valid ):
369
- _it = self ._tsl2561IntegrationTime ;
369
+ _it = self ._tsl2561IntegrationTime
370
370
371
371
# Get the hi/low threshold for the current integration time */
372
372
if _it == self .TSL2561_INTEGRATIONTIME_13MS :
@@ -379,22 +379,22 @@ def getLuminosity (self):
379
379
_hi = self .TSL2561_AGC_THI_402MS
380
380
_lo = self .TSL2561_AGC_TLO_402MS
381
381
382
- self .getData ()
382
+ self .get_data ()
383
383
384
384
# Run an auto-gain check if we haven't already done so ... */
385
385
if (not _agcCheck ):
386
386
if ((self ._broadband < _lo ) and (self ._tsl2561Gain == self .TSL2561_GAIN_1X )):
387
387
# Increase the gain and try again */
388
- self .setGain (self .TSL2561_GAIN_16X )
388
+ self .set_gain (self .TSL2561_GAIN_16X )
389
389
# Drop the previous conversion results */
390
- self .getData ()
390
+ self .get_data ()
391
391
# Set a flag to indicate we've adjusted the gain */
392
392
_agcCheck = True
393
393
elif ((self ._broadband > _hi ) and (self ._tsl2561Gain == self .TSL2561_GAIN_16X )):
394
394
# Drop gain to 1x and try again */
395
- self .setGain (self .TSL2561_GAIN_1X )
395
+ self .set_gain (self .TSL2561_GAIN_1X )
396
396
# Drop the previous conversion results */
397
- self .getData ()
397
+ self .get_data ()
398
398
# Set a flag to indicate we've adjusted the gain */
399
399
_agcCheck = True
400
400
else :
@@ -407,49 +407,49 @@ def getLuminosity (self):
407
407
# and the the other extreme post-gain */
408
408
valid = True
409
409
if (self ._debug == True ): print "getLuminosity_end"
410
-
411
- def calculateLux (self ):
410
+
411
+ def calculate_lux (self ):
412
412
"""
413
413
Converts the raw sensor values to the standard SI lux equivalent.
414
414
Returns 0 if the sensor is saturated and the values are unreliable.
415
415
416
416
:return: lux value, unsigned 16bit word (0 - 65535)
417
417
"""
418
- if (self ._debug == True ): print "calculateLux "
419
- self .getLuminosity ()
418
+ if (self ._debug == True ): print "calculate_lux "
419
+ self .get_luminosity ()
420
420
# Make sure the sensor isn't saturated! */
421
421
if (self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_13MS ):
422
- clipThreshold = self .TSL2561_CLIPPING_13MS
422
+ clip_threshold = self .TSL2561_CLIPPING_13MS
423
423
elif (self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_101MS ):
424
- clipThreshold = self .TSL2561_CLIPPING_101MS
424
+ clip_threshold = self .TSL2561_CLIPPING_101MS
425
425
else :
426
- clipThreshold = self .TSL2561_CLIPPING_402MS
426
+ clip_threshold = self .TSL2561_CLIPPING_402MS
427
427
428
428
# Return 0 lux if the sensor is saturated */
429
429
# TODO: Throw an exception rather than return 0
430
- if ((self ._broadband > clipThreshold ) or (self ._ir > clipThreshold )):
430
+ if ((self ._broadband > clip_threshold ) or (self ._ir > clip_threshold )):
431
431
return 0
432
432
433
433
# Get the correct scale depending on the intergration time */
434
434
if (self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_13MS ):
435
- chScale = self .TSL2561_LUX_CHSCALE_TINT0
435
+ ch_scale = self .TSL2561_LUX_CHSCALE_TINT0
436
436
elif (self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_101MS ):
437
- chScale = self .TSL2561_LUX_CHSCALE_TINT1
437
+ ch_scale = self .TSL2561_LUX_CHSCALE_TINT1
438
438
else :
439
- chScale = (1 << self .TSL2561_LUX_CHSCALE )
439
+ ch_scale = (1 << self .TSL2561_LUX_CHSCALE )
440
440
441
441
# Scale for gain (1x or 16x) */
442
442
if (not self ._tsl2561Gain ):
443
- chScale = chScale << 4
443
+ ch_scale = ch_scale << 4
444
444
445
445
# Scale the channel values */
446
- channel0 = (self ._broadband * chScale ) >> self .TSL2561_LUX_CHSCALE
447
- channel1 = (self ._ir * chScale ) >> self .TSL2561_LUX_CHSCALE
446
+ channel0 = (self ._broadband * ch_scale ) >> self .TSL2561_LUX_CHSCALE
447
+ channel1 = (self ._ir * ch_scale ) >> self .TSL2561_LUX_CHSCALE
448
448
449
449
# Find the ratio of the channel values (Channel1/Channel0) */
450
- ratio1 = 0 ;
450
+ ratio1 = 0
451
451
if (channel0 != 0 ):
452
- ratio1 = (channel1 << (self .TSL2561_LUX_RATIOSCALE + 1 )) / channel0
452
+ ratio1 = (channel1 << (self .TSL2561_LUX_RATIOSCALE + 1 )) / channel0
453
453
454
454
# round the ratio value */
455
455
ratio = (ratio1 + 1 ) >> 1
@@ -504,25 +504,25 @@ def calculateLux(self):
504
504
elif (ratio > self .TSL2561_LUX_K8T ):
505
505
b = self .TSL2561_LUX_B8T
506
506
m = self .TSL2561_LUX_M8T
507
- #endif
507
+ # endif
508
508
509
509
temp = ((channel0 * b ) - (channel1 * m ))
510
510
511
511
# Do not allow negative lux value */
512
- if (temp < 0 ):
512
+ if (temp < 0 ):
513
513
temp = 0
514
514
515
515
# Round lsb (2^(LUX_SCALE-1)) */
516
- temp += (1 << (self .TSL2561_LUX_LUXSCALE - 1 ))
516
+ temp += (1 << (self .TSL2561_LUX_LUXSCALE - 1 ))
517
517
518
518
# Strip off fractional portion */
519
- lux = temp >> self .TSL2561_LUX_LUXSCALE ;
519
+ lux = temp >> self .TSL2561_LUX_LUXSCALE
520
520
521
521
# Signal I2C had no errors */
522
522
if (self ._debug == True ): print "calculateLux_end"
523
523
return lux
524
524
525
- def calculateAvgLux (self , testavg = TSL2561_NO_OF_AVG_SAMPLES ):
525
+ def calculate_avg_lux (self , testavg = TSL2561_NO_OF_AVG_SAMPLES ):
526
526
"""
527
527
Calculates an averaged Lux value, useful for flickering lights and for smoothing values due to noise
528
528
@@ -534,28 +534,28 @@ def calculateAvgLux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
534
534
luxavgtotal = 0
535
535
# Create a cumulative total of values for 'testavg' tests
536
536
while True :
537
- capture = self .calculateLux ()
537
+ capture = self .calculate_lux ()
538
538
luxavgtotal = capture + luxavgtotal
539
539
count += 1
540
540
# Once we reach the number of required tests, work out the average
541
- if ( count >= testavg ):
541
+ if (count >= testavg ):
542
542
luxavg = round (luxavgtotal / testavg )
543
543
return (luxavg )
544
544
545
545
if __name__ == "__main__" :
546
546
LightSensor = Adafruit_TSL2561 ()
547
- LightSensor .enableAutoGain (True )
547
+ LightSensor .enable_auto_gain (True )
548
548
549
549
# See if "loop" has been passed as an arg.
550
550
try :
551
551
arg = sys .argv [1 ]
552
- if ( arg == "loop" ):
552
+ if (arg == "loop" ):
553
553
while True :
554
554
try :
555
- print (int (LightSensor .calculateAvgLux ()))
555
+ print (int (LightSensor .calculate_avg_lux ()))
556
556
except KeyboardInterrupt :
557
557
quit ()
558
558
else :
559
559
print ("Invalid arg(s):" , sys .argv )
560
560
except IndexError :
561
- print (int (LightSensor .calculateAvgLux ()))
561
+ print (int (LightSensor .calculate_avg_lux ()))
0 commit comments