Commit 2d454291 authored by Damien George's avatar Damien George
Browse files

Use memcpy instead of strncpy; add usart.status to stm.

parent 0d4cab13
......@@ -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));
}
......
......@@ -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));
......
......@@ -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;
......
......@@ -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, "<Usart %lu>", 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 },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment