Skip to content
Snippets Groups Projects
Commit f48cf671 authored by Damien's avatar Damien
Browse files

Implement crude but working REPL for board.

parent cbb8868f
No related branches found
No related tags found
No related merge requests found
...@@ -184,32 +184,38 @@ static void board_info() { ...@@ -184,32 +184,38 @@ static void board_info() {
} }
char *readline(const char *prompt) { char *readline(const char *prompt) {
printf("a\n"); vstr_t vstr;
led_state(PYB_LED_R1, 1); vstr_init(&vstr);
printf("b\n");
usb_vcp_send_str(prompt); usb_vcp_send_str(prompt);
for (;;) { for (;;) {
printf("c\n"); //extern int rx_buf_in;
led_state(PYB_LED_R2, 1); //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(); char c = usb_vcp_rx_get();
led_state(PYB_LED_R2, 0); 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); usb_vcp_send_strn(&c, 1);
led_state(PYB_LED_G1, 1); }
sys_tick_delay_ms(100); sys_tick_delay_ms(100);
led_state(PYB_LED_G1, 0);
} }
return NULL; return NULL;
} }
extern char rx_buf[];
extern int rx_buf_out;
void do_repl() { void do_repl() {
int i = 0;
for (;;) {
usb_vcp_send_str("Micro Python\r\n"); 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 (;;) { for (;;) {
char *line = readline(">>> "); char *line = readline(">>> ");
...@@ -282,6 +288,11 @@ int main() { ...@@ -282,6 +288,11 @@ int main() {
qstr_init(); qstr_init();
rt_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 // print a message
printf(" micro py board\n"); printf(" micro py board\n");
...@@ -478,11 +489,6 @@ int main() { ...@@ -478,11 +489,6 @@ int main() {
} else { } else {
// execute it! // 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); py_obj_t module_fun = rt_make_function_from_id(1);
// flash once // flash once
......
...@@ -209,12 +209,12 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { ...@@ -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 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) { void stdout_print_strn(void *data, const char *str, unsigned int len) {
// send stdout to LCD and USB CDC VCP // send stdout to LCD and USB CDC VCP
lcd_print_strn(str, len); 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}; static const pfenv_t pfenv_stdout = {0, stdout_print_strn};
......
...@@ -26,6 +26,7 @@ void usb_init() { ...@@ -26,6 +26,7 @@ void usb_init() {
} }
void usb_vcp_receive(const char *buf, uint32_t len) { void usb_vcp_receive(const char *buf, uint32_t len) {
if (is_enabled) {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
rx_buf[rx_buf_in++] = buf[i]; rx_buf[rx_buf_in++] = buf[i];
if (rx_buf_in >= sizeof(rx_buf)) { if (rx_buf_in >= sizeof(rx_buf)) {
...@@ -39,6 +40,7 @@ void usb_vcp_receive(const char *buf, uint32_t len) { ...@@ -39,6 +40,7 @@ void usb_vcp_receive(const char *buf, uint32_t len) {
} }
} }
} }
}
int usb_vcp_rx_any() { int usb_vcp_rx_any() {
if (rx_buf_in >= rx_buf_out) { if (rx_buf_in >= rx_buf_out) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment