11
11
from typing import Optional , Type
12
12
from types import TracebackType
13
13
from busio import I2C
14
+
15
+ try :
16
+ from circuitpython_typing import ReadableBuffer , WriteableBuffer
17
+ except ImportError :
18
+ from _typing import ReadableBuffer , WriteableBuffer
14
19
except ImportError :
15
20
pass
16
21
@@ -48,15 +53,17 @@ class I2CDevice:
48
53
device.write(bytes_read)
49
54
"""
50
55
51
- def __init__ (self , i2c : I2C , device_address : int , probe : bool = True ):
56
+ def __init__ (self , i2c : I2C , device_address : int , probe : bool = True ) -> None :
52
57
53
58
self .i2c = i2c
54
59
self .device_address = device_address
55
60
56
61
if probe :
57
62
self .__probe_for_device ()
58
63
59
- def readinto (self , buf : bytearray , * , start : int = 0 , end : Optional [int ] = None ):
64
+ def readinto (
65
+ self , buf : WriteableBuffer , * , start : int = 0 , end : Optional [int ] = None
66
+ ) -> None :
60
67
"""
61
68
Read into ``buf`` from the device. The number of bytes read will be the
62
69
length of ``buf``.
@@ -65,15 +72,17 @@ def readinto(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None)
65
72
as if ``buf[start:end]``. This will not cause an allocation like
66
73
``buf[start:end]`` will so it saves memory.
67
74
68
- :param bytearray buffer: buffer to write into
75
+ :param ~WriteableBuffer buffer: buffer to write into
69
76
:param int start: Index to start writing at
70
77
:param int end: Index to write up to but not include; if None, use ``len(buf)``
71
78
"""
72
79
if end is None :
73
80
end = len (buf )
74
81
self .i2c .readfrom_into (self .device_address , buf , start = start , end = end )
75
82
76
- def write (self , buf : bytearray , * , start : int = 0 , end : Optional [int ] = None ):
83
+ def write (
84
+ self , buf : ReadableBuffer , * , start : int = 0 , end : Optional [int ] = None
85
+ ) -> None :
77
86
"""
78
87
Write the bytes from ``buffer`` to the device, then transmit a stop
79
88
bit.
@@ -82,7 +91,7 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
82
91
as if ``buffer[start:end]``. This will not cause an allocation like
83
92
``buffer[start:end]`` will so it saves memory.
84
93
85
- :param bytearray buffer: buffer containing the bytes to write
94
+ :param ~ReadableBuffer buffer: buffer containing the bytes to write
86
95
:param int start: Index to start writing from
87
96
:param int end: Index to read up to but not include; if None, use ``len(buf)``
88
97
"""
@@ -93,14 +102,14 @@ def write(self, buf: bytearray, *, start: int = 0, end: Optional[int] = None):
93
102
# pylint: disable-msg=too-many-arguments
94
103
def write_then_readinto (
95
104
self ,
96
- out_buffer : bytearray ,
97
- in_buffer : bytearray ,
105
+ out_buffer : ReadableBuffer ,
106
+ in_buffer : WriteableBuffer ,
98
107
* ,
99
108
out_start : int = 0 ,
100
109
out_end : Optional [int ] = None ,
101
110
in_start : int = 0 ,
102
111
in_end : Optional [int ] = None
103
- ):
112
+ ) -> None :
104
113
"""
105
114
Write the bytes from ``out_buffer`` to the device, then immediately
106
115
reads into ``in_buffer`` from the device. The number of bytes read
@@ -116,8 +125,8 @@ def write_then_readinto(
116
125
cause an allocation like ``in_buffer[in_start:in_end]`` will so
117
126
it saves memory.
118
127
119
- :param bytearray out_buffer: buffer containing the bytes to write
120
- :param bytearray in_buffer: buffer containing the bytes to read into
128
+ :param ~ReadableBuffer out_buffer: buffer containing the bytes to write
129
+ :param ~WriteableBuffer in_buffer: buffer containing the bytes to read into
121
130
:param int out_start: Index to start writing from
122
131
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
123
132
:param int in_start: Index to start writing at
0 commit comments