Skip to content

Commit 3c8331e

Browse files
committed
Correct Missing Type Annotations
1 parent 9a45f64 commit 3c8331e

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

adafruit_aw9523.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2021 ladyada for Adafruit
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 ladyada for Adafruit
23
#
34
# SPDX-License-Identifier: MIT
45
"""
@@ -32,6 +33,12 @@
3233
from adafruit_register.i2c_bits import RWBits
3334
from micropython import const
3435

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

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

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:
8190
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
8291
self._buffer = bytearray(2)
8392
if self._chip_id != 0x23:
@@ -88,11 +97,11 @@ def __init__(self, i2c_bus, address=_AW9523_DEFAULT_ADDR, reset=True):
8897
self.interrupt_enables = 0x0000 # no IRQ
8998
self.directions = 0x0000 # all inputs!
9099

91-
def reset(self):
100+
def reset(self) -> None:
92101
"""Perform a soft reset, check datasheets for post-reset defaults!"""
93102
self._reset_reg = 0
94103

95-
def set_constant_current(self, pin, value):
104+
def set_constant_current(self, pin: int, value: int) -> None:
96105
"""
97106
Set the constant current drain for an AW9523 pin
98107
:param int pin: pin to set constant current, 0..15
@@ -115,7 +124,7 @@ def set_constant_current(self, pin, value):
115124
with self.i2c_device as i2c:
116125
i2c.write(self._buffer)
117126

118-
def get_pin(self, pin):
127+
def get_pin(self, pin: int) -> "DigitalInOut":
119128
"""Convenience function to create an instance of the DigitalInOut class
120129
pointing at the specified pin of this AW9523 device.
121130
:param int pin: pin to use for digital IO, 0 to 15
@@ -124,30 +133,30 @@ def get_pin(self, pin):
124133
return DigitalInOut(pin, self)
125134

126135
@property
127-
def interrupt_enables(self):
136+
def interrupt_enables(self) -> int:
128137
"""Enables interrupt for input pin change if bit mask is 1"""
129138
return ~self._interrupt_enables & 0xFFFF
130139

131140
@interrupt_enables.setter
132-
def interrupt_enables(self, enables):
141+
def interrupt_enables(self, enables: int) -> None:
133142
self._interrupt_enables = ~enables & 0xFFFF
134143

135144
@property
136-
def directions(self):
145+
def directions(self) -> int:
137146
"""Direction is output if bit mask is 1, input if bit is 0"""
138147
return ~self._directions & 0xFFFF
139148

140149
@directions.setter
141-
def directions(self, dirs):
150+
def directions(self, dirs) -> None:
142151
self._directions = (~dirs) & 0xFFFF
143152

144153
@property
145-
def LED_modes(self):
154+
def LED_modes(self) -> int:
146155
"""Pin is set up for constant current mode if bit mask is 1"""
147156
return ~self._LED_modes & 0xFFFF
148157

149158
@LED_modes.setter
150-
def LED_modes(self, modes):
159+
def LED_modes(self, modes) -> None:
151160
self._LED_modes = ~modes & 0xFFFF
152161

153162

@@ -166,15 +175,15 @@ def LED_modes(self, modes):
166175
"""
167176

168177
# 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:
170179
return val & (1 << bit) > 0
171180

172181

173-
def _enable_bit(val, bit):
182+
def _enable_bit(val: bool, bit: int) -> int:
174183
return val | (1 << bit)
175184

176185

177-
def _clear_bit(val, bit):
186+
def _clear_bit(val: bool, bit: int) -> int:
178187
return val & ~(1 << bit)
179188

180189

@@ -188,7 +197,7 @@ class DigitalInOut:
188197
configurations.
189198
"""
190199

191-
def __init__(self, pin_number, aw):
200+
def __init__(self, pin_number: int, aw: "AW9523") -> None:
192201
"""Specify the pin number of the AW9523 0..15, and instance."""
193202
self._pin = pin_number
194203
self._aw = aw
@@ -198,14 +207,14 @@ def __init__(self, pin_number, aw):
198207
# is unused by this class). Do not remove them, instead turn off pylint
199208
# in this case.
200209
# pylint: disable=unused-argument
201-
def switch_to_output(self, value=False, **kwargs):
210+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
202211
"""Switch the pin state to a digital output with the provided starting
203212
value (True/False for high or low, default is False/low).
204213
"""
205214
self.direction = digitalio.Direction.OUTPUT
206215
self.value = value
207216

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

218227
@property
219-
def value(self):
228+
def value(self) -> bool:
220229
"""The value of the pin, either True for high or False for
221230
low. Note you must configure as an output or input appropriately
222231
before reading and writing this value.
223232
"""
224233
return _get_bit(self._aw.inputs, self._pin)
225234

226235
@value.setter
227-
def value(self, val):
236+
def value(self, val: bool) -> None:
228237
if val:
229238
self._aw.outputs = _enable_bit(self._aw.outputs, self._pin)
230239
else:
231240
self._aw.outputs = _clear_bit(self._aw.outputs, self._pin)
232241

233242
@property
234-
def direction(self):
243+
def direction(self) -> bool:
235244
"""The direction of the pin, either True for an input or
236245
False for an output.
237246
"""
@@ -240,7 +249,7 @@ def direction(self):
240249
return digitalio.Direction.OUTPUT
241250

242251
@direction.setter
243-
def direction(self, val):
252+
def direction(self, val: bool) -> None:
244253
if val == digitalio.Direction.INPUT:
245254
self._aw.directions = _clear_bit(self._aw.directions, self._pin)
246255

@@ -250,13 +259,13 @@ def direction(self, val):
250259
raise ValueError("Expected INPUT or OUTPUT direction!")
251260

252261
@property
253-
def pull(self):
262+
def pull(self) -> None:
254263
"""
255264
Pull-down resistors are NOT supported!
256265
"""
257266
raise NotImplementedError("Pull-up/pull-down resistors not supported.")
258267

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

0 commit comments

Comments
 (0)