Extend command handling to multi-byte commands

This commit is contained in:
Lynn Ochs 2021-08-15 17:43:29 +02:00
parent 9992331c97
commit 14ae66d206

60
main.c
View File

@ -4,6 +4,7 @@
#include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/rcc.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "adc.h" #include "adc.h"
#include "buttons.h" #include "buttons.h"
@ -31,6 +32,8 @@ static void erase_app(void);
#endif #endif
static void fast_reset(void); static void fast_reset(void);
static void set_watchdog(int w);
static void set_print_changes(int p);
static void sys_tick_setup() { static void sys_tick_setup() {
systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1); systick_set_reload(rcc_ahb_frequency / SYSTICK_FREQUENCY - 1);
@ -61,6 +64,19 @@ void erase_app() {
} }
#endif #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) { int main(void) {
#if 0 #if 0
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
@ -86,9 +102,7 @@ int main(void) {
uint32_t last_tick = tick; uint32_t last_tick = tick;
int last_pos = 0; int last_pos = 0;
int watchdog = 1;
uint32_t watchdog_counter = 0; uint32_t watchdog_counter = 0;
int print_changes = 0;
while (tick < 5000) { while (tick < 5000) {
usbd_poll(g_usbd_dev); usbd_poll(g_usbd_dev);
@ -98,6 +112,10 @@ int main(void) {
fast_reset(); fast_reset();
} }
char cmd_buf[64];
uint8_t cmd_len = 0;
bool cmd = false;
while (1) { while (1) {
/* Handle control messages through the comms CDC */ /* Handle control messages through the comms CDC */
char buf[64]; char buf[64];
@ -117,7 +135,37 @@ int main(void) {
} }
for (char c; ringbuffer_get(comm_in_buf, (void *)&c, 1);) { 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 case 'B': // Battery
printf("%lu", (unsigned long)adc_bat_voltage()); printf("%lu", (unsigned long)adc_bat_voltage());
break; break;
@ -131,12 +179,10 @@ int main(void) {
printf("pos: %d %d", encoder_get(), encoder_current_speed()); printf("pos: %d %d", encoder_get(), encoder_current_speed());
break; break;
case 'r': // toggle reporting case 'r': // toggle reporting
print_changes = !print_changes; set_print_changes(!print_changes);
printf("pos reporting: %d", print_changes);
break; break;
case 'W': // toggle watchdog case 'W': // toggle watchdog
watchdog = !watchdog; set_watchdog(!watchdog);
printf("watchdog: %d", watchdog);
break; break;
case 'S': // reStart case 'S': // reStart
fast_reset(); fast_reset();