30
30
31
31
from adafruit_bus_device import i2c_device
32
32
33
+ try :
34
+ from typing import Tuple
35
+ from busio import I2C
36
+ except ImportError :
37
+ pass
33
38
34
39
__version__ = "0.0.0+auto.0"
35
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2591.git"
@@ -118,7 +123,7 @@ class TSL2591:
118
123
# Note this is NOT thread-safe or re-entrant by design.
119
124
_BUFFER = bytearray (2 )
120
125
121
- def __init__ (self , i2c , address = _TSL2591_ADDR ):
126
+ def __init__ (self , i2c : I2C , address : int = _TSL2591_ADDR ) -> None :
122
127
self ._integration_time = 0
123
128
self ._gain = 0
124
129
self ._device = i2c_device .I2CDevice (i2c , address )
@@ -131,7 +136,7 @@ def __init__(self, i2c, address=_TSL2591_ADDR):
131
136
# Put the device in a powered on state after initialization.
132
137
self .enable ()
133
138
134
- def _read_u8 (self , address ) :
139
+ def _read_u8 (self , address : int ) -> int :
135
140
# Read an 8-bit unsigned value from the specified 8-bit address.
136
141
with self ._device as i2c :
137
142
# Make sure to add command bit to read request.
@@ -142,7 +147,7 @@ def _read_u8(self, address):
142
147
# Disable invalid name check since pylint isn't smart enough to know LE
143
148
# is an abbreviation for little-endian.
144
149
# pylint: disable=invalid-name
145
- def _read_u16LE (self , address ) :
150
+ def _read_u16LE (self , address : int ) -> int :
146
151
# Read a 16-bit little-endian unsigned value from the specified 8-bit
147
152
# address.
148
153
with self ._device as i2c :
@@ -153,15 +158,15 @@ def _read_u16LE(self, address):
153
158
154
159
# pylint: enable=invalid-name
155
160
156
- def _write_u8 (self , address , val ) :
161
+ def _write_u8 (self , address : int , val : int ) -> None :
157
162
# Write an 8-bit unsigned value to the specified 8-bit address.
158
163
with self ._device as i2c :
159
164
# Make sure to add command bit to write request.
160
165
self ._BUFFER [0 ] = (_TSL2591_COMMAND_BIT | address ) & 0xFF
161
166
self ._BUFFER [1 ] = val & 0xFF
162
167
i2c .write (self ._BUFFER , end = 2 )
163
168
164
- def enable (self ):
169
+ def enable (self ) -> None :
165
170
"""Put the device in a fully powered enabled mode."""
166
171
self ._write_u8 (
167
172
_TSL2591_REGISTER_ENABLE ,
@@ -171,12 +176,12 @@ def enable(self):
171
176
| _TSL2591_ENABLE_NPIEN ,
172
177
)
173
178
174
- def disable (self ):
179
+ def disable (self ) -> None :
175
180
"""Disable the device and go into low power mode."""
176
181
self ._write_u8 (_TSL2591_REGISTER_ENABLE , _TSL2591_ENABLE_POWEROFF )
177
182
178
183
@property
179
- def gain (self ):
184
+ def gain (self ) -> int :
180
185
"""Get and set the gain of the sensor. Can be a value of:
181
186
182
187
- ``GAIN_LOW`` (1x)
@@ -188,7 +193,7 @@ def gain(self):
188
193
return control & 0b00110000
189
194
190
195
@gain .setter
191
- def gain (self , val ) :
196
+ def gain (self , val : int ) -> None :
192
197
assert val in (GAIN_LOW , GAIN_MED , GAIN_HIGH , GAIN_MAX )
193
198
# Set appropriate gain value.
194
199
control = self ._read_u8 (_TSL2591_REGISTER_CONTROL )
@@ -199,7 +204,7 @@ def gain(self, val):
199
204
self ._gain = val
200
205
201
206
@property
202
- def integration_time (self ):
207
+ def integration_time (self ) -> int :
203
208
"""Get and set the integration time of the sensor. Can be a value of:
204
209
205
210
- ``INTEGRATIONTIME_100MS`` (100 millis)
@@ -213,7 +218,7 @@ def integration_time(self):
213
218
return control & 0b00000111
214
219
215
220
@integration_time .setter
216
- def integration_time (self , val ) :
221
+ def integration_time (self , val : int ) -> None :
217
222
assert 0 <= val <= 5
218
223
# Set control bits appropriately.
219
224
control = self ._read_u8 (_TSL2591_REGISTER_CONTROL )
@@ -224,7 +229,7 @@ def integration_time(self, val):
224
229
self ._integration_time = val
225
230
226
231
@property
227
- def raw_luminosity (self ):
232
+ def raw_luminosity (self ) -> Tuple [ int , int ] :
228
233
"""Read the raw luminosity from the sensor (both IR + visible and IR
229
234
only channels) and return a 2-tuple of those values. The first value
230
235
is IR + visible luminosity (channel 0) and the second is the IR only
@@ -236,28 +241,28 @@ def raw_luminosity(self):
236
241
return (channel_0 , channel_1 )
237
242
238
243
@property
239
- def full_spectrum (self ):
244
+ def full_spectrum (self ) -> int :
240
245
"""Read the full spectrum (IR + visible) light and return its value
241
246
as a 32-bit unsigned number.
242
247
"""
243
248
channel_0 , channel_1 = self .raw_luminosity
244
249
return (channel_1 << 16 ) | channel_0
245
250
246
251
@property
247
- def infrared (self ):
252
+ def infrared (self ) -> int :
248
253
"""Read the infrared light and return its value as a 16-bit unsigned number."""
249
254
_ , channel_1 = self .raw_luminosity
250
255
return channel_1
251
256
252
257
@property
253
- def visible (self ):
258
+ def visible (self ) -> int :
254
259
"""Read the visible light and return its value as a 32-bit unsigned number."""
255
260
channel_0 , channel_1 = self .raw_luminosity
256
261
full = (channel_1 << 16 ) | channel_0
257
262
return full - channel_1
258
263
259
264
@property
260
- def lux (self ):
265
+ def lux (self ) -> float :
261
266
"""Read the sensor and calculate a lux value from both its infrared
262
267
and visible light channels.
263
268
0 commit comments