31
31
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
32
32
#include "shared-bindings/util.h"
33
33
#include "shared-module/adafruit_bus_device/I2CDevice.h"
34
+ #include "shared-bindings/busio/I2C.h"
34
35
35
36
#include "lib/utils/buffer_helper.h"
36
37
#include "lib/utils/context_manager_helpers.h"
@@ -76,7 +77,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type
76
77
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
77
78
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
78
79
79
- busio_i2c_obj_t * i2c = args [ARG_i2c ].u_obj ;
80
+ mp_obj_t * i2c = args [ARG_i2c ].u_obj ;
80
81
81
82
common_hal_adafruit_bus_device_i2cdevice_construct (MP_OBJ_TO_PTR (self ), i2c , args [ARG_device_address ].u_int );
82
83
if (args [ARG_probe ].u_bool == true) {
@@ -107,7 +108,7 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_obj___exit__(size_t n_args, const
107
108
}
108
109
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (adafruit_bus_device_i2cdevice___exit___obj , 4 , 4 , adafruit_bus_device_i2cdevice_obj___exit__ );
109
110
110
- //| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: int = 0 ) -> None:
111
+ //| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: Optional[ int] = None ) -> None:
111
112
//| """Read into ``buf`` from the device. The number of bytes read will be the
112
113
//| length of ``buf``.
113
114
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
@@ -118,22 +119,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit_
118
119
//| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
119
120
//| ...
120
121
//|
121
- STATIC void readinto (adafruit_bus_device_i2cdevice_obj_t * self , mp_obj_t buffer , int32_t start , mp_int_t end ) {
122
- mp_buffer_info_t bufinfo ;
123
- mp_get_buffer_raise (buffer , & bufinfo , MP_BUFFER_WRITE );
124
-
125
- size_t length = bufinfo .len ;
126
- normalize_buffer_bounds (& start , end , & length );
127
- if (length == 0 ) {
128
- mp_raise_ValueError (translate ("Buffer must be at least length 1" ));
129
- }
130
-
131
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto (MP_OBJ_TO_PTR (self ), ((uint8_t * )bufinfo .buf ) + start , length );
132
- if (status != 0 ) {
133
- mp_raise_OSError (status );
134
- }
135
- }
136
-
137
122
STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
138
123
enum { ARG_buffer , ARG_start , ARG_end };
139
124
static const mp_arg_t allowed_args [] = {
@@ -147,12 +132,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_o
147
132
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
148
133
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
149
134
150
- readinto (self , args [ARG_buffer ].u_obj , args [ARG_start ].u_int , args [ARG_end ].u_int );
135
+ mp_obj_t dest [8 ];
136
+ mp_load_method (self -> i2c , MP_QSTR_readfrom_into , dest );
137
+ dest [2 ] = mp_obj_new_int_from_ull (self -> device_address );
138
+ dest [3 ] = args [ARG_buffer ].u_obj ;
139
+ dest [4 ] = mp_obj_new_str ("start" , 5 );
140
+ dest [5 ] = mp_obj_new_int (args [ARG_start ].u_int );
141
+ dest [6 ] = mp_obj_new_str ("end" , 3 );
142
+ dest [7 ] = mp_obj_new_int (args [ARG_end ].u_int );
143
+ mp_call_method_n_kw (2 , 2 , dest );
144
+
151
145
return mp_const_none ;
152
146
}
153
147
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (adafruit_bus_device_i2cdevice_readinto_obj , 2 , adafruit_bus_device_i2cdevice_readinto );
154
148
155
- //| def write(self, buf: ReadableBuffer, *, start: int = 0, end: int = 0 ) -> None:
149
+ //| def write(self, buf: ReadableBuffer, *, start: int = 0, end: Optional[ int] = None ) -> None:
156
150
//| """Write the bytes from ``buffer`` to the device, then transmit a stop bit.
157
151
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
158
152
//| as if ``buffer[start:end]``. This will not cause an allocation like
@@ -163,22 +157,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2,
163
157
//| """
164
158
//| ...
165
159
//|
166
- STATIC void write (adafruit_bus_device_i2cdevice_obj_t * self , mp_obj_t buffer , int32_t start , mp_int_t end , bool transmit_stop_bit ) {
167
- mp_buffer_info_t bufinfo ;
168
- mp_get_buffer_raise (buffer , & bufinfo , MP_BUFFER_READ );
169
-
170
- size_t length = bufinfo .len ;
171
- normalize_buffer_bounds (& start , end , & length );
172
- if (length == 0 ) {
173
- mp_raise_ValueError (translate ("Buffer must be at least length 1" ));
174
- }
175
-
176
- uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write (MP_OBJ_TO_PTR (self ), ((uint8_t * )bufinfo .buf ) + start , length , transmit_stop_bit );
177
- if (status != 0 ) {
178
- mp_raise_OSError (status );
179
- }
180
- }
181
-
182
160
STATIC mp_obj_t adafruit_bus_device_i2cdevice_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
183
161
enum { ARG_buffer , ARG_start , ARG_end };
184
162
static const mp_arg_t allowed_args [] = {
@@ -191,13 +169,22 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_
191
169
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
192
170
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
193
171
194
- write (self , args [ARG_buffer ].u_obj , args [ARG_start ].u_int , args [ARG_end ].u_int , true);
172
+ mp_obj_t dest [8 ];
173
+ mp_load_method (self -> i2c , MP_QSTR_writeto , dest );
174
+ dest [2 ] = mp_obj_new_int_from_ull (self -> device_address );
175
+ dest [3 ] = args [ARG_buffer ].u_obj ;
176
+ dest [4 ] = mp_obj_new_str ("start" , 5 );
177
+ dest [5 ] = mp_obj_new_int (args [ARG_start ].u_int );
178
+ dest [6 ] = mp_obj_new_str ("end" , 3 );
179
+ dest [7 ] = mp_obj_new_int (args [ARG_end ].u_int );
180
+ mp_call_method_n_kw (2 , 2 , dest );
181
+
195
182
return mp_const_none ;
196
183
}
197
184
MP_DEFINE_CONST_FUN_OBJ_KW (adafruit_bus_device_i2cdevice_write_obj , 2 , adafruit_bus_device_i2cdevice_write );
198
185
199
186
200
- //| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: int = 0 , in_start: int = 0, in_end: int = 0 ) -> None:
187
+ //| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: Optional[ int] = None , in_start: int = 0, in_end: Optional[ int] = None ) -> None:
201
188
//| """Write the bytes from ``out_buffer`` to the device, then immediately
202
189
//| reads into ``in_buffer`` from the device. The number of bytes read
203
190
//| will be the length of ``in_buffer``.
@@ -233,9 +220,21 @@ STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args,
233
220
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
234
221
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
235
222
236
- write (self , args [ARG_out_buffer ].u_obj , args [ARG_out_start ].u_int , args [ARG_out_end ].u_int , false);
237
-
238
- readinto (self , args [ARG_in_buffer ].u_obj , args [ARG_in_start ].u_int , args [ARG_in_end ].u_int );
223
+ mp_obj_t dest [13 ];
224
+ mp_load_method (self -> i2c , MP_QSTR_writeto_then_readfrom , dest );
225
+ dest [2 ] = mp_obj_new_int_from_ull (self -> device_address );
226
+ dest [3 ] = args [ARG_out_buffer ].u_obj ;
227
+ dest [4 ] = args [ARG_in_buffer ].u_obj ;
228
+ dest [5 ] = mp_obj_new_str ("out_start" , 9 );
229
+ dest [6 ] = mp_obj_new_int (args [ARG_out_start ].u_int );
230
+ dest [7 ] = mp_obj_new_str ("out_end" , 7 );
231
+ dest [8 ] = mp_obj_new_int (args [ARG_out_end ].u_int );
232
+ dest [9 ] = mp_obj_new_str ("in_start" , 8 );
233
+ dest [10 ] = mp_obj_new_int (args [ARG_in_start ].u_int );
234
+ dest [11 ] = mp_obj_new_str ("in_end" , 6 );
235
+ dest [12 ] = mp_obj_new_int (args [ARG_in_end ].u_int );
236
+
237
+ mp_call_method_n_kw (3 , 4 , dest );
239
238
240
239
return mp_const_none ;
241
240
}
0 commit comments