@@ -224,32 +224,53 @@ STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
224
224
MP_DEFINE_CONST_FUN_OBJ_2 (bitbangio_spi_write_obj , bitbangio_spi_write );
225
225
226
226
227
- //| def readinto(self, buf: WriteableBuffer) -> None:
228
- //| """Read into the buffer specified by ``buf`` while writing zeroes.
229
- //| Requires the SPI being locked.
230
- //| If the number of bytes to read is 0, nothing happens."""
227
+ //| def readinto(self, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None, write_value: int = 0) -> None:
228
+ //| """Read into ``buffer`` while writing ``write_value`` for each byte read.
229
+ //| The SPI object must be locked.
230
+ //| If the number of bytes to read is 0, nothing happens.
231
+ //|
232
+ //| :param bytearray buffer: Read data into this buffer
233
+ //| :param int start: Start of the slice of ``buffer`` to read into: ``buffer[start:end]``
234
+ //| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
235
+ //| :param int write_value: Value to write while reading."""
231
236
//| ...
232
237
//|
233
- // TODO(tannewt): Add support for start and end kwargs.
234
- STATIC mp_obj_t bitbangio_spi_readinto (size_t n_args , const mp_obj_t * args ) {
235
- bitbangio_spi_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
238
+
239
+ STATIC mp_obj_t bitbangio_spi_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
240
+ enum { ARG_buffer , ARG_start , ARG_end , ARG_write_value };
241
+ static const mp_arg_t allowed_args [] = {
242
+ { MP_QSTR_buffer , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
243
+ { MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
244
+ { MP_QSTR_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
245
+ { MP_QSTR_write_value ,MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
246
+ };
247
+ bitbangio_spi_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
236
248
check_for_deinit (self );
249
+ check_lock (self );
250
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
251
+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
252
+
237
253
mp_buffer_info_t bufinfo ;
238
- mp_get_buffer_raise (args [1 ], & bufinfo , MP_BUFFER_WRITE );
239
- if (bufinfo .len == 0 ) {
254
+ mp_get_buffer_raise (args [ARG_buffer ].u_obj , & bufinfo , MP_BUFFER_WRITE );
255
+ int32_t start = args [ARG_start ].u_int ;
256
+ size_t length = bufinfo .len ;
257
+ normalize_buffer_bounds (& start , args [ARG_end ].u_int , & length );
258
+
259
+ if (length == 0 ) {
240
260
return mp_const_none ;
241
261
}
242
- check_lock ( args [ 0 ]);
243
- bool ok = shared_module_bitbangio_spi_read (self , bufinfo .buf , bufinfo . len );
262
+
263
+ bool ok = shared_module_bitbangio_spi_read (self , (( uint8_t * ) bufinfo .buf ) + start , length , args [ ARG_write_value ]. u_int );
244
264
if (!ok ) {
245
265
mp_raise_OSError (MP_EIO );
246
266
}
247
267
return mp_const_none ;
248
268
}
249
- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (bitbangio_spi_readinto_obj , 2 , 2 , bitbangio_spi_readinto );
269
+ MP_DEFINE_CONST_FUN_OBJ_KW (bitbangio_spi_readinto_obj , 2 , bitbangio_spi_readinto );
250
270
251
- //| def write_readinto(self, buffer_out: ReadableBuffer, buffer_in: WriteableBuffer , *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
271
+ //| def write_readinto(self, buffer_out: ReadableBuffer, buffer_in: ReadableBuffer , *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
252
272
//| """Write out the data in ``buffer_out`` while simultaneously reading data into ``buffer_in``.
273
+ //| The SPI object must be locked.
253
274
//| The lengths of the slices defined by ``buffer_out[out_start:out_end]`` and ``buffer_in[in_start:in_end]``
254
275
//| must be equal.
255
276
//| If buffer slice lengths are both 0, nothing happens.
@@ -274,6 +295,7 @@ STATIC mp_obj_t bitbangio_spi_write_readinto(size_t n_args, const mp_obj_t *pos_
274
295
};
275
296
bitbangio_spi_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
276
297
check_for_deinit (self );
298
+ check_lock (self );
277
299
278
300
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
279
301
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
0 commit comments