From 14ae66d2066ce8b914a690428fea59ff7af7f04c Mon Sep 17 00:00:00 2001 From: Valentin Ochs Date: Sun, 15 Aug 2021 17:43:29 +0200 Subject: [PATCH] Extend command handling to multi-byte commands --- main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 2d498f0..1a4320b 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "adc.h" #include "buttons.h" @@ -31,6 +32,8 @@ static void erase_app(void); #endif static void fast_reset(void); +static void set_watchdog(int w); +static void set_print_changes(int p); static void sys_tick_setup() { systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1); @@ -61,6 +64,19 @@ void erase_app() { } #endif +int watchdog = 0; +int print_changes = 0; + +void set_watchdog(int w) { + watchdog = w; + printf("watchdog: %d", watchdog); +} + +void set_print_changes(int p) { + print_changes = p; + printf("pos reporting: %d", print_changes); +} + int main(void) { #if 0 rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); @@ -86,9 +102,7 @@ int main(void) { uint32_t last_tick = tick; int last_pos = 0; - int watchdog = 1; uint32_t watchdog_counter = 0; - int print_changes = 0; while (tick < 5000) { usbd_poll(g_usbd_dev); @@ -98,6 +112,10 @@ int main(void) { fast_reset(); } + char cmd_buf[64]; + uint8_t cmd_len = 0; + bool cmd = false; + while (1) { /* Handle control messages through the comms CDC */ char buf[64]; @@ -117,7 +135,37 @@ int main(void) { } for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) { - switch (c) { + if (cmd) { + if (c == '\r' || c == '\n') { + cmd_buf[cmd_len] = 0; + + if (cmd_buf[0] == 'r' && cmd_len == 2 && (cmd_buf[1] == '0' || cmd_buf[1] == '1')) { + set_print_changes(cmd_buf[1] == '1'); + } else if(cmd_buf[0] == 'w' && cmd_len == 2 && (cmd_buf[1] == '0' || cmd_buf[1] == '1')) { + set_watchdog(cmd_buf[1] == '1'); + } else if (!strcmp(cmd_buf, "reset")) { + fast_reset(); + } +#if defined(BOOTLOADER) + else if (!strcmp(cmd_buf, "flash")) { + start_bootloader(); + } else if (!strcmp(cmd_buf, "erase")) { + erase_app(); + } +#endif + else { + printf("unknown command: %s", cmd_buf); + } + cmd = false; + } else if (cmd_len < sizeof cmd_buf - 1) { + cmd_buf[cmd_len++] = c; + } + } + else switch (c) { + case '>': + cmd = true; + cmd_len = 0; + break; case 'B': // Battery printf("%lu", (unsigned long)adc_bat_voltage()); break; @@ -131,12 +179,10 @@ int main(void) { printf("pos: %d %d", encoder_get(), encoder_current_speed()); break; case 'r': // toggle reporting - print_changes = !print_changes; - printf("pos reporting: %d", print_changes); + set_print_changes(!print_changes); break; case 'W': // toggle watchdog - watchdog = !watchdog; - printf("watchdog: %d", watchdog); + set_watchdog(!watchdog); break; case 'S': // reStart fast_reset();