21
21
22
22
"""
23
23
24
+ try :
25
+ from typing import Optional , Type , List
26
+ from types import TracebackType
27
+ from circuitpython_typing import WriteableBuffer , ReadableBuffer
28
+ from busio import I2C
29
+ except ImportError :
30
+ pass
31
+
24
32
__version__ = "0.0.0-auto.0"
25
33
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Debug_I2C.git"
26
34
@@ -57,35 +65,47 @@ class DebugI2C:
57
65
58
66
"""
59
67
60
- def __init__ (self , i2c ) :
68
+ def __init__ (self , i2c : I2C ) -> None :
61
69
self ._i2c = i2c
62
70
if hasattr (self ._i2c , "writeto_then_readfrom" ):
63
71
self .writeto_then_readfrom = self ._writeto_then_readfrom
64
72
65
- def __enter__ (self ):
73
+ def __enter__ (self ) -> I2C :
66
74
"""
67
75
No-op used in Context Managers.
68
76
"""
69
77
return self ._i2c .__enter__ ()
70
78
71
- def __exit__ (self , exc_type , exc_val , exc_tb ):
79
+ def __exit__ (
80
+ self ,
81
+ exc_type : Optional [Type [type ]],
82
+ exc_val : Optional [BaseException ],
83
+ exc_tb : Optional [TracebackType ],
84
+ ) -> None :
72
85
"""
73
86
Automatically deinitialises the hardware on context exit.
74
87
"""
75
88
return self ._i2c .__exit__ (exc_type , exc_val , exc_tb )
76
89
77
- def deinit (self ):
90
+ def deinit (self ) -> None :
78
91
"""
79
92
Releases control of the underlying I2C hardware so other classes can use it.
80
93
"""
81
94
return self ._i2c .deinit ()
82
95
83
- def readfrom_into (self , address , buffer , * args , start = 0 , end = None ):
96
+ def readfrom_into (
97
+ self ,
98
+ address : int ,
99
+ buffer : WriteableBuffer ,
100
+ * args ,
101
+ start : int = 0 ,
102
+ end : Optional [int ] = None
103
+ ) -> None :
84
104
"""
85
105
Debug version of ``readfrom_into`` that prints the buffer after reading.
86
106
87
107
:param int address: 7-bit device address
88
- :param bytearray buffer: buffer to write into
108
+ :param ~WritableBuffer buffer: buffer to write into
89
109
:param int start: Index to start writing at
90
110
:param int end: Index to write up to but not include
91
111
@@ -95,7 +115,7 @@ def readfrom_into(self, address, buffer, *args, start=0, end=None):
95
115
in_buffer_str = ", " .join ([hex (i ) for i in buffer ])
96
116
print ("\t I2CREAD @ {} ::" .format (hex (address )), in_buffer_str )
97
117
98
- def scan (self ):
118
+ def scan (self ) -> List [ int ] :
99
119
"""
100
120
Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that
101
121
respond.
@@ -105,7 +125,7 @@ def scan(self):
105
125
"""
106
126
return self ._i2c .scan ()
107
127
108
- def try_lock (self ):
128
+ def try_lock (self ) -> bool :
109
129
"""
110
130
Attempts to grab the I2C lock. Returns True on success.
111
131
@@ -114,18 +134,18 @@ def try_lock(self):
114
134
"""
115
135
return self ._i2c .try_lock ()
116
136
117
- def unlock (self ):
137
+ def unlock (self ) -> None :
118
138
"""
119
139
Releases the I2C lock.
120
140
"""
121
141
return self ._i2c .unlock ()
122
142
123
- def writeto (self , address , buffer , * args , ** kwargs ):
143
+ def writeto (self , address : int , buffer : ReadableBuffer , * args , ** kwargs ) -> None :
124
144
"""
125
145
Debug version of ``write`` that prints the buffer before sending.
126
146
127
147
:param int address: 7-bit device address
128
- :param bytearray buffer: buffer containing the bytes to write
148
+ :param ~ReadableBuffer buffer: buffer containing the bytes to write
129
149
:param int start: Index to start writing from
130
150
:param int end: Index to read up to but not include
131
151
:param bool stop: If true, output an I2C stop condition after the
@@ -138,23 +158,22 @@ def writeto(self, address, buffer, *args, **kwargs):
138
158
139
159
def _writeto_then_readfrom (
140
160
self ,
141
- address ,
142
- buffer_out ,
143
- buffer_in ,
161
+ address : int ,
162
+ buffer_out : ReadableBuffer ,
163
+ buffer_in : WriteableBuffer ,
144
164
* args ,
145
- out_start = 0 ,
146
- out_end = None ,
147
- in_start = 0 ,
148
- in_end = None
149
- ):
165
+ out_start : int = 0 ,
166
+ out_end : Optional [ int ] = None ,
167
+ in_start : int = 0 ,
168
+ in_end : Optional [ int ] = None
169
+ ) -> None :
150
170
"""
151
171
Debug version of ``write_readinto`` that prints the ``buffer_out`` before writing and the
152
172
``buffer_in`` after reading.
153
173
154
- :TODO Verify parameter documentation is accurate
155
- :param address: 7-bit device address
156
- :param bytearray buffer_out: Write out the data in this buffer
157
- :param bytearray buffer_in: Read data into this buffer
174
+ :param int address: 7-bit device address
175
+ :param ~ReadableBuffer buffer_out: Write out the data in this buffer
176
+ :param ~WriteableBuffer buffer_in: Read data into this buffer
158
177
:param int out_start: Start of the slice of buffer_out to write out:
159
178
``buffer_out[out_start:out_end]``
160
179
:param int out_end: End of the slice; this index is not included. Defaults to
0 commit comments