Skip to content

Commit f68b509

Browse files
authored
Merge pull request adafruit#5 from tcfranks/main
Add Missing type annotations
2 parents 41f69cc + cde38e8 commit f68b509

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

adafruit_tca8418.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
from adafruit_register.i2c_bits import ROBits
3434
import digitalio
3535

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

@@ -71,8 +78,13 @@ class TCA8418_register:
7178

7279
# pylint: disable=too-many-arguments
7380
def __init__(
74-
self, tca, base_addr, invert_value=False, read_only=False, initial_value=None
75-
):
81+
self,
82+
tca: "TCA8418",
83+
base_addr: int,
84+
invert_value: bool = False,
85+
read_only: bool = False,
86+
initial_value: Optional[int] = None,
87+
) -> None:
7688
self._tca = tca
7789
self._baseaddr = base_addr
7890
self._invert = invert_value
@@ -84,7 +96,7 @@ def __init__(
8496
self._tca._write_reg(base_addr + 1, initial_value)
8597
self._tca._write_reg(base_addr + 2, initial_value)
8698

87-
def __index__(self):
99+
def __index__(self) -> int:
88100
"""Read all 18 bits of register data and return as one integer"""
89101
val = self._tca._read_reg(self._baseaddr + 2)
90102
val <<= 8
@@ -94,14 +106,14 @@ def __index__(self):
94106
val &= 0x3FFFF
95107
return val
96108

97-
def __getitem__(self, pin_number):
109+
def __getitem__(self, pin_number: int) -> bool:
98110
"""Read the single bit at 'pin_number' offset"""
99111
value = self._tca._get_gpio_register(self._baseaddr, pin_number)
100112
if self._invert:
101113
value = not value
102114
return value
103115

104-
def __setitem__(self, pin_number, value):
116+
def __setitem__(self, pin_number: int, value: bool) -> None:
105117
"""Set a single bit at 'pin_number' offset to 'value'"""
106118
if self._ro:
107119
raise NotImplementedError("Read only register")
@@ -156,7 +168,7 @@ class TCA8418:
156168

157169
# pylint: enable=invalid-name
158170

159-
def __init__(self, i2c_bus, address=TCA8418_I2CADDR_DEFAULT):
171+
def __init__(self, i2c_bus: I2C, address: int = TCA8418_I2CADDR_DEFAULT) -> None:
160172
# pylint: disable=no-member
161173
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
162174
self._buf = bytearray(2)
@@ -214,26 +226,28 @@ def __init__(self, i2c_bus, address=TCA8418_I2CADDR_DEFAULT):
214226
self.gpi_int = False
215227

216228
@property
217-
def next_event(self):
229+
def next_event(self) -> int:
218230
"""The next key event"""
219231

220232
if self.events_count == 0:
221233
raise RuntimeError("No events in FIFO")
222234
return self._read_reg(_TCA8418_REG_KEYEVENT)
223235

224-
def _set_gpio_register(self, reg_base_addr, pin_number, value):
236+
def _set_gpio_register(
237+
self, reg_base_addr: int, pin_number: int, value: bool
238+
) -> None:
225239
if not 0 <= pin_number <= 17:
226240
raise ValueError("Pin number must be between 0 & 17")
227241
reg_base_addr += pin_number // 8
228242
self._set_reg_bit(reg_base_addr, pin_number % 8, value)
229243

230-
def _get_gpio_register(self, reg_base_addr, pin_number):
244+
def _get_gpio_register(self, reg_base_addr: int, pin_number: int) -> bool:
231245
if not 0 <= pin_number <= 17:
232246
raise ValueError("Pin number must be between 0 & 17")
233247
reg_base_addr += pin_number // 8
234248
return self._get_reg_bit(reg_base_addr, pin_number % 8)
235249

236-
def get_pin(self, pin):
250+
def get_pin(self, pin: int) -> "DigitalInOut":
237251
"""Convenience function to create an instance of the DigitalInOut class
238252
pointing at the specified pin of this TCA8418 device.
239253
@@ -245,25 +259,25 @@ def get_pin(self, pin):
245259

246260
# register helpers
247261

248-
def _set_reg_bit(self, addr, bitoffset, value):
262+
def _set_reg_bit(self, addr: int, bitoffset: int, value: bool) -> None:
249263
temp = self._read_reg(addr)
250264
if value:
251265
temp |= 1 << bitoffset
252266
else:
253267
temp &= ~(1 << bitoffset)
254268
self._write_reg(addr, temp)
255269

256-
def _get_reg_bit(self, addr, bitoffset):
270+
def _get_reg_bit(self, addr: int, bitoffset: int) -> bool:
257271
temp = self._read_reg(addr)
258272
return bool(temp & (1 << bitoffset))
259273

260-
def _write_reg(self, addr, val):
274+
def _write_reg(self, addr: int, val: int) -> None:
261275
with self.i2c_device as i2c:
262276
self._buf[0] = addr
263277
self._buf[1] = val
264278
i2c.write(self._buf, end=2)
265279

266-
def _read_reg(self, addr):
280+
def _read_reg(self, addr: int) -> int:
267281
with self.i2c_device as i2c:
268282
self._buf[0] = addr
269283
i2c.write_then_readinto(self._buf, self._buf, out_end=1, in_end=1)
@@ -285,7 +299,7 @@ class DigitalInOut:
285299
:param TCA8418 tca: The TCA8418 object associated with the DIO
286300
"""
287301

288-
def __init__(self, pin_number, tca):
302+
def __init__(self, pin_number: int, tca: "TCA8418") -> None:
289303
"""Specify the pin number of the TCA8418 0..17, and instance."""
290304
self._pin = pin_number
291305
self._tca = tca
@@ -296,14 +310,14 @@ def __init__(self, pin_number, tca):
296310
# is unused by this class). Do not remove them, instead turn off pylint
297311
# in this case.
298312
# pylint: disable=unused-argument
299-
def switch_to_output(self, value=False, **kwargs):
313+
def switch_to_output(self, value: bool = False, **kwargs) -> None:
300314
"""Switch the pin state to a digital output with the provided starting
301315
value (True/False for high or low, default is False/low).
302316
"""
303317
self.direction = digitalio.Direction.OUTPUT
304318
self.value = value
305319

306-
def switch_to_input(self, pull=None, **kwargs):
320+
def switch_to_input(self, pull: Optional[digitalio.Pull] = None, **kwargs) -> None:
307321
"""Switch the pin state to a digital input which is the same as
308322
setting the light pullup on. Note that true tri-state or
309323
pull-down resistors are NOT supported!
@@ -314,25 +328,25 @@ def switch_to_input(self, pull=None, **kwargs):
314328
# pylint: enable=unused-argument
315329

316330
@property
317-
def value(self):
331+
def value(self) -> bool:
318332
"""The value of the pin, either True for high/pulled-up or False for
319333
low.
320334
"""
321335
return self._tca.input_value[self._pin]
322336

323337
@value.setter
324-
def value(self, val):
338+
def value(self, val: bool) -> None:
325339
self._tca.output_value[self._pin] = val
326340

327341
@property
328-
def direction(self):
342+
def direction(self) -> digitalio.Direction:
329343
"""The direction of the pin, works identically to
330344
the one in `digitalio`
331345
"""
332346
return self._dir
333347

334348
@direction.setter
335-
def direction(self, val):
349+
def direction(self, val: digitalio.Direction) -> None:
336350
if val == digitalio.Direction.INPUT:
337351
self._tca.gpio_direction[self._pin] = False
338352
elif val == digitalio.Direction.OUTPUT:
@@ -343,7 +357,7 @@ def direction(self, val):
343357
self._dir = val
344358

345359
@property
346-
def pull(self):
360+
def pull(self) -> Optional[Literal[digitalio.Pull.UP]]:
347361
"""The pull setting for the digital IO, either `digitalio.Pull.UP`
348362
for pull up, or ``None`` for no pull up
349363
"""
@@ -353,7 +367,7 @@ def pull(self):
353367
return None
354368

355369
@pull.setter
356-
def pull(self, val):
370+
def pull(self, val: Optional[Literal[digitalio.Pull.UP]]) -> None:
357371
if val is digitalio.Pull.UP:
358372
# for inputs, turn on the pullup (write high)
359373
self._tca.pullup[self._pin] = True

0 commit comments

Comments
 (0)