Skip to content

Commit b6cb1ab

Browse files
authored
Merge pull request #38 from tcfranks/main
Add missing type annotations
2 parents 7a4901f + 3e5e091 commit b6cb1ab

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

adafruit_tsl2561.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_bus_device.i2c_device import I2CDevice
3434
from micropython import const
3535

36+
try:
37+
from typing import Optional, Tuple, Union
38+
from busio import I2C
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0+auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2561.git"
3844

@@ -60,66 +66,66 @@
6066
class TSL2561:
6167
"""Class which provides interface to TSL2561 light sensor."""
6268

63-
def __init__(self, i2c, address=_DEFAULT_ADDRESS):
69+
def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None:
6470
self.buf = bytearray(3)
6571
self.i2c_device = I2CDevice(i2c, address)
6672
partno, revno = self.chip_id
6773
# data sheet says TSL2561 = 0001, reality says 0101
6874
if not partno == 5:
6975
raise RuntimeError(
70-
"Failed to find TSL2561! Part 0x%x Rev 0x%x" % (partno, revno)
76+
f"Failed to find TSL2561! Part {hex(partno)} Rev {hex(revno)}"
7177
)
7278
self.enabled = True
7379

7480
@property
75-
def chip_id(self):
81+
def chip_id(self) -> Tuple[int, int]:
7682
"""A tuple containing the part number and the revision number."""
7783
chip_id = self._read_register(_REGISTER_ID)
7884
partno = (chip_id >> 4) & 0x0F
7985
revno = chip_id & 0x0F
8086
return (partno, revno)
8187

8288
@property
83-
def enabled(self):
89+
def enabled(self) -> bool:
8490
"""The state of the sensor."""
8591
return (self._read_register(_REGISTER_CONTROL) & 0x03) != 0
8692

8793
@enabled.setter
88-
def enabled(self, enable):
94+
def enabled(self, enable: bool) -> None:
8995
"""Enable or disable the sensor."""
9096
if enable:
9197
self._enable()
9298
else:
9399
self._disable()
94100

95101
@property
96-
def lux(self):
102+
def lux(self) -> Optional[float]:
97103
"""The computed lux value or None when value is not computable."""
98104
return self._compute_lux()
99105

100106
@property
101-
def broadband(self):
107+
def broadband(self) -> int:
102108
"""The broadband channel value."""
103109
return self._read_broadband()
104110

105111
@property
106-
def infrared(self):
112+
def infrared(self) -> int:
107113
"""The infrared channel value."""
108114
return self._read_infrared()
109115

110116
@property
111-
def luminosity(self):
117+
def luminosity(self) -> Tuple[int, int]:
112118
"""The overall luminosity as a tuple containing the broadband
113119
channel and the infrared channel value."""
114120
return (self.broadband, self.infrared)
115121

116122
@property
117-
def gain(self):
123+
def gain(self) -> int:
118124
"""The gain. 0:1x, 1:16x."""
119125
return self._read_register(_REGISTER_TIMING) >> 4 & 0x01
120126

121127
@gain.setter
122-
def gain(self, value):
128+
def gain(self, value: int) -> None:
123129
"""Set the gain. 0:1x, 1:16x."""
124130
value &= 0x01
125131
value <<= 4
@@ -130,13 +136,13 @@ def gain(self, value):
130136
i2c.write(self.buf, end=2)
131137

132138
@property
133-
def integration_time(self):
139+
def integration_time(self) -> int:
134140
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
135141
current = self._read_register(_REGISTER_TIMING)
136142
return current & 0x03
137143

138144
@integration_time.setter
139-
def integration_time(self, value):
145+
def integration_time(self, value: int) -> None:
140146
"""Set the integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual."""
141147
value &= 0x03
142148
current = self._read_register(_REGISTER_TIMING)
@@ -146,50 +152,50 @@ def integration_time(self, value):
146152
i2c.write(self.buf, end=2)
147153

148154
@property
149-
def threshold_low(self):
155+
def threshold_low(self) -> int:
150156
"""The low light interrupt threshold level."""
151157
low, high = self._read_register(_REGISTER_TH_LOW, 2)
152158
return high << 8 | low
153159

154160
@threshold_low.setter
155-
def threshold_low(self, value):
161+
def threshold_low(self, value: int) -> None:
156162
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_LOW
157163
self.buf[1] = value & 0xFF
158164
self.buf[2] = (value >> 8) & 0xFF
159165
with self.i2c_device as i2c:
160166
i2c.write(self.buf)
161167

162168
@property
163-
def threshold_high(self):
169+
def threshold_high(self) -> int:
164170
"""The upper light interrupt threshold level."""
165171
low, high = self._read_register(_REGISTER_TH_HIGH, 2)
166172
return high << 8 | low
167173

168174
@threshold_high.setter
169-
def threshold_high(self, value):
175+
def threshold_high(self, value: int) -> None:
170176
self.buf[0] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_HIGH
171177
self.buf[1] = value & 0xFF
172178
self.buf[2] = (value >> 8) & 0xFF
173179
with self.i2c_device as i2c:
174180
i2c.write(self.buf)
175181

176182
@property
177-
def cycles(self):
183+
def cycles(self) -> int:
178184
"""The number of integration cycles for which an out of bounds
179185
value must persist to cause an interrupt."""
180186
value = self._read_register(_REGISTER_INT_CTRL)
181187
return value & 0x0F
182188

183189
@cycles.setter
184-
def cycles(self, value):
190+
def cycles(self, value: int) -> None:
185191
current = self._read_register(_REGISTER_INT_CTRL)
186192
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
187193
self.buf[1] = current | (value & 0x0F)
188194
with self.i2c_device as i2c:
189195
i2c.write(self.buf, end=2)
190196

191197
@property
192-
def interrupt_mode(self):
198+
def interrupt_mode(self) -> int:
193199
"""The interrupt mode selection.
194200
195201
==== =========================
@@ -205,20 +211,20 @@ def interrupt_mode(self):
205211
return (self._read_register(_REGISTER_INT_CTRL) >> 4) & 0x03
206212

207213
@interrupt_mode.setter
208-
def interrupt_mode(self, value):
214+
def interrupt_mode(self, value: int) -> None:
209215
current = self._read_register(_REGISTER_INT_CTRL)
210216
self.buf[0] = _COMMAND_BIT | _REGISTER_INT_CTRL
211217
self.buf[1] = (current & 0x0F) | ((value & 0x03) << 4)
212218
with self.i2c_device as i2c:
213219
i2c.write(self.buf, end=2)
214220

215-
def clear_interrupt(self):
221+
def clear_interrupt(self) -> None:
216222
"""Clears any pending interrupt."""
217223
self.buf[0] = 0xC0
218224
with self.i2c_device as i2c:
219225
i2c.write(self.buf, end=1)
220226

221-
def _compute_lux(self):
227+
def _compute_lux(self) -> Optional[float]:
222228
"""Based on datasheet for FN package."""
223229
ch0, ch1 = self.luminosity
224230
if ch0 == 0:
@@ -247,36 +253,34 @@ def _compute_lux(self):
247253
lux *= _TIME_SCALE[self.integration_time]
248254
return lux
249255

250-
def _enable(self):
256+
def _enable(self) -> None:
251257
self._write_control_register(_CONTROL_POWERON)
252258

253-
def _disable(self):
259+
def _disable(self) -> None:
254260
self._write_control_register(_CONTROL_POWEROFF)
255261

256-
def _read_register(self, reg, count=1):
257-
# pylint: disable=no-else-return
258-
# Disable should be removed when refactor can be tested
262+
def _read_register(self, reg: int, count: int = 1) -> Union[int, Tuple[int, int]]:
263+
if count not in (1, 2):
264+
raise RuntimeError("Can only read up to 2 consecutive registers")
259265
self.buf[0] = _COMMAND_BIT | reg
260266
if count == 2:
261267
self.buf[0] |= _WORD_BIT
262268
with self.i2c_device as i2c:
263269
i2c.write_then_readinto(self.buf, self.buf, out_end=1, in_start=1)
264270
if count == 1:
265271
return self.buf[1]
266-
elif count == 2:
267-
return self.buf[1], self.buf[2]
268-
return None
272+
return self.buf[1], self.buf[2]
269273

270-
def _write_control_register(self, reg):
274+
def _write_control_register(self, reg: int) -> None:
271275
self.buf[0] = _COMMAND_BIT | _REGISTER_CONTROL
272276
self.buf[1] = reg
273277
with self.i2c_device as i2c:
274278
i2c.write(self.buf, end=2)
275279

276-
def _read_broadband(self):
280+
def _read_broadband(self) -> int:
277281
low, high = self._read_register(_REGISTER_CHAN0_LOW, 2)
278282
return high << 8 | low
279283

280-
def _read_infrared(self):
284+
def _read_infrared(self) -> int:
281285
low, high = self._read_register(_REGISTER_CHAN1_LOW, 2)
282286
return high << 8 | low

0 commit comments

Comments
 (0)