33
33
from adafruit_bus_device .i2c_device import I2CDevice
34
34
from micropython import const
35
35
36
+ try :
37
+ from typing import Optional , Tuple , Union
38
+ from busio import I2C
39
+ except ImportError :
40
+ pass
41
+
36
42
__version__ = "0.0.0+auto.0"
37
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TSL2561.git"
38
44
60
66
class TSL2561 :
61
67
"""Class which provides interface to TSL2561 light sensor."""
62
68
63
- def __init__ (self , i2c , address = _DEFAULT_ADDRESS ):
69
+ def __init__ (self , i2c : I2C , address : int = _DEFAULT_ADDRESS ) -> None :
64
70
self .buf = bytearray (3 )
65
71
self .i2c_device = I2CDevice (i2c , address )
66
72
partno , revno = self .chip_id
67
73
# data sheet says TSL2561 = 0001, reality says 0101
68
74
if not partno == 5 :
69
75
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 )} "
71
77
)
72
78
self .enabled = True
73
79
74
80
@property
75
- def chip_id (self ):
81
+ def chip_id (self ) -> Tuple [ int , int ] :
76
82
"""A tuple containing the part number and the revision number."""
77
83
chip_id = self ._read_register (_REGISTER_ID )
78
84
partno = (chip_id >> 4 ) & 0x0F
79
85
revno = chip_id & 0x0F
80
86
return (partno , revno )
81
87
82
88
@property
83
- def enabled (self ):
89
+ def enabled (self ) -> bool :
84
90
"""The state of the sensor."""
85
91
return (self ._read_register (_REGISTER_CONTROL ) & 0x03 ) != 0
86
92
87
93
@enabled .setter
88
- def enabled (self , enable ) :
94
+ def enabled (self , enable : bool ) -> None :
89
95
"""Enable or disable the sensor."""
90
96
if enable :
91
97
self ._enable ()
92
98
else :
93
99
self ._disable ()
94
100
95
101
@property
96
- def lux (self ):
102
+ def lux (self ) -> Optional [ float ] :
97
103
"""The computed lux value or None when value is not computable."""
98
104
return self ._compute_lux ()
99
105
100
106
@property
101
- def broadband (self ):
107
+ def broadband (self ) -> int :
102
108
"""The broadband channel value."""
103
109
return self ._read_broadband ()
104
110
105
111
@property
106
- def infrared (self ):
112
+ def infrared (self ) -> int :
107
113
"""The infrared channel value."""
108
114
return self ._read_infrared ()
109
115
110
116
@property
111
- def luminosity (self ):
117
+ def luminosity (self ) -> Tuple [ int , int ] :
112
118
"""The overall luminosity as a tuple containing the broadband
113
119
channel and the infrared channel value."""
114
120
return (self .broadband , self .infrared )
115
121
116
122
@property
117
- def gain (self ):
123
+ def gain (self ) -> int :
118
124
"""The gain. 0:1x, 1:16x."""
119
125
return self ._read_register (_REGISTER_TIMING ) >> 4 & 0x01
120
126
121
127
@gain .setter
122
- def gain (self , value ) :
128
+ def gain (self , value : int ) -> None :
123
129
"""Set the gain. 0:1x, 1:16x."""
124
130
value &= 0x01
125
131
value <<= 4
@@ -130,13 +136,13 @@ def gain(self, value):
130
136
i2c .write (self .buf , end = 2 )
131
137
132
138
@property
133
- def integration_time (self ):
139
+ def integration_time (self ) -> int :
134
140
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
135
141
current = self ._read_register (_REGISTER_TIMING )
136
142
return current & 0x03
137
143
138
144
@integration_time .setter
139
- def integration_time (self , value ) :
145
+ def integration_time (self , value : int ) -> None :
140
146
"""Set the integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual."""
141
147
value &= 0x03
142
148
current = self ._read_register (_REGISTER_TIMING )
@@ -146,50 +152,50 @@ def integration_time(self, value):
146
152
i2c .write (self .buf , end = 2 )
147
153
148
154
@property
149
- def threshold_low (self ):
155
+ def threshold_low (self ) -> int :
150
156
"""The low light interrupt threshold level."""
151
157
low , high = self ._read_register (_REGISTER_TH_LOW , 2 )
152
158
return high << 8 | low
153
159
154
160
@threshold_low .setter
155
- def threshold_low (self , value ) :
161
+ def threshold_low (self , value : int ) -> None :
156
162
self .buf [0 ] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_LOW
157
163
self .buf [1 ] = value & 0xFF
158
164
self .buf [2 ] = (value >> 8 ) & 0xFF
159
165
with self .i2c_device as i2c :
160
166
i2c .write (self .buf )
161
167
162
168
@property
163
- def threshold_high (self ):
169
+ def threshold_high (self ) -> int :
164
170
"""The upper light interrupt threshold level."""
165
171
low , high = self ._read_register (_REGISTER_TH_HIGH , 2 )
166
172
return high << 8 | low
167
173
168
174
@threshold_high .setter
169
- def threshold_high (self , value ) :
175
+ def threshold_high (self , value : int ) -> None :
170
176
self .buf [0 ] = _COMMAND_BIT | _WORD_BIT | _REGISTER_TH_HIGH
171
177
self .buf [1 ] = value & 0xFF
172
178
self .buf [2 ] = (value >> 8 ) & 0xFF
173
179
with self .i2c_device as i2c :
174
180
i2c .write (self .buf )
175
181
176
182
@property
177
- def cycles (self ):
183
+ def cycles (self ) -> int :
178
184
"""The number of integration cycles for which an out of bounds
179
185
value must persist to cause an interrupt."""
180
186
value = self ._read_register (_REGISTER_INT_CTRL )
181
187
return value & 0x0F
182
188
183
189
@cycles .setter
184
- def cycles (self , value ) :
190
+ def cycles (self , value : int ) -> None :
185
191
current = self ._read_register (_REGISTER_INT_CTRL )
186
192
self .buf [0 ] = _COMMAND_BIT | _REGISTER_INT_CTRL
187
193
self .buf [1 ] = current | (value & 0x0F )
188
194
with self .i2c_device as i2c :
189
195
i2c .write (self .buf , end = 2 )
190
196
191
197
@property
192
- def interrupt_mode (self ):
198
+ def interrupt_mode (self ) -> int :
193
199
"""The interrupt mode selection.
194
200
195
201
==== =========================
@@ -205,20 +211,20 @@ def interrupt_mode(self):
205
211
return (self ._read_register (_REGISTER_INT_CTRL ) >> 4 ) & 0x03
206
212
207
213
@interrupt_mode .setter
208
- def interrupt_mode (self , value ) :
214
+ def interrupt_mode (self , value : int ) -> None :
209
215
current = self ._read_register (_REGISTER_INT_CTRL )
210
216
self .buf [0 ] = _COMMAND_BIT | _REGISTER_INT_CTRL
211
217
self .buf [1 ] = (current & 0x0F ) | ((value & 0x03 ) << 4 )
212
218
with self .i2c_device as i2c :
213
219
i2c .write (self .buf , end = 2 )
214
220
215
- def clear_interrupt (self ):
221
+ def clear_interrupt (self ) -> None :
216
222
"""Clears any pending interrupt."""
217
223
self .buf [0 ] = 0xC0
218
224
with self .i2c_device as i2c :
219
225
i2c .write (self .buf , end = 1 )
220
226
221
- def _compute_lux (self ):
227
+ def _compute_lux (self ) -> Optional [ float ] :
222
228
"""Based on datasheet for FN package."""
223
229
ch0 , ch1 = self .luminosity
224
230
if ch0 == 0 :
@@ -247,36 +253,34 @@ def _compute_lux(self):
247
253
lux *= _TIME_SCALE [self .integration_time ]
248
254
return lux
249
255
250
- def _enable (self ):
256
+ def _enable (self ) -> None :
251
257
self ._write_control_register (_CONTROL_POWERON )
252
258
253
- def _disable (self ):
259
+ def _disable (self ) -> None :
254
260
self ._write_control_register (_CONTROL_POWEROFF )
255
261
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" )
259
265
self .buf [0 ] = _COMMAND_BIT | reg
260
266
if count == 2 :
261
267
self .buf [0 ] |= _WORD_BIT
262
268
with self .i2c_device as i2c :
263
269
i2c .write_then_readinto (self .buf , self .buf , out_end = 1 , in_start = 1 )
264
270
if count == 1 :
265
271
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 ]
269
273
270
- def _write_control_register (self , reg ) :
274
+ def _write_control_register (self , reg : int ) -> None :
271
275
self .buf [0 ] = _COMMAND_BIT | _REGISTER_CONTROL
272
276
self .buf [1 ] = reg
273
277
with self .i2c_device as i2c :
274
278
i2c .write (self .buf , end = 2 )
275
279
276
- def _read_broadband (self ):
280
+ def _read_broadband (self ) -> int :
277
281
low , high = self ._read_register (_REGISTER_CHAN0_LOW , 2 )
278
282
return high << 8 | low
279
283
280
- def _read_infrared (self ):
284
+ def _read_infrared (self ) -> int :
281
285
low , high = self ._read_register (_REGISTER_CHAN1_LOW , 2 )
282
286
return high << 8 | low
0 commit comments