39
39
40
40
41
41
//| class I2CDevice:
42
- //| """
43
- //| Represents a single I2C device and manages locking the bus and the device
44
- //| address.
45
- //| :param ~busio.I2C i2c: The I2C bus the device is on
46
- //| :param int device_address: The 7 bit device address
47
- //| :param bool probe: Probe for the device upon object creation, default is true
48
- //| .. note:: This class is **NOT** built into CircuitPython. See
49
- //| :ref:`here for install instructions <bus_device_installation>`.
50
- //| Example:
51
- //| .. code-block:: python
52
- //| import busio
53
- //| from board import *
54
- //| from adafruit_bus_device.i2c_device import I2CDevice
55
- //| with busio.I2C(SCL, SDA) as i2c:
56
- //| device = I2CDevice(i2c, 0x70)
57
- //| bytes_read = bytearray(4)
58
- //| with device:
59
- //| device.readinto(bytes_read)
60
- //| # A second transaction
61
- //| with device:
62
- //| device.write(bytes_read)"""
42
+ //| """I2C Device Manager"""
43
+ //|
44
+ //| def __init__(self, scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 100000, timeout: int = 255) -> None:
45
+ //|
46
+ //| """Represents a single I2C device and manages locking the bus and the device
47
+ //| address.
48
+ //| :param ~busio.I2C i2c: The I2C bus the device is on
49
+ //| :param int device_address: The 7 bit device address
50
+ //| :param bool probe: Probe for the device upon object creation, default is true
51
+ //| .. note:: This class is **NOT** built into CircuitPython. See
52
+ //| :ref:`here for install instructions <bus_device_installation>`.
53
+ //| Example:
54
+ //| .. code-block:: python
55
+ //| import busio
56
+ //| from board import *
57
+ //| from adafruit_bus_device.i2c_device import I2CDevice
58
+ //| with busio.I2C(SCL, SDA) as i2c:
59
+ //| device = I2CDevice(i2c, 0x70)
60
+ //| bytes_read = bytearray(4)
61
+ //| with device:
62
+ //| device.readinto(bytes_read)
63
+ //| # A second transaction
64
+ //| with device:
65
+ //| device.write(bytes_read)"""
63
66
//| ...
64
67
//|
65
68
STATIC mp_obj_t busdevice_i2cdevice_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -84,20 +87,28 @@ STATIC mp_obj_t busdevice_i2cdevice_make_new(const mp_obj_type_t *type, size_t n
84
87
return (mp_obj_t )self ;
85
88
}
86
89
90
+ //| def __enter__(self) -> I2C:
91
+ //| """Context manager entry to lock bus."""
92
+ //| ...
93
+ //|
87
94
STATIC mp_obj_t busdevice_i2cdevice_obj___enter__ (mp_obj_t self_in ) {
88
95
busdevice_i2cdevice_obj_t * self = MP_OBJ_TO_PTR (self_in );
89
96
common_hal_busdevice_i2cdevice_lock (self );
90
97
return self ;
91
98
}
92
99
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (busdevice_i2cdevice___enter___obj , busdevice_i2cdevice_obj___enter__ );
93
100
101
+ //| def __exit__(self) -> None:
102
+ //| """Automatically unlocks the bus on exit."""
103
+ //| ...
104
+ //|
94
105
STATIC mp_obj_t busdevice_i2cdevice_obj___exit__ (size_t n_args , const mp_obj_t * args ) {
95
106
common_hal_busdevice_i2cdevice_unlock (MP_OBJ_TO_PTR (args [0 ]));
96
107
return mp_const_none ;
97
108
}
98
109
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (busdevice_i2cdevice___exit___obj , 4 , 4 , busdevice_i2cdevice_obj___exit__ );
99
110
100
- //| def readinto(self, buf, *, start=0, end=None):
111
+ //| def readinto(self, buf, *, start=0, end=None) -> None :
101
112
//| """
102
113
//| Read into ``buf`` from the device. The number of bytes read will be the
103
114
//| length of ``buf``.
@@ -143,18 +154,17 @@ STATIC mp_obj_t busdevice_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_
143
154
}
144
155
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (busdevice_i2cdevice_readinto_obj , 2 , busdevice_i2cdevice_readinto );
145
156
146
- //| def write(self, buf, *, start=0, end=None):
147
- //| """
148
- //| Write the bytes from ``buffer`` to the device, then transmit a stop
149
- //| bit.
150
- //| If ``start`` or ``end`` is provided, then the buffer will be sliced
151
- //| as if ``buffer[start:end]``. This will not cause an allocation like
152
- //| ``buffer[start:end]`` will so it saves memory.
153
- //| :param bytearray buffer: buffer containing the bytes to write
154
- //| :param int start: Index to start writing from
155
- //| :param int end: Index to read up to but not include; if None, use ``len(buf)``
156
- //| """
157
- //| ...
157
+ //| def write(self, buf, *, start=0, end=None) -> None:
158
+ //| """
159
+ //| Write the bytes from ``buffer`` to the device, then transmit a stop bit.
160
+ //| If ``start`` or ``end`` is provided, then the buffer will be sliced
161
+ //| as if ``buffer[start:end]``. This will not cause an allocation like
162
+ //| ``buffer[start:end]`` will so it saves memory.
163
+ //| :param bytearray buffer: buffer containing the bytes to write
164
+ //| :param int start: Index to start writing from
165
+ //| :param int end: Index to read up to but not include; if None, use ``len(buf)``
166
+ //| """
167
+ //| ...
158
168
//|
159
169
STATIC void write (busdevice_i2cdevice_obj_t * self , mp_obj_t buffer , int32_t start , mp_int_t end ) {
160
170
mp_buffer_info_t bufinfo ;
@@ -190,7 +200,7 @@ STATIC mp_obj_t busdevice_i2cdevice_write(size_t n_args, const mp_obj_t *pos_arg
190
200
MP_DEFINE_CONST_FUN_OBJ_KW (busdevice_i2cdevice_write_obj , 2 , busdevice_i2cdevice_write );
191
201
192
202
193
- //| def write_then_readinto(self, out_buffer, in_buffer, *, out_start=0, out_end=None, in_start=0, in_end=None):
203
+ //| def write_then_readinto(self, out_buffer, in_buffer, *, out_start=0, out_end=None, in_start=0, in_end=None) -> None :
194
204
//| """
195
205
//| Write the bytes from ``out_buffer`` to the device, then immediately
196
206
//| reads into ``in_buffer`` from the device. The number of bytes read
0 commit comments