Skip to content

Commit 93ae2ae

Browse files
committed
Let the device tell us whether it's using the T or CS package.
This actually started with debugging the fact that my TSL2561 returned 0x0A in its ID register when it was off but 0x00 when it was on. I subsequently realized two things: the bits are reversed from what the datasheet says, and I can use the ID register to distinguish between the T and CS packages, so that doesn't need to be an initialization parameter.
1 parent 0ce05da commit 93ae2ae

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

Adafruit_TSL2561/Adafruit_TSL2561.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ class TSL2561(object):
7070
* integrationTime - The integration time in milliseconds for a sensor
7171
reading. See the `integrationTime` class property for more information
7272
and acceptable values. Defaults to 402.
73-
* package - A string indicating which TSL2561 package is in use. Takes one
74-
of two values:
75-
* 'T' - TMB package. This is the default.
76-
* 'CS' - Chipscale package.
7773
* debug - Controls whether additional debugging information is printed as
7874
the class performs its functions. Defaults to False.
7975
@@ -158,17 +154,44 @@ def reset_sensor(cls, address):
158154
TSL2561._REGISTER['CONTROL'] | TSL2561._REG_MOD['COMMAND'],
159155
TSL2561._CONTROL['OFF'])
160156

161-
def __init__(self, address=ADDR_FLOAT, mode=MODE_CONTINUOUS, gain=_gain, integrationTime=_integrationTime.key, package='T', debug=False):
157+
def __init__(self, address=ADDR_FLOAT, mode=MODE_CONTINUOUS, gain=_gain, integrationTime=_integrationTime.key, debug=False):
162158
if mode not in self._MODES:
163159
raise ValueError('Incorrect value passed as operating mode.')
164160
if not address in [TSL2561.ADDR_LOW, TSL2561.ADDR_FLOAT, TSL2561.ADDR_HIGH]:
165161
addr_msg = '0x{0:x} is not a standard TSL2561 address; continuing anyway'.format(address)
166162
warnings.warn(addr_msg)
167163
self.debug = debug
168164
self._i2c = Adafruit_I2C(address, debug=debug)
165+
# The datasheet says that the ID register has a product identifier in
166+
# bits 4-7 and a revision number in bits 0-3. Testing on my (PMG)
167+
# sensor indicates that the bits are completely reversed. I get a value
168+
# of 0x0A (0b00001010) for my TSL2561T when the datasheet says I should
169+
# get 0x50 (0b01010000). The Adafruit Arduino code also checks for
170+
# 0x0A, so the datasheet is probably just wrong.
171+
# Additionally, the even though the datasheet says the ID register never
172+
# changes, it only appears to return the product identifier if the
173+
# sensor is powered off. If the sensor is on, the register returns
174+
# 0x00. Consequently, if we get a 0x00 result (which is also what a
175+
# TSL2560CS returns when it's off), we try turning the sensor off and
176+
# reading the value again, just to check what we're talking to.
169177
id = self._i2c.readU8(TSL2561._REGISTER['ID'])
170-
if id < 0 or not id & 0x0A:
178+
if id == 0:
179+
self._poweroff()
180+
id = self._i2c.readU8(TSL2561._REGISTER['ID'])
181+
if id < 0:
182+
raise EnvironmentError, 'Device at 0x{0:x} does not appear to be a TSL2561.'.format(address)
183+
elif id & 0x0F in (0x0A, 0x02):
184+
# 0x0A - TSL2561T
185+
# 0x02 - TSL2560T
186+
package = 'T'
187+
elif id & 0x0F in (0x08, 0x00):
188+
# 0x08 - TSL2561CS
189+
# 0x00 - TSL2560CS
190+
package = 'CS'
191+
else:
171192
raise EnvironmentError, 'Device at 0x{0:x} does not appear to be a TSL2561.'.format(address)
193+
if self.debug:
194+
print 'Device uses "{0}" packaging.'.format(package)
172195

173196
self.mode = mode
174197
if self.mode == TSL2561.MODE_CONTINUOUS:

0 commit comments

Comments
 (0)