Skip to content

Commit 2e29a80

Browse files
authored
Merge pull request #25 from tcfranks/main
Add Missing Type Annotations
2 parents 325a7ce + a189b12 commit 2e29a80

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

adafruit_tsl2591.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
from adafruit_bus_device import i2c_device
3232

33+
try:
34+
from typing import Tuple
35+
from busio import I2C
36+
except ImportError:
37+
pass
3338

3439
__version__ = "0.0.0+auto.0"
3540
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2591.git"
@@ -118,7 +123,7 @@ class TSL2591:
118123
# Note this is NOT thread-safe or re-entrant by design.
119124
_BUFFER = bytearray(2)
120125

121-
def __init__(self, i2c, address=_TSL2591_ADDR):
126+
def __init__(self, i2c: I2C, address: int = _TSL2591_ADDR) -> None:
122127
self._integration_time = 0
123128
self._gain = 0
124129
self._device = i2c_device.I2CDevice(i2c, address)
@@ -131,7 +136,7 @@ def __init__(self, i2c, address=_TSL2591_ADDR):
131136
# Put the device in a powered on state after initialization.
132137
self.enable()
133138

134-
def _read_u8(self, address):
139+
def _read_u8(self, address: int) -> int:
135140
# Read an 8-bit unsigned value from the specified 8-bit address.
136141
with self._device as i2c:
137142
# Make sure to add command bit to read request.
@@ -142,7 +147,7 @@ def _read_u8(self, address):
142147
# Disable invalid name check since pylint isn't smart enough to know LE
143148
# is an abbreviation for little-endian.
144149
# pylint: disable=invalid-name
145-
def _read_u16LE(self, address):
150+
def _read_u16LE(self, address: int) -> int:
146151
# Read a 16-bit little-endian unsigned value from the specified 8-bit
147152
# address.
148153
with self._device as i2c:
@@ -153,15 +158,15 @@ def _read_u16LE(self, address):
153158

154159
# pylint: enable=invalid-name
155160

156-
def _write_u8(self, address, val):
161+
def _write_u8(self, address: int, val: int) -> None:
157162
# Write an 8-bit unsigned value to the specified 8-bit address.
158163
with self._device as i2c:
159164
# Make sure to add command bit to write request.
160165
self._BUFFER[0] = (_TSL2591_COMMAND_BIT | address) & 0xFF
161166
self._BUFFER[1] = val & 0xFF
162167
i2c.write(self._BUFFER, end=2)
163168

164-
def enable(self):
169+
def enable(self) -> None:
165170
"""Put the device in a fully powered enabled mode."""
166171
self._write_u8(
167172
_TSL2591_REGISTER_ENABLE,
@@ -171,12 +176,12 @@ def enable(self):
171176
| _TSL2591_ENABLE_NPIEN,
172177
)
173178

174-
def disable(self):
179+
def disable(self) -> None:
175180
"""Disable the device and go into low power mode."""
176181
self._write_u8(_TSL2591_REGISTER_ENABLE, _TSL2591_ENABLE_POWEROFF)
177182

178183
@property
179-
def gain(self):
184+
def gain(self) -> int:
180185
"""Get and set the gain of the sensor. Can be a value of:
181186
182187
- ``GAIN_LOW`` (1x)
@@ -188,7 +193,7 @@ def gain(self):
188193
return control & 0b00110000
189194

190195
@gain.setter
191-
def gain(self, val):
196+
def gain(self, val: int) -> None:
192197
assert val in (GAIN_LOW, GAIN_MED, GAIN_HIGH, GAIN_MAX)
193198
# Set appropriate gain value.
194199
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
@@ -199,7 +204,7 @@ def gain(self, val):
199204
self._gain = val
200205

201206
@property
202-
def integration_time(self):
207+
def integration_time(self) -> int:
203208
"""Get and set the integration time of the sensor. Can be a value of:
204209
205210
- ``INTEGRATIONTIME_100MS`` (100 millis)
@@ -213,7 +218,7 @@ def integration_time(self):
213218
return control & 0b00000111
214219

215220
@integration_time.setter
216-
def integration_time(self, val):
221+
def integration_time(self, val: int) -> None:
217222
assert 0 <= val <= 5
218223
# Set control bits appropriately.
219224
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
@@ -224,7 +229,7 @@ def integration_time(self, val):
224229
self._integration_time = val
225230

226231
@property
227-
def raw_luminosity(self):
232+
def raw_luminosity(self) -> Tuple[int, int]:
228233
"""Read the raw luminosity from the sensor (both IR + visible and IR
229234
only channels) and return a 2-tuple of those values. The first value
230235
is IR + visible luminosity (channel 0) and the second is the IR only
@@ -236,28 +241,28 @@ def raw_luminosity(self):
236241
return (channel_0, channel_1)
237242

238243
@property
239-
def full_spectrum(self):
244+
def full_spectrum(self) -> int:
240245
"""Read the full spectrum (IR + visible) light and return its value
241246
as a 32-bit unsigned number.
242247
"""
243248
channel_0, channel_1 = self.raw_luminosity
244249
return (channel_1 << 16) | channel_0
245250

246251
@property
247-
def infrared(self):
252+
def infrared(self) -> int:
248253
"""Read the infrared light and return its value as a 16-bit unsigned number."""
249254
_, channel_1 = self.raw_luminosity
250255
return channel_1
251256

252257
@property
253-
def visible(self):
258+
def visible(self) -> int:
254259
"""Read the visible light and return its value as a 32-bit unsigned number."""
255260
channel_0, channel_1 = self.raw_luminosity
256261
full = (channel_1 << 16) | channel_0
257262
return full - channel_1
258263

259264
@property
260-
def lux(self):
265+
def lux(self) -> float:
261266
"""Read the sensor and calculate a lux value from both its infrared
262267
and visible light channels.
263268

0 commit comments

Comments
 (0)