stmhal: Improve REPL CTRL commands.

This commit is contained in:
Damien George 2014-03-24 12:23:37 +00:00
parent 2f8beb8d88
commit f704e7f20e
3 changed files with 62 additions and 24 deletions

View File

@ -446,7 +446,19 @@ soft_reset:
#endif #endif
#endif #endif
pyexec_repl(); // enter REPL
// REPL mode can change, or it can request a soft reset
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
break;
}
} else {
if (pyexec_friendly_repl() != 0) {
break;
}
}
}
printf("PYB: sync filesystems\n"); printf("PYB: sync filesystems\n");
storage_flush(); storage_flush();

View File

@ -24,7 +24,8 @@
#include "usb.h" #include "usb.h"
#include "usart.h" #include "usart.h"
static bool repl_display_debugging_info = 0; pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
STATIC bool repl_display_debugging_info = 0;
void stdout_tx_str(const char *str) { void stdout_tx_str(const char *str) {
if (pyb_usart_global_debug != PYB_USART_NONE) { if (pyb_usart_global_debug != PYB_USART_NONE) {
@ -279,12 +280,12 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
return ret; return ret;
} }
void pyexec_raw_repl(void) { int pyexec_raw_repl(void) {
vstr_t line; vstr_t line;
vstr_init(&line, 32); vstr_init(&line, 32);
raw_repl_reset: raw_repl_reset:
stdout_tx_str("raw REPL; CTRL-C to exit\r\n"); stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
for (;;) { for (;;) {
vstr_reset(&line); vstr_reset(&line);
@ -292,11 +293,19 @@ raw_repl_reset:
for (;;) { for (;;) {
char c = stdin_rx_chr(); char c = stdin_rx_chr();
if (c == VCP_CHAR_CTRL_A) { if (c == VCP_CHAR_CTRL_A) {
// reset raw REPL
goto raw_repl_reset; goto raw_repl_reset;
} else if (c == VCP_CHAR_CTRL_B) {
// change to friendly REPL
stdout_tx_str("\r\n");
vstr_clear(&line);
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
return 0;
} else if (c == VCP_CHAR_CTRL_C) { } else if (c == VCP_CHAR_CTRL_C) {
// clear line
vstr_reset(&line); vstr_reset(&line);
break;
} else if (c == VCP_CHAR_CTRL_D) { } else if (c == VCP_CHAR_CTRL_D) {
// input finished
break; break;
} else if (c == '\r') { } else if (c == '\r') {
vstr_add_char(&line, '\n'); vstr_add_char(&line, '\n');
@ -305,30 +314,35 @@ raw_repl_reset:
} }
} }
// indicate reception of command
stdout_tx_str("OK"); stdout_tx_str("OK");
if (vstr_len(&line) == 0) { if (line.len == 0) {
// finished // exit for a soft reset
break; stdout_tx_str("\r\n");
vstr_clear(&line);
return 1;
} }
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0); mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line.buf, line.len, 0);
parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false); parse_compile_execute(lex, MP_PARSE_FILE_INPUT, false);
// indicate end of output with EOF character
stdout_tx_str("\004"); stdout_tx_str("\004");
} }
vstr_clear(&line);
stdout_tx_str("\r\n");
} }
void pyexec_repl(void) { int pyexec_friendly_repl(void) {
vstr_t line;
vstr_init(&line, 32);
#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
// in host mode, we enable the LCD for the repl // in host mode, we enable the LCD for the repl
mp_obj_t lcd_o = rt_call_function_0(rt_load_name(qstr_from_str("LCD"))); mp_obj_t lcd_o = rt_call_function_0(rt_load_name(qstr_from_str("LCD")));
rt_call_function_1(rt_load_attr(lcd_o, qstr_from_str("light")), mp_const_true); rt_call_function_1(rt_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
#endif #endif
friendly_repl_reset:
stdout_tx_str("Micro Python build <git hash> on 25/1/2014; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n"); stdout_tx_str("Micro Python build <git hash> on 25/1/2014; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n");
stdout_tx_str("Type \"help()\" for more information.\r\n"); stdout_tx_str("Type \"help()\" for more information.\r\n");
@ -350,22 +364,29 @@ void pyexec_repl(void) {
} }
*/ */
vstr_t line;
vstr_init(&line, 32);
for (;;) { for (;;) {
vstr_reset(&line); vstr_reset(&line);
int ret = readline(&line, ">>> "); int ret = readline(&line, ">>> ");
if (ret == VCP_CHAR_CTRL_A) { if (ret == VCP_CHAR_CTRL_A) {
pyexec_raw_repl(); // change to raw REPL
continue; stdout_tx_str("\r\n");
vstr_clear(&line);
pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
return 0;
} else if (ret == VCP_CHAR_CTRL_B) {
// reset friendly REPL
stdout_tx_str("\r\n");
goto friendly_repl_reset;
} else if (ret == VCP_CHAR_CTRL_C) { } else if (ret == VCP_CHAR_CTRL_C) {
// break
stdout_tx_str("\r\n"); stdout_tx_str("\r\n");
continue; continue;
} else if (ret == VCP_CHAR_CTRL_D) { } else if (ret == VCP_CHAR_CTRL_D) {
// EOF // exit for a soft reset
break; stdout_tx_str("\r\n");
vstr_clear(&line);
return 1;
} else if (vstr_len(&line) == 0) { } else if (vstr_len(&line) == 0) {
continue; continue;
} }
@ -385,8 +406,6 @@ void pyexec_repl(void) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0); mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr_str(&line), vstr_len(&line), 0);
parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, true); parse_compile_execute(lex, MP_PARSE_SINGLE_INPUT, true);
} }
stdout_tx_str("\r\n");
} }
bool pyexec_file(const char *filename) { bool pyexec_file(const char *filename) {

View File

@ -1,5 +1,12 @@
void pyexec_raw_repl(void); typedef enum {
void pyexec_repl(void); PYEXEC_MODE_RAW_REPL,
PYEXEC_MODE_FRIENDLY_REPL,
} pyexec_mode_kind_t;
extern pyexec_mode_kind_t pyexec_mode_kind;
int pyexec_raw_repl(void);
int pyexec_friendly_repl(void);
bool pyexec_file(const char *filename); bool pyexec_file(const char *filename);
MP_DECLARE_CONST_FUN_OBJ(pyb_set_repl_info_obj); MP_DECLARE_CONST_FUN_OBJ(pyb_set_repl_info_obj);