Skip to content

Commit f48cf67

Browse files
committed
Implement crude but working REPL for board.
1 parent cbb8868 commit f48cf67

3 files changed

Lines changed: 40 additions & 32 deletions

File tree

stm/main.c

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,32 +184,38 @@ static void board_info() {
184184
}
185185

186186
char *readline(const char *prompt) {
187-
printf("a\n");
188-
led_state(PYB_LED_R1, 1);
189-
printf("b\n");
187+
vstr_t vstr;
188+
vstr_init(&vstr);
190189
usb_vcp_send_str(prompt);
191190
for (;;) {
192-
printf("c\n");
193-
led_state(PYB_LED_R2, 1);
191+
//extern int rx_buf_in;
192+
//extern int rx_buf_out;
193+
while (usb_vcp_rx_any() == 0) {
194+
//printf("nope %x %x\n", rx_buf_in, rx_buf_out);
195+
sys_tick_delay_ms(10);
196+
}
194197
char c = usb_vcp_rx_get();
195-
led_state(PYB_LED_R2, 0);
196-
usb_vcp_send_strn(&c, 1);
197-
led_state(PYB_LED_G1, 1);
198+
if (c == 4 && vstr_len(&vstr) == 0) {
199+
return NULL;
200+
} else if (c == '\r') {
201+
usb_vcp_send_str("\r\n");
202+
return vstr_str(&vstr);
203+
} else if (c == 127) {
204+
if (vstr_len(&vstr) > 0) {
205+
vstr_cut_tail(&vstr, 1);
206+
usb_vcp_send_str("\b \b");
207+
}
208+
} else if (32 <= c && c <= 126) {
209+
vstr_add_char(&vstr, c);
210+
usb_vcp_send_strn(&c, 1);
211+
}
198212
sys_tick_delay_ms(100);
199-
led_state(PYB_LED_G1, 0);
200213
}
201214
return NULL;
202215
}
203216

204-
extern char rx_buf[];
205-
extern int rx_buf_out;
206217
void do_repl() {
207-
int i = 0;
208-
for (;;) {
209218
usb_vcp_send_str("Micro Python\r\n");
210-
printf("%d %d %c\n", i++, usb_vcp_rx_any(), rx_buf[rx_buf_out]);
211-
sys_tick_delay_ms(1000);
212-
}
213219

214220
for (;;) {
215221
char *line = readline(">>> ");
@@ -282,6 +288,11 @@ int main() {
282288
qstr_init();
283289
rt_init();
284290

291+
// add some functions to the python namespace
292+
rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay));
293+
rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led));
294+
rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw));
295+
285296
// print a message
286297
printf(" micro py board\n");
287298

@@ -478,11 +489,6 @@ int main() {
478489
} else {
479490
// execute it!
480491

481-
// add some functions to the python namespace
482-
rt_store_name(qstr_from_str_static("pyb_delay"), rt_make_function_1(pyb_delay));
483-
rt_store_name(qstr_from_str_static("pyb_led"), rt_make_function_1(pyb_led));
484-
rt_store_name(qstr_from_str_static("pyb_sw"), rt_make_function_0(pyb_sw));
485-
486492
py_obj_t module_fun = rt_make_function_from_id(1);
487493

488494
// flash once

stm/printf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
209209
}
210210

211211
void lcd_print_strn(const char *str, unsigned int len);
212-
void usb_vcp_send(const char* str, int len);
212+
void usb_vcp_send_strn(const char* str, int len);
213213

214214
void stdout_print_strn(void *data, const char *str, unsigned int len) {
215215
// send stdout to LCD and USB CDC VCP
216216
lcd_print_strn(str, len);
217-
//usb_vcp_send(str, len);
217+
usb_vcp_send_strn(str, len);
218218
}
219219

220220
static const pfenv_t pfenv_stdout = {0, stdout_print_strn};

stm/usb.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ void usb_init() {
2626
}
2727

2828
void usb_vcp_receive(const char *buf, uint32_t len) {
29-
for (int i = 0; i < len; i++) {
30-
rx_buf[rx_buf_in++] = buf[i];
31-
if (rx_buf_in >= sizeof(rx_buf)) {
32-
rx_buf_in = 0;
33-
}
34-
if (rx_buf_in == rx_buf_out) {
35-
rx_buf_out = rx_buf_in + 1;
36-
if (rx_buf_out >= sizeof(rx_buf)) {
37-
rx_buf_out = 0;
29+
if (is_enabled) {
30+
for (int i = 0; i < len; i++) {
31+
rx_buf[rx_buf_in++] = buf[i];
32+
if (rx_buf_in >= sizeof(rx_buf)) {
33+
rx_buf_in = 0;
34+
}
35+
if (rx_buf_in == rx_buf_out) {
36+
rx_buf_out = rx_buf_in + 1;
37+
if (rx_buf_out >= sizeof(rx_buf)) {
38+
rx_buf_out = 0;
39+
}
3840
}
3941
}
4042
}

0 commit comments

Comments
 (0)