Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 27dfdb8

Browse files
committed
Add some code to range check inputs for timing and gain.
Add a method to get the id of the chip.
1 parent 1fcf2e6 commit 27dfdb8

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

Adafruit_TSL2561/read_tsl2561.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ class TSL2561(object):
126126
INTEG_TIME_101MS = 0x01 # 101ms
127127
INTEG_TIME_402MS = 0x02 # 402ms
128128

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
130132
GAIN_16X = 0x10 # 16x gain
131133

134+
VALID_GAINS = (GAIN_0X, GAIN_16X)
135+
132136
##################################################################
133137
#
134138
def __init__(self, addr):
@@ -284,33 +288,57 @@ def calculate_lux(self, ch0, ch1):
284288

285289
##################################################################
286290
#
287-
def set_timing(self, integration):
291+
def set_timing(self, integration = None):
288292
"""
293+
Update the integration value as supplied in the argument. If the
294+
'integration' argument is not supplied, use the already set value.
289295
290296
Arguments:
291297
- `integration`:
292298
"""
293299
self.enable()
294-
self.integration = integration
300+
if integration is not None:
301+
self.integration = integration
295302
self.i2c.write8(self.COMMAND_BIT | self.REG_TIMING,
296303
self.integration | self.gain)
297304
self.disable()
298305
return
299306

300307
##################################################################
301308
#
302-
def set_gain(self, gain):
309+
def set_gain(self, gain = None):
303310
"""
311+
Update the gain value as supplied in the argument. If the
312+
'gain' argument is not supplied, use the already set value.
313+
304314
Arguments:
305315
- `gain`:
306316
"""
307317
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+
309324
self.i2c.write8(self.COMMAND_BIT | self.REG_TIMING,
310325
self.integration | self.gain)
311326
self.disable()
312327
return
313328

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+
314342
##################################################################
315343
#
316344
def get_luminosity(self):
@@ -351,6 +379,8 @@ def main():
351379

352380
tsl2561 = TSL2561(TSL2561.ADDR_FLOAT)
353381
# 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)
354384
while True:
355385
full_spectrum, ir = tsl2561.get_luminosity()
356386
print "\n*******"

0 commit comments

Comments
 (0)