Skip to content

Commit 6bbc191

Browse files
authored
Merge pull request #2 from caternuson/iss001
privatized consts, light back to lux, check id
2 parents 51b7cd9 + 5d88334 commit 6bbc191

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Usage Example
3333
>>> i2c = busio.I2C(board.SCL, board.SDA)
3434
>>> import adafruit_tsl2561
3535
>>> tsl = adafruit_tsl2561.TSL2561(i2c)
36-
>>> tsl.light
36+
>>> tsl.lux
3737
3294.37
3838
3939
Contributing

adafruit_tsl2561.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,50 @@
2727
2828
* Author(s): Carter Nelson
2929
"""
30+
from micropython import const
3031
from adafruit_bus_device.i2c_device import I2CDevice
3132

32-
TSL2561_DEFAULT_ADDRESS = const(0x39)
33-
TSL2561_COMMAND_BIT = const(0x80)
34-
TSL2561_WORD_BIT = const(0x20)
33+
_TSL2561_DEFAULT_ADDRESS = const(0x39)
34+
_TSL2561_COMMAND_BIT = const(0x80)
35+
_TSL2561_WORD_BIT = const(0x20)
3536

36-
TSL2561_CONTROL_POWERON = const(0x03)
37-
TSL2561_CONTROL_POWEROFF = const(0x00)
37+
_TSL2561_CONTROL_POWERON = const(0x03)
38+
_TSL2561_CONTROL_POWEROFF = const(0x00)
3839

39-
TSL2561_REGISTER_CONTROL = const(0x00)
40-
TSL2561_REGISTER_TIMING = const(0x01)
41-
TSL2561_REGISTER_CHAN0_LOW = const(0x0C)
42-
TSL2561_REGISTER_CHAN1_LOW = const(0x0E)
43-
TSL2561_REGISTER_ID = const(0x0A)
40+
_TSL2561_REGISTER_CONTROL = const(0x00)
41+
_TSL2561_REGISTER_TIMING = const(0x01)
42+
_TSL2561_REGISTER_CHAN0_LOW = const(0x0C)
43+
_TSL2561_REGISTER_CHAN1_LOW = const(0x0E)
44+
_TSL2561_REGISTER_ID = const(0x0A)
4445

45-
TSL2561_GAIN_SCALE = (16, 1)
46-
TSL2561_TIME_SCALE = (1 / 0.034, 1 / 0.252, 1)
47-
TSL2561_CLIP_THRESHOLD = (4900, 37000, 65000)
46+
_TSL2561_GAIN_SCALE = (16, 1)
47+
_TSL2561_TIME_SCALE = (1 / 0.034, 1 / 0.252, 1)
48+
_TSL2561_CLIP_THRESHOLD = (4900, 37000, 65000)
4849

4950
class TSL2561():
5051
"""Class which provides interface to TSL2561 light sensor."""
5152

52-
def __init__(self, i2c, address=TSL2561_DEFAULT_ADDRESS, **kwargs):
53+
def __init__(self, i2c, address=_TSL2561_DEFAULT_ADDRESS, **kwargs):
5354
self.buf = bytearray(3)
5455
self.i2c_device = I2CDevice(i2c, address)
56+
partno, revno = self.id
57+
# data sheet says TSL2561 = 0001, reality says 0101
58+
if not partno == 5:
59+
raise RuntimeError('Failed to find TSL2561! Part 0x%x Rev 0x%x' % (partno, revno))
5560
self.enabled = True
5661

5762
@property
5863
def id(self):
5964
"""A tuple containing the part number and the revision number."""
60-
id = self._read_register(TSL2561_REGISTER_ID)
65+
id = self._read_register(_TSL2561_REGISTER_ID)
6166
partno = (id >> 4 ) & 0x0f
6267
revno = id & 0x0f
6368
return (partno, revno)
6469

6570
@property
6671
def enabled(self):
6772
"""The state of the sensor."""
68-
return (self._read_register(TSL2561_REGISTER_CONTROL) & 0x03) != 0
73+
return (self._read_register(_TSL2561_REGISTER_CONTROL) & 0x03) != 0
6974

7075
@enabled.setter
7176
def enabled(self, enable):
@@ -76,7 +81,7 @@ def enabled(self, enable):
7681
self._disable()
7782

7883
@property
79-
def light(self):
84+
def lux(self):
8085
"""The computed lux value."""
8186
return self._compute_lux()
8287

@@ -99,31 +104,31 @@ def luminosity(self):
99104
@property
100105
def gain(self):
101106
"""The gain. 0:1x, 1:16x."""
102-
return self._read_register(TSL2561_REGISTER_TIMING) >> 4 & 0x01
107+
return self._read_register(_TSL2561_REGISTER_TIMING) >> 4 & 0x01
103108

104109
@gain.setter
105110
def gain(self, value):
106111
"""Set the gain. 0:1x, 1:16x."""
107112
value &= 0x01
108113
value <<= 4
109-
current = self._read_register(TSL2561_REGISTER_TIMING)
110-
self.buf[0] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING
114+
current = self._read_register(_TSL2561_REGISTER_TIMING)
115+
self.buf[0] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_TIMING
111116
self.buf[1] = (current & 0xef) | value
112117
with self.i2c_device as i2c:
113118
i2c.write(self.buf, end=2)
114119

115120
@property
116121
def integration_time(self):
117122
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
118-
current = self._read_register(TSL2561_REGISTER_TIMING)
123+
current = self._read_register(_TSL2561_REGISTER_TIMING)
119124
return current & 0x03
120125

121126
@integration_time.setter
122127
def integration_time(self, value):
123128
"""Set the integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual."""
124129
value &= 0x03
125-
current = self._read_register(TSL2561_REGISTER_TIMING)
126-
self.buf[0] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING
130+
current = self._read_register(_TSL2561_REGISTER_TIMING)
131+
self.buf[0] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_TIMING
127132
self.buf[1] = (current & 0xfc) | value
128133
with self.i2c_device as i2c:
129134
i2c.write(self.buf, end=2)
@@ -132,8 +137,8 @@ def _compute_lux(self):
132137
"""Based on datasheet for FN package."""
133138
ch0, ch1 = self.luminosity
134139
if ch0 == 0: return None
135-
if ch0 > TSL2561_CLIP_THRESHOLD[self.integration_time]: return None
136-
if ch1 > TSL2561_CLIP_THRESHOLD[self.integration_time]: return None
140+
if ch0 > _TSL2561_CLIP_THRESHOLD[self.integration_time]: return None
141+
if ch1 > _TSL2561_CLIP_THRESHOLD[self.integration_time]: return None
137142
ratio = ch1 / ch0
138143
if ratio > 0 and ratio <= 0.50:
139144
lux = 0.0304 * ch0 - 0.062 * ch0 * ratio**1.4
@@ -149,21 +154,21 @@ def _compute_lux(self):
149154
# is based on 16x gain and 402ms integration time. Need to scale
150155
# result for other settings.
151156
# Scale for gain.
152-
lux *= TSL2561_GAIN_SCALE[self.gain]
157+
lux *= _TSL2561_GAIN_SCALE[self.gain]
153158
# Scale for integration time.
154-
lux *= TSL2561_TIME_SCALE[self.integration_time]
159+
lux *= _TSL2561_TIME_SCALE[self.integration_time]
155160
return lux
156161

157162
def _enable(self):
158-
self._write_control_register(TSL2561_CONTROL_POWERON)
163+
self._write_control_register(_TSL2561_CONTROL_POWERON)
159164

160165
def _disable(self):
161-
self._write_control_register(TSL2561_CONTROL_POWEROFF)
166+
self._write_control_register(_TSL2561_CONTROL_POWEROFF)
162167

163168
def _read_register(self, reg, count=1):
164-
self.buf[0] = TSL2561_COMMAND_BIT | reg
169+
self.buf[0] = _TSL2561_COMMAND_BIT | reg
165170
if count == 2:
166-
self.buf[0] |= TSL2561_WORD_BIT
171+
self.buf[0] |= _TSL2561_WORD_BIT
167172
with self.i2c_device as i2c:
168173
i2c.write(self.buf, end=1, stop=False)
169174
i2c.read_into(self.buf, start=1)
@@ -173,15 +178,15 @@ def _read_register(self, reg, count=1):
173178
return (self.buf[1], self.buf[2])
174179

175180
def _write_control_register(self, reg):
176-
self.buf[0] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL
181+
self.buf[0] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_CONTROL
177182
self.buf[1] = reg
178183
with self.i2c_device as i2c:
179184
i2c.write(self.buf, end=2)
180185

181186
def _read_broadband(self):
182-
low, high = self._read_register(TSL2561_REGISTER_CHAN0_LOW, 2)
187+
low, high = self._read_register(_TSL2561_REGISTER_CHAN0_LOW, 2)
183188
return high << 8 | low
184189

185190
def _read_infrared(self):
186-
low, high = self._read_register(TSL2561_REGISTER_CHAN1_LOW, 2)
191+
low, high = self._read_register(_TSL2561_REGISTER_CHAN1_LOW, 2)
187192
return high << 8 | low

0 commit comments

Comments
 (0)