@@ -126,9 +126,13 @@ class TSL2561(object):
126
126
INTEG_TIME_101MS = 0x01 # 101ms
127
127
INTEG_TIME_402MS = 0x02 # 402ms
128
128
129
- GAIN_0X = 0x00 # No gain
129
+ VALID_INTEG_TIMES = (INTEG_TIME_13MS , INTEG_TIME_101MS , INTEG_TIME_402MS )
130
+
131
+ GAIN_0X = 0x00 # No gain
130
132
GAIN_16X = 0x10 # 16x gain
131
133
134
+ VALID_GAINS = (GAIN_0X , GAIN_16X )
135
+
132
136
##################################################################
133
137
#
134
138
def __init__ (self , addr ):
@@ -284,33 +288,57 @@ def calculate_lux(self, ch0, ch1):
284
288
285
289
##################################################################
286
290
#
287
- def set_timing (self , integration ):
291
+ def set_timing (self , integration = None ):
288
292
"""
293
+ Update the integration value as supplied in the argument. If the
294
+ 'integration' argument is not supplied, use the already set value.
289
295
290
296
Arguments:
291
297
- `integration`:
292
298
"""
293
299
self .enable ()
294
- self .integration = integration
300
+ if integration is not None :
301
+ self .integration = integration
295
302
self .i2c .write8 (self .COMMAND_BIT | self .REG_TIMING ,
296
303
self .integration | self .gain )
297
304
self .disable ()
298
305
return
299
306
300
307
##################################################################
301
308
#
302
- def set_gain (self , gain ):
309
+ def set_gain (self , gain = None ):
303
310
"""
311
+ Update the gain value as supplied in the argument. If the
312
+ 'gain' argument is not supplied, use the already set value.
313
+
304
314
Arguments:
305
315
- `gain`:
306
316
"""
307
317
self .enable ()
308
- self .gain = gain
318
+ if gain is not None :
319
+ self .gain = gain
320
+ elif gain not in self .VALID_GAINS :
321
+ raise ValueError ("%d is not a valid gain (%s)" % \
322
+ (gain , repr (self .VALID_GAINS )))
323
+
309
324
self .i2c .write8 (self .COMMAND_BIT | self .REG_TIMING ,
310
325
self .integration | self .gain )
311
326
self .disable ()
312
327
return
313
328
329
+ ##################################################################
330
+ #
331
+ def get_id (self ):
332
+ """
333
+ Read the part number and silicon revision number for this device.
334
+ It returns a tuple of (part number, revision number)
335
+ """
336
+ self .enable ()
337
+ id = self .i2c .readU8 (self .COMMAND_BIT | self .REG_ID )
338
+ self .disable ()
339
+ print "id is: %d, binary: %s" % (id ,bin (id ))
340
+ return ((id & 0xf0 ) >> 4 , (id & 0x0f ))
341
+
314
342
##################################################################
315
343
#
316
344
def get_luminosity (self ):
@@ -351,6 +379,8 @@ def main():
351
379
352
380
tsl2561 = TSL2561 (TSL2561 .ADDR_FLOAT )
353
381
# tsl2561.set_timing(TSL2561.INTEG_TIME_101MS)
382
+ part_number , revision = tsl2561 .get_id ()
383
+ print "TSL2561 part number: %d, revision: %d" % (part_number , revision )
354
384
while True :
355
385
full_spectrum , ir = tsl2561 .get_luminosity ()
356
386
print "\n *******"
0 commit comments