modcustomdevices: read all uart if no length given
This makes it convenient to read all available data:
>>> uart.read()
@@ -359,13 +359,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(customdevices_UARTDevice_waiting_obj, customdev
STATIC mp_obj_t customdevices_UARTDevice_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
- PB_ARG_DEFAULT_INT(length, 1)
+ PB_ARG_DEFAULT_NONE(length)
);
customdevices_UARTDevice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
// Get requested data length
- size_t len = mp_obj_get_int(length);
+ size_t len;
+ if (length == mp_const_none) {
+ // If no length is requested, return all pending data
+ pb_assert(pbio_serial_in_waiting(self->serial, &len));
+ }
+ else {
+ // Otherwise, get length directly from argument
+ len = mp_obj_get_int(length);
+ }
+
if (len > UART_MAX_LEN) {
pb_assert(PBIO_ERROR_INVALID_ARG);
}
