Skip to content

Add Missing Type Annotations #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions adafruit_tca9548a.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
import time
from micropython import const

try:
from typing import List
from typing_extensions import Literal
from circuitpython_typing import WriteableBuffer, ReadableBuffer
from busio import I2C
except ImportError:
pass

_DEFAULT_ADDRESS = const(0x70)

__version__ = "0.0.0+auto.0"
Expand All @@ -43,35 +51,41 @@ class TCA9548A_Channel:
of the necessary I2C commands for channel switching. This class needs to
behave like an I2CDevice."""

def __init__(self, tca, channel):
def __init__(self, tca: "TCA9548A", channel: int) -> None:
self.tca = tca
self.channel_switch = bytearray([1 << channel])

def try_lock(self):
def try_lock(self) -> bool:
"""Pass through for try_lock."""
while not self.tca.i2c.try_lock():
time.sleep(0)
self.tca.i2c.writeto(self.tca.address, self.channel_switch)
return True

def unlock(self):
def unlock(self) -> bool:
"""Pass through for unlock."""
self.tca.i2c.writeto(self.tca.address, b"\x00")
return self.tca.i2c.unlock()

def readfrom_into(self, address, buffer, **kwargs):
def readfrom_into(self, address: int, buffer: ReadableBuffer, **kwargs):
"""Pass through for readfrom_into."""
if address == self.tca.address:
raise ValueError("Device address must be different than TCA9548A address.")
return self.tca.i2c.readfrom_into(address, buffer, **kwargs)

def writeto(self, address, buffer, **kwargs):
def writeto(self, address: int, buffer: WriteableBuffer, **kwargs):
"""Pass through for writeto."""
if address == self.tca.address:
raise ValueError("Device address must be different than TCA9548A address.")
return self.tca.i2c.writeto(address, buffer, **kwargs)

def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
def writeto_then_readfrom(
self,
address: int,
buffer_out: WriteableBuffer,
buffer_in: ReadableBuffer,
**kwargs
):
"""Pass through for writeto_then_readfrom."""
# In linux, at least, this is a special kernel function call
if address == self.tca.address:
Expand All @@ -80,23 +94,23 @@ def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
address, buffer_out, buffer_in, **kwargs
)

def scan(self):
def scan(self) -> List[int]:
"""Perform an I2C Device Scan"""
return self.tca.i2c.scan()


class TCA9548A:
"""Class which provides interface to TCA9548A I2C multiplexer."""

def __init__(self, i2c, address=_DEFAULT_ADDRESS):
def __init__(self, i2c: I2C, address: int = _DEFAULT_ADDRESS) -> None:
self.i2c = i2c
self.address = address
self.channels = [None] * 8

def __len__(self):
def __len__(self) -> Literal[8]:
return 8

def __getitem__(self, key):
def __getitem__(self, key: Literal[0, 1, 2, 3, 4, 5, 6, 7]) -> "TCA9548A_Channel":
if not 0 <= key <= 7:
raise IndexError("Channel must be an integer in the range: 0-7.")
if self.channels[key] is None:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

Adafruit-Blinka
adafruit-circuitpython-busdevice
adafruit-circuitpython-typing
typing-extensions~=4.0