32
32
import time
33
33
from micropython import const
34
34
35
+ try :
36
+ from typing import List
37
+ from typing_extensions import Literal
38
+ from circuitpython_typing import WriteableBuffer , ReadableBuffer
39
+ from busio import I2C
40
+ except ImportError :
41
+ pass
42
+
35
43
_DEFAULT_ADDRESS = const (0x70 )
36
44
37
45
__version__ = "0.0.0+auto.0"
@@ -43,35 +51,41 @@ class TCA9548A_Channel:
43
51
of the necessary I2C commands for channel switching. This class needs to
44
52
behave like an I2CDevice."""
45
53
46
- def __init__ (self , tca , channel ) :
54
+ def __init__ (self , tca : "TCA9548A" , channel : int ) -> None :
47
55
self .tca = tca
48
56
self .channel_switch = bytearray ([1 << channel ])
49
57
50
- def try_lock (self ):
58
+ def try_lock (self ) -> bool :
51
59
"""Pass through for try_lock."""
52
60
while not self .tca .i2c .try_lock ():
53
61
time .sleep (0 )
54
62
self .tca .i2c .writeto (self .tca .address , self .channel_switch )
55
63
return True
56
64
57
- def unlock (self ):
65
+ def unlock (self ) -> bool :
58
66
"""Pass through for unlock."""
59
67
self .tca .i2c .writeto (self .tca .address , b"\x00 " )
60
68
return self .tca .i2c .unlock ()
61
69
62
- def readfrom_into (self , address , buffer , ** kwargs ):
70
+ def readfrom_into (self , address : int , buffer : ReadableBuffer , ** kwargs ):
63
71
"""Pass through for readfrom_into."""
64
72
if address == self .tca .address :
65
73
raise ValueError ("Device address must be different than TCA9548A address." )
66
74
return self .tca .i2c .readfrom_into (address , buffer , ** kwargs )
67
75
68
- def writeto (self , address , buffer , ** kwargs ):
76
+ def writeto (self , address : int , buffer : WriteableBuffer , ** kwargs ):
69
77
"""Pass through for writeto."""
70
78
if address == self .tca .address :
71
79
raise ValueError ("Device address must be different than TCA9548A address." )
72
80
return self .tca .i2c .writeto (address , buffer , ** kwargs )
73
81
74
- def writeto_then_readfrom (self , address , buffer_out , buffer_in , ** kwargs ):
82
+ def writeto_then_readfrom (
83
+ self ,
84
+ address : int ,
85
+ buffer_out : WriteableBuffer ,
86
+ buffer_in : ReadableBuffer ,
87
+ ** kwargs
88
+ ):
75
89
"""Pass through for writeto_then_readfrom."""
76
90
# In linux, at least, this is a special kernel function call
77
91
if address == self .tca .address :
@@ -80,23 +94,23 @@ def writeto_then_readfrom(self, address, buffer_out, buffer_in, **kwargs):
80
94
address , buffer_out , buffer_in , ** kwargs
81
95
)
82
96
83
- def scan (self ):
97
+ def scan (self ) -> List [ int ] :
84
98
"""Perform an I2C Device Scan"""
85
99
return self .tca .i2c .scan ()
86
100
87
101
88
102
class TCA9548A :
89
103
"""Class which provides interface to TCA9548A I2C multiplexer."""
90
104
91
- def __init__ (self , i2c , address = _DEFAULT_ADDRESS ):
105
+ def __init__ (self , i2c : I2C , address : int = _DEFAULT_ADDRESS ) -> None :
92
106
self .i2c = i2c
93
107
self .address = address
94
108
self .channels = [None ] * 8
95
109
96
- def __len__ (self ):
110
+ def __len__ (self ) -> Literal [ 8 ] :
97
111
return 8
98
112
99
- def __getitem__ (self , key ) :
113
+ def __getitem__ (self , key : Literal [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ]) -> "TCA9548A_Channel" :
100
114
if not 0 <= key <= 7 :
101
115
raise IndexError ("Channel must be an integer in the range: 0-7." )
102
116
if self .channels [key ] is None :
0 commit comments