Skip to content

Commit

Permalink
Merge pull request #40 from tcfranks/main
Browse files Browse the repository at this point in the history
Add Missing Type Annotations
  • Loading branch information
tekktrik authored Sep 29, 2022
2 parents 380f369 + 422e02c commit 7929b0b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
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

0 comments on commit 7929b0b

Please sign in to comment.