From 2d45429122e61c7292349986e6bc77f4b74d80a0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Jan 2014 22:04:45 +0000 Subject: [PATCH] Use memcpy instead of strncpy; add usart.status to stm. --- py/objstr.c | 2 +- stm/main.c | 20 -------------------- stm/printf.c | 3 +++ stm/usart.c | 11 +++++++++++ 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index 58e8bcf6fd..ea4f5ead24 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -200,7 +200,7 @@ mp_obj_t str_strip(int n_args, const mp_obj_t *args) { size_t stripped_len = last_good_char_pos - first_good_char_pos + 1; //+1 to accomodate '\0' char *stripped_str = m_new(char, stripped_len + 1); - strncpy(stripped_str, orig_str + first_good_char_pos, stripped_len); + memcpy(stripped_str, orig_str + first_good_char_pos, stripped_len); stripped_str[stripped_len] = '\0'; return mp_obj_new_str(qstr_from_str_take(stripped_str, stripped_len + 1)); } diff --git a/stm/main.c b/stm/main.c index 776b0f2ac2..4406cd6964 100644 --- a/stm/main.c +++ b/stm/main.c @@ -294,23 +294,6 @@ static mp_obj_t pyb_standby(void) { return mp_const_none; } -mp_obj_t pyb_usart_send(mp_obj_t data) { - usart_tx_char(mp_obj_get_int(data)); - return mp_const_none; -} - -mp_obj_t pyb_usart_receive(void) { - return mp_obj_new_int(usart_rx_char()); -} - -mp_obj_t pyb_usart_status(void) { - if (usart_rx_any()) { - return mp_const_true; - } else { - return mp_const_false; - } -} - char *strdup(const char *str) { uint32_t len = strlen(str); char *s2 = m_new(char, len + 1); @@ -846,9 +829,6 @@ soft_reset: rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj); rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report)); rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read)); - rt_store_attr(m, qstr_from_str_static("uout"), rt_make_function_1(pyb_usart_send)); - rt_store_attr(m, qstr_from_str_static("uin"), rt_make_function_0(pyb_usart_receive)); - rt_store_attr(m, qstr_from_str_static("ustat"), rt_make_function_0(pyb_usart_status)); rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get)); rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led)); rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo)); diff --git a/stm/printf.c b/stm/printf.c index bd8bb2d256..8a40a61a5f 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -219,10 +219,13 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { void stdout_print_strn(void *data, const char *str, unsigned int len) { // send stdout to USART, USB CDC VCP, and LCD if nothing else bool any = false; + + // TODO should have a setting for which USART port to send to if (usart_is_enabled()) { usart_tx_strn_cooked(str, len); any = true; } + if (usb_vcp_is_enabled()) { usb_vcp_send_strn_cooked(str, len); any = true; diff --git a/stm/usart.c b/stm/usart.c index 05b897dbfa..9adc7d6ae3 100644 --- a/stm/usart.c +++ b/stm/usart.c @@ -175,6 +175,15 @@ typedef struct _pyb_usart_obj_t { bool is_enabled; } pyb_usart_obj_t; +static mp_obj_t usart_obj_status(mp_obj_t self_in) { + // TODO make it check the correct USART port! + if (usart_rx_any()) { + return mp_const_true; + } else { + return mp_const_false; + } +} + static mp_obj_t usart_obj_rx_char(mp_obj_t self_in) { mp_obj_t ret = mp_const_none; pyb_usart_obj_t *self = self_in; @@ -207,11 +216,13 @@ static void usart_obj_print(void (*print)(void *env, const char *fmt, ...), void print(env, "", self->usart_id); } +static MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_status_obj, usart_obj_status); static MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_rx_char_obj, usart_obj_rx_char); static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_char_obj, usart_obj_tx_char); static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_str_obj, usart_obj_tx_str); static const mp_method_t usart_methods[] = { + { "status", &usart_obj_status_obj }, { "recv_chr", &usart_obj_rx_char_obj }, { "send_chr", &usart_obj_tx_char_obj }, { "send", &usart_obj_tx_str_obj },