From f48cf67108121e60560e45ee5f351031fdac7909 Mon Sep 17 00:00:00 2001 From: Damien Date: Mon, 21 Oct 2013 10:42:06 +0100 Subject: [PATCH] Implement crude but working REPL for board. --- stm/main.c | 48 +++++++++++++++++++++++++++--------------------- stm/printf.c | 4 ++-- stm/usb.c | 20 +++++++++++--------- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/stm/main.c b/stm/main.c index 660f42cb31..9f7bda8e6a 100644 --- a/stm/main.c +++ b/stm/main.c @@ -184,32 +184,38 @@ static void board_info() { } char *readline(const char *prompt) { - printf("a\n"); - led_state(PYB_LED_R1, 1); - printf("b\n"); + vstr_t vstr; + vstr_init(&vstr); usb_vcp_send_str(prompt); for (;;) { - printf("c\n"); - led_state(PYB_LED_R2, 1); + //extern int rx_buf_in; + //extern int rx_buf_out; + while (usb_vcp_rx_any() == 0) { + //printf("nope %x %x\n", rx_buf_in, rx_buf_out); + sys_tick_delay_ms(10); + } char c = usb_vcp_rx_get(); - led_state(PYB_LED_R2, 0); - usb_vcp_send_strn(&c, 1); - led_state(PYB_LED_G1, 1); + if (c == 4 && vstr_len(&vstr) == 0) { + return NULL; + } else if (c == '\r') { + usb_vcp_send_str("\r\n"); + return vstr_str(&vstr); + } else if (c == 127) { + if (vstr_len(&vstr) > 0) { + vstr_cut_tail(&vstr, 1); + usb_vcp_send_str("\b \b"); + } + } else if (32 <= c && c <= 126) { + vstr_add_char(&vstr, c); + usb_vcp_send_strn(&c, 1); + } sys_tick_delay_ms(100); - led_state(PYB_LED_G1, 0); } return NULL; } -extern char rx_buf[]; -extern int rx_buf_out; void do_repl() { - int i = 0; - for (;;) { usb_vcp_send_str("Micro Python\r\n"); - printf("%d %d %c\n", i++, usb_vcp_rx_any(), rx_buf[rx_buf_out]); - sys_tick_delay_ms(1000); - } for (;;) { char *line = readline(">>> "); @@ -282,6 +288,11 @@ int main() { qstr_init(); rt_init(); + // add some functions to the python namespace + rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay)); + rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led)); + rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw)); + // print a message printf(" micro py board\n"); @@ -478,11 +489,6 @@ int main() { } else { // execute it! - // add some functions to the python namespace - rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay)); - rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led)); - rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw)); - py_obj_t module_fun = rt_make_function_from_id(1); // flash once diff --git a/stm/printf.c b/stm/printf.c index 39d902d21b..31ab8c3d27 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -209,12 +209,12 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { } void lcd_print_strn(const char *str, unsigned int len); -void usb_vcp_send(const char* str, int len); +void usb_vcp_send_strn(const char* str, int len); void stdout_print_strn(void *data, const char *str, unsigned int len) { // send stdout to LCD and USB CDC VCP lcd_print_strn(str, len); - //usb_vcp_send(str, len); + usb_vcp_send_strn(str, len); } static const pfenv_t pfenv_stdout = {0, stdout_print_strn}; diff --git a/stm/usb.c b/stm/usb.c index 6ac2392a1f..0b88e7bf75 100644 --- a/stm/usb.c +++ b/stm/usb.c @@ -26,15 +26,17 @@ void usb_init() { } void usb_vcp_receive(const char *buf, uint32_t len) { - for (int i = 0; i < len; i++) { - rx_buf[rx_buf_in++] = buf[i]; - if (rx_buf_in >= sizeof(rx_buf)) { - rx_buf_in = 0; - } - if (rx_buf_in == rx_buf_out) { - rx_buf_out = rx_buf_in + 1; - if (rx_buf_out >= sizeof(rx_buf)) { - rx_buf_out = 0; + if (is_enabled) { + for (int i = 0; i < len; i++) { + rx_buf[rx_buf_in++] = buf[i]; + if (rx_buf_in >= sizeof(rx_buf)) { + rx_buf_in = 0; + } + if (rx_buf_in == rx_buf_out) { + rx_buf_out = rx_buf_in + 1; + if (rx_buf_out >= sizeof(rx_buf)) { + rx_buf_out = 0; + } } } }