diff options
author | Damien George <damien.p.george@gmail.com> | 2014-10-24 11:19:01 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-10-24 11:19:01 +0000 |
commit | e294bee45b1dd0e7688ff90f385c8e86052810ed (patch) | |
tree | cfa35667f1fe7d844ae6a07deadfedf501e8e2bc /stmhal/uart.c | |
parent | e5b1b7348a9883bc0d20e19a5c514a87c6229c11 (diff) | |
download | micropython-e294bee45b1dd0e7688ff90f385c8e86052810ed.tar.gz micropython-e294bee45b1dd0e7688ff90f385c8e86052810ed.zip |
stmhal: Use stream's readinto.
Diffstat (limited to 'stmhal/uart.c')
-rw-r--r-- | stmhal/uart.c | 50 |
1 files changed, 2 insertions, 48 deletions
diff --git a/stmhal/uart.c b/stmhal/uart.c index e06ee6b46e..feb033eda5 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -575,53 +575,6 @@ STATIC mp_obj_t pyb_uart_readchar(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_readchar_obj, pyb_uart_readchar); -/// \method readinto(buf, len=-1) -/// -/// Read data on the bus: -/// -/// - `buf` is a mutable buffer which will be filled with read characters. -/// - `len` is the maximum number of characters to read; if negative, uses len(buf). -/// -/// Return value: number of characters stored in buf. -STATIC mp_obj_t pyb_uart_readinto(mp_uint_t n_args, const mp_obj_t *pos_args) { - pyb_uart_obj_t *self = pos_args[0]; - - // get the buffer to read into - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_WRITE); - bufinfo.len >>= self->char_width; - - // adjust the length, if given - if (n_args == 3) { - mp_int_t len = mp_obj_get_int(pos_args[2]); - if (len >= 0 && len < bufinfo.len) { - bufinfo.len = len; - } - } - - // make sure we want at least 1 char, and wait for it to become available - if (bufinfo.len == 0 || !uart_rx_wait(self, self->timeout)) { - return MP_OBJ_NEW_SMALL_INT(0); - } - - // read the chars - byte *buf = bufinfo.buf; - for (;;) { - int data = uart_rx_char(self); - if (self->char_width == CHAR_WIDTH_9BIT) { - *(uint16_t*)buf = data; - buf += 2; - } else { - *buf++ = data; - } - if (--bufinfo.len == 0 || !uart_rx_wait(self, self->timeout_char)) { - // return the number of chars read - return mp_obj_new_int((buf - (byte*)bufinfo.buf) >> self->char_width); - } - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pyb_uart_readinto_obj, 2, pyb_uart_readinto); - STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = { // instance methods @@ -635,12 +588,13 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, /// \method readline() { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, + /// \method readinto(buf[, nbytes]) + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, /// \method write(buf) { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_writechar), (mp_obj_t)&pyb_uart_writechar_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readchar), (mp_obj_t)&pyb_uart_readchar_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&pyb_uart_readinto_obj }, }; STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); |