8000 Merge pull request #5641 from jepler/bitbangio-spi-write-slice · tannewt/circuitpython@39cc38e · GitHub
[go: up one dir, main page]

Skip to content

Commit 39cc38e

Browse files
authored
Merge pull request micropython#5641 from jepler/bitbangio-spi-write-slice
bitbangio.SPI: Handle kwargs like busio.SPI
2 parents 932e699 + 9ee46bb commit 39cc38e

File tree

1 file changed

+22
-9
lines changed
  • shared-bindings/bitbangio

1 file changed

+22
-9
lines changed

shared-bindings/bitbangio/SPI.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,36 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock);
205205
//| If the buffer is empty, nothing happens."""
206206
//| ...
207207
//|
208-
// TODO(tannewt): Add support for start and end kwargs.
209-
STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
210-
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
208+
STATIC mp_obj_t bitbangio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
209+
enum { ARG_buffer, ARG_start, ARG_end };
210+
static const mp_arg_t allowed_args[] = {
211+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
212+
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
213+
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
214+
};
215+
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
211216
check_for_deinit(self);
212-
mp_buffer_info_t src;
213-
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
214-
if (src.len == 0) {
217+
check_lock(self);
218+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
219+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
220+
221+
mp_buffer_info_t bufinfo;
222+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ);
223+
int32_t start = args[ARG_start].u_int;
224+
size_t length = bufinfo.len;
225+
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
226+
227+
if (length == 0) {
215228
return mp_const_none;
216229
}
217-
check_lock(self);
218-
bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len);
230+
231+
bool ok = shared_module_bitbangio_spi_write(self, ((uint8_t *)bufinfo.buf) + start, length);
219232
if (!ok) {
220233
mp_raise_OSError(MP_EIO);
221234
}
222235
return mp_const_none;
223236
}
224-
MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write);
237+
MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_write_obj, 1, bitbangio_spi_write);
225238

226239

227240
//| import sys

0 commit comments

Comments
 (0)
0