Skip to content

Commit 94f8b9e

Browse files
authored
Merge pull request #8 from tcfranks/main
Correct Missing Type Annotations
2 parents 91f6ea4 + c43fe01 commit 94f8b9e

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

adafruit_aw9523.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
from adafruit_register.i2c_bits import RWBits
3333
from micropython import const
3434

35+
try:
36+
from typing import Optional
37+
from busio import I2C
38+
except ImportError:
39+
pass
40+
3541
__version__ = "0.0.0+auto.0"
3642
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AW9523.git"
3743

@@ -77,7 +83,9 @@ class AW9523:
7783
# 256-step constant-current range selector 'ISEL' - choice of 'full' (4/4), 3/4, 2/4, or 1/4.
7884
constant_current_range = RWBits(2, _AW9523_REG_GCR, 0)
7985

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:
8189
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
8290
self._buffer = bytearray(2)
8391
if self._chip_id != 0x23:
@@ -88,11 +96,11 @@ def __init__(self, i2c_bus, address=_AW9523_DEFAULT_ADDR, reset=True):
8896
self.interrupt_enables = 0x0000 # no IRQ
8997
self.directions = 0x0000 # all inputs!
9098

91-
def reset(self):
99+
def reset(self) -> None:
92100
"""Perform a soft reset, check datasheets for post-reset defaults!"""
93101
self._reset_reg = 0
94102

95-
def set_constant_current(self, pin, value):
103+
def set_constant_current(self, pin: int, value: int) -> None:
96104
"""
97105
Set the constant current drain for an AW9523 pin
98106
:param int pin: pin to set constant current, 0..15
@@ -115,7 +123,7 @@ def set_constant_current(self, pin, value):
115123
with self.i2c_device as i2c:
116124
i2c.write(self._buffer)
117125

118-
def get_pin(self, pin):
126+
def get_pin(self, pin: int) -> "DigitalInOut":
119127
"""Convenience function to create an instance of the DigitalInOut class
120128
pointing at the specified pin of this AW9523 device.
121129
:param int pin: pin to use for digital IO, 0 to 15
@@ -124,30 +132,30 @@ def get_pin(self, pin):
124132
return DigitalInOut(pin, self)
125133

126134
@property
127-
def interrupt_enables(self):
135+
def interrupt_enables(self) -> int:
128136
"""Enables interrupt for input pin change if bit mask is 1"""
129137
return ~self._interrupt_enables & 0xFFFF
130138

131139
@interrupt_enables.setter
132-
def interrupt_enables(self, enables):
140+
def interrupt_enables(self, enables: int) -> None:
133141
self._interrupt_enables = ~enables & 0xFFFF
134142

135143
@property
136-
def directions(self):
144+
def directions(self) -> int:
137145
"""Direction is output if bit mask is 1, input if bit is 0"""
138146
return ~self._directions & 0xFFFF
139147

140148
@directions.setter
141-
def directions(self, dirs):
149+
def directions(self, dirs: int) -> None:
142150
self._directions = (~dirs) & 0xFFFF
143151

144152
@property
145-
def LED_modes(self):
153+
def LED_modes(self) -> int:
146154
"""Pin is set up for constant current mode if bit mask is 1"""
147155
return ~self._LED_modes & 0xFFFF
148156

149157
@LED_modes.setter
150-
def LED_modes(self, modes):
158+
def LED_modes(self, modes: int) -> None:
151159
self._LED_modes = ~modes & 0xFFFF
152160

153161

@@ -166,15 +174,15 @@ def LED_modes(self, modes):
166174
"""
167175

168176
# 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:
170178
return val & (1 << bit) > 0
171179

172180

173-
def _enable_bit(val, bit):
181+
def _enable_bit(val: bool, bit: int) -> int:
174182
return val | (1 << bit)
175183

176184

177-
def _clear_bit(val, bit):
185+
def _clear_bit(val: bool, bit: int) -> int:
178186
return val & ~(1 << bit)
179187

180188

@@ -188,7 +196,7 @@ class DigitalInOut:
188196
configurations.
189197
"""
190198

191-
def __init__(self, pin_number, aw):
199+
def __init__(self, pin_number: int, aw: "AW9523") -> None:
192200
"""Specify the pin number of the AW9523 0..15, and instance."""
193201
self._pin = pin_number
194202
self._aw = aw
@@ -198,14 +206,14 @@ def __init__(self, pin_number, aw):
198206
# is unused by this class). Do not remove them, instead turn off pylint
199207
# in this case.
200208
# pylint: disable=unused-argument
201-
def switch_to_output(self, value=False, **kwargs):
209+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
202210
"""Switch the pin state to a digital output with the provided starting
203211
value (True/False for high or low, default is False/low).
204212
"""
205213
self.direction = digitalio.Direction.OUTPUT
206214
self.value = value
207215

208-
def switch_to_input(self, pull=None, **kwargs):
216+
def switch_to_input(self, pull: Optional[bool] = None, **kwargs) -> None:
209217
"""Switch the pin state to a digital input with the provided starting
210218
pull-up resistor state (optional, no pull-up by default) and input polarity. Note that
211219
pull-down resistors are NOT supported!
@@ -216,22 +224,22 @@ def switch_to_input(self, pull=None, **kwargs):
216224
# pylint: enable=unused-argument
217225

218226
@property
219-
def value(self):
227+
def value(self) -> bool:
220228
"""The value of the pin, either True for high or False for
221229
low. Note you must configure as an output or input appropriately
222230
before reading and writing this value.
223231
"""
224232
return _get_bit(self._aw.inputs, self._pin)
225233

226234
@value.setter
227-
def value(self, val):
235+
def value(self, val: bool) -> None:
228236
if val:
229237
self._aw.outputs = _enable_bit(self._aw.outputs, self._pin)
230238
else:
231239
self._aw.outputs = _clear_bit(self._aw.outputs, self._pin)
232240

233241
@property
234-
def direction(self):
242+
def direction(self) -> bool:
235243
"""The direction of the pin, either True for an input or
236244
False for an output.
237245
"""
@@ -240,7 +248,7 @@ def direction(self):
240248
return digitalio.Direction.OUTPUT
241249

242250
@direction.setter
243-
def direction(self, val):
251+
def direction(self, val: bool) -> None:
244252
if val == digitalio.Direction.INPUT:
245253
self._aw.directions = _clear_bit(self._aw.directions, self._pin)
246254

@@ -250,13 +258,13 @@ def direction(self, val):
250258
raise ValueError("Expected INPUT or OUTPUT direction!")
251259

252260
@property
253-
def pull(self):
261+
def pull(self) -> None:
254262
"""
255263
Pull-down resistors are NOT supported!
256264
"""
257265
raise NotImplementedError("Pull-up/pull-down resistors not supported.")
258266

259267
@pull.setter
260-
def pull(self, val): # pylint: disable=no-self-use
268+
def pull(self, val) -> None: # pylint: disable=no-self-use
261269
if val is not None:
262270
raise NotImplementedError("Pull-up/pull-down resistors not supported.")

0 commit comments

Comments
 (0)