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