32
32
from adafruit_register .i2c_bits import RWBits
33
33
from micropython import const
34
34
35
+ try :
36
+ from typing import Optional
37
+ from busio import I2C
38
+ except ImportError :
39
+ pass
40
+
35
41
__version__ = "0.0.0+auto.0"
36
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AW9523.git"
37
43
@@ -77,7 +83,9 @@ class AW9523:
77
83
# 256-step constant-current range selector 'ISEL' - choice of 'full' (4/4), 3/4, 2/4, or 1/4.
78
84
constant_current_range = RWBits (2 , _AW9523_REG_GCR , 0 )
79
85
80
- def __init__ (self , i2c_bus , address = _AW9523_DEFAULT_ADDR , reset = True ):
86
+ def __init__ (
87
+ self , i2c_bus : I2C , address : int = _AW9523_DEFAULT_ADDR , reset : bool = True
88
+ ) -> None :
81
89
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
82
90
self ._buffer = bytearray (2 )
83
91
if self ._chip_id != 0x23 :
@@ -88,11 +96,11 @@ def __init__(self, i2c_bus, address=_AW9523_DEFAULT_ADDR, reset=True):
88
96
self .interrupt_enables = 0x0000 # no IRQ
89
97
self .directions = 0x0000 # all inputs!
90
98
91
- def reset (self ):
99
+ def reset (self ) -> None :
92
100
"""Perform a soft reset, check datasheets for post-reset defaults!"""
93
101
self ._reset_reg = 0
94
102
95
- def set_constant_current (self , pin , value ) :
103
+ def set_constant_current (self , pin : int , value : int ) -> None :
96
104
"""
97
105
Set the constant current drain for an AW9523 pin
98
106
:param int pin: pin to set constant current, 0..15
@@ -115,7 +123,7 @@ def set_constant_current(self, pin, value):
115
123
with self .i2c_device as i2c :
116
124
i2c .write (self ._buffer )
117
125
118
- def get_pin (self , pin ) :
126
+ def get_pin (self , pin : int ) -> "DigitalInOut" :
119
127
"""Convenience function to create an instance of the DigitalInOut class
120
128
pointing at the specified pin of this AW9523 device.
121
129
:param int pin: pin to use for digital IO, 0 to 15
@@ -124,30 +132,30 @@ def get_pin(self, pin):
124
132
return DigitalInOut (pin , self )
125
133
126
134
@property
127
- def interrupt_enables (self ):
135
+ def interrupt_enables (self ) -> int :
128
136
"""Enables interrupt for input pin change if bit mask is 1"""
129
137
return ~ self ._interrupt_enables & 0xFFFF
130
138
131
139
@interrupt_enables .setter
132
- def interrupt_enables (self , enables ) :
140
+ def interrupt_enables (self , enables : int ) -> None :
133
141
self ._interrupt_enables = ~ enables & 0xFFFF
134
142
135
143
@property
136
- def directions (self ):
144
+ def directions (self ) -> int :
137
145
"""Direction is output if bit mask is 1, input if bit is 0"""
138
146
return ~ self ._directions & 0xFFFF
139
147
140
148
@directions .setter
141
- def directions (self , dirs ) :
149
+ def directions (self , dirs : int ) -> None :
142
150
self ._directions = (~ dirs ) & 0xFFFF
143
151
144
152
@property
145
- def LED_modes (self ):
153
+ def LED_modes (self ) -> int :
146
154
"""Pin is set up for constant current mode if bit mask is 1"""
147
155
return ~ self ._LED_modes & 0xFFFF
148
156
149
157
@LED_modes .setter
150
- def LED_modes (self , modes ) :
158
+ def LED_modes (self , modes : int ) -> None :
151
159
self ._LED_modes = ~ modes & 0xFFFF
152
160
153
161
@@ -166,15 +174,15 @@ def LED_modes(self, modes):
166
174
"""
167
175
168
176
# Internal helpers to simplify setting and getting a bit inside an integer.
169
- def _get_bit (val , bit ) :
177
+ def _get_bit (val : bool , bit : int ) -> bool :
170
178
return val & (1 << bit ) > 0
171
179
172
180
173
- def _enable_bit (val , bit ) :
181
+ def _enable_bit (val : bool , bit : int ) -> int :
174
182
return val | (1 << bit )
175
183
176
184
177
- def _clear_bit (val , bit ) :
185
+ def _clear_bit (val : bool , bit : int ) -> int :
178
186
return val & ~ (1 << bit )
179
187
180
188
@@ -188,7 +196,7 @@ class DigitalInOut:
188
196
configurations.
189
197
"""
190
198
191
- def __init__ (self , pin_number , aw ) :
199
+ def __init__ (self , pin_number : int , aw : "AW9523" ) -> None :
192
200
"""Specify the pin number of the AW9523 0..15, and instance."""
193
201
self ._pin = pin_number
194
202
self ._aw = aw
@@ -198,14 +206,14 @@ def __init__(self, pin_number, aw):
198
206
# is unused by this class). Do not remove them, instead turn off pylint
199
207
# in this case.
200
208
# pylint: disable=unused-argument
201
- def switch_to_output (self , value = False , ** kwargs ):
209
+ def switch_to_output (self , value : bool = False , ** kwargs ) -> None :
202
210
"""Switch the pin state to a digital output with the provided starting
203
211
value (True/False for high or low, default is False/low).
204
212
"""
205
213
self .direction = digitalio .Direction .OUTPUT
206
214
self .value = value
207
215
208
- def switch_to_input (self , pull = None , ** kwargs ):
216
+ def switch_to_input (self , pull : Optional [ bool ] = None , ** kwargs ) -> None :
209
217
"""Switch the pin state to a digital input with the provided starting
210
218
pull-up resistor state (optional, no pull-up by default) and input polarity. Note that
211
219
pull-down resistors are NOT supported!
@@ -216,22 +224,22 @@ def switch_to_input(self, pull=None, **kwargs):
216
224
# pylint: enable=unused-argument
217
225
218
226
@property
219
- def value (self ):
227
+ def value (self ) -> bool :
220
228
"""The value of the pin, either True for high or False for
221
229
low. Note you must configure as an output or input appropriately
222
230
before reading and writing this value.
223
231
"""
224
232
return _get_bit (self ._aw .inputs , self ._pin )
225
233
226
234
@value .setter
227
- def value (self , val ) :
235
+ def value (self , val : bool ) -> None :
228
236
if val :
229
237
self ._aw .outputs = _enable_bit (self ._aw .outputs , self ._pin )
230
238
else :
231
239
self ._aw .outputs = _clear_bit (self ._aw .outputs , self ._pin )
232
240
233
241
@property
234
- def direction (self ):
242
+ def direction (self ) -> bool :
235
243
"""The direction of the pin, either True for an input or
236
244
False for an output.
237
245
"""
@@ -240,7 +248,7 @@ def direction(self):
240
248
return digitalio .Direction .OUTPUT
241
249
242
250
@direction .setter
243
- def direction (self , val ) :
251
+ def direction (self , val : bool ) -> None :
244
252
if val == digitalio .Direction .INPUT :
245
253
self ._aw .directions = _clear_bit (self ._aw .directions , self ._pin )
246
254
@@ -250,13 +258,13 @@ def direction(self, val):
250
258
raise ValueError ("Expected INPUT or OUTPUT direction!" )
251
259
252
260
@property
253
- def pull (self ):
261
+ def pull (self ) -> None :
254
262
"""
255
263
Pull-down resistors are NOT supported!
256
264
"""
257
265
raise NotImplementedError ("Pull-up/pull-down resistors not supported." )
258
266
259
267
@pull .setter
260
- def pull (self , val ): # pylint: disable=no-self-use
268
+ def pull (self , val ) -> None : # pylint: disable=no-self-use
261
269
if val is not None :
262
270
raise NotImplementedError ("Pull-up/pull-down resistors not supported." )
0 commit comments