application: use menu library

This commit is contained in:
King Kévin 2018-01-21 23:35:42 +01:00
parent afb39f2dd0
commit a822c9b131
1 changed files with 114 additions and 46 deletions

View File

@ -34,6 +34,7 @@
#include "uart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "terminal.h" // handle the terminal interface
#include "menu.h" // menu utilities
#include "busvoodoo_global.h" // BusVoodoo definitions
#include "busvoodoo_hiz.h" // BusVoodoo HiZ mode utilities
@ -55,9 +56,8 @@ size_t putc(char c)
return length; // return number of characters printed
}
static bool wait_space(void)
bool wait_space(void)
{
// disable watchdog when waiting for user input
printf("press space to continue, or any other key to abort\n");
while (!uart_received && !usb_cdcacm_received) { // wait for user input
__WFI(); // go to sleep
@ -77,6 +77,105 @@ static bool wait_space(void)
}
}
/** command to show help
* @param[in] argument no argument required
*/
static void command_help(void* argument);
/** command to switch LED
* @param[in] argument on, off, toggle to switch LED, or NULL to display LED status
*/
static void command_led(void* argument);
/** command to reset board
* @param[in] argument no argument required
*/
static void command_reset(void* argument);
/** command to reboot into bootloader
* @param[in] argument no argument required
*/
static void command_bootloader(void* argument);
/** list of all supported commands */
static const struct menu_command_t menu_commands[] = {
{
'h',
"help",
"display help",
MENU_ARGUMENT_NONE,
NULL,
&command_help,
},
{
'l',
"led",
"switch LED",
MENU_ARGUMENT_STRING,
"[on|off|toggle]",
&command_led,
},
{
'r',
"reset",
"reset board",
MENU_ARGUMENT_NONE,
NULL,
&command_reset,
},
{
'b',
"bootloader",
"reboot into DFU bootloader",
MENU_ARGUMENT_NONE,
NULL,
&command_bootloader,
},
};
static void command_help(void* argument)
{
(void)argument; // we won't use the argument
printf("available commands:\n");
menu_print_commands(menu_commands, LENGTH(menu_commands)); // print global commands
// menu_print_commands(busvoodoo_hiz_commands, LENGTH(busvoodoo_hiz_commands)); // print BusVoodoo HiZ commands
}
static void command_led(void* argument)
{
if (NULL==argument || 0==strlen(argument)) {
printf("LED is ");
if (gpio_get(GPIO(LED_PORT), GPIO(LED_PIN))) {
printf("on\n");
} else {
printf("off\n");
}
} else if (0==strcmp(argument, "on")) {
led_on(); // switch LED on
printf("LED switched on\n"); // notify user
} else if (0==strcmp(argument, "off")) {
led_off(); // switch LED off
printf("LED switched off\n"); // notify user
} else if (0==strcmp(argument, "toggle")) {
led_toggle(); // toggle LED
printf("LED toggled\n"); // notify user
} else {
printf("option malformed: %s\n", argument);
}
}
static void command_reset(void* argument)
{
(void)argument; // we won't use the argument
scb_reset_system(); // reset device
while (true); // wait for the reset to happen
}
static void command_bootloader(void* argument)
{
(void)argument; // we won't use the argument
RCC_CSR |= RCC_CSR_RMVF; // clear reset flags
scb_reset_core(); // reset core (the bootloader will interpret it as starting into DFU)
while (true); // wait for the reset to happen
}
/** process user command
* @param[in] str user command string (\0 ended)
*/
@ -86,6 +185,17 @@ static void process_command(char* str)
if (!str || 0==strlen(str)) {
return;
}
// ensure actions are available
if (NULL==menu_commands || 0==LENGTH(menu_commands)) {
return;
}
// handle user input
// if (!menu_handle_command(str, busvoodoo_hiz_commands, LENGTH(busvoodoo_hiz_commands))) {
if (!menu_handle_command(str, menu_commands, LENGTH(menu_commands))) {
printf("command not recognized. enter help to list commands\n");
}
// }
return;
// split command
const char* delimiter = " ";
char* word = strtok(str,delimiter);
@ -94,32 +204,8 @@ static void process_command(char* str)
}
// parse command
if (0==strcmp(word,"h") || 0==strcmp(word,"help") || 0==strcmp(word,"?")) {
printf("available commands:\n");
printf("l|led [on|off|toggle]\n");
printf("s|self-test\n");
printf("p|pin-test\n");
printf("r|reset\n");
} else if (0==strcmp(word,"l") || 0==strcmp(word,"led")) {
word = strtok(NULL,delimiter);
if (!word) {
printf("LED is ");
if (gpio_get(GPIO(LED_PORT), GPIO(LED_PIN))) {
printf("on\n");
} else {
printf("off\n");
}
} else if (0==strcmp(word,"on")) {
led_on(); // switch LED on
printf("LED switched on\n"); // notify user
} else if (0==strcmp(word,"off")) {
led_off(); // switch LED off
printf("LED switched off\n"); // notify user
} else if (0==strcmp(word,"toggle")) {
led_toggle(); // toggle LED
printf("LED toggled\n"); // notify user
} else {
goto error;
}
printf("s|self-test perform self-test\n");
printf("p|pin-test perform pin test\n");
} else if (0==strcmp(word,"s") || 0==strcmp(word,"self_-test")) {
printf("performing self-test\n");
printf("remove all cables from connectors and press space to start\n");
@ -149,9 +235,6 @@ static void process_command(char* str)
} else {
printf("pin :test aborted\n");
}
} else if (0==strcmp(word,"r") || 0==strcmp(word,"reset")) {
scb_reset_system(); // reset device
while (true); // wait for the reset to happen
} else {
goto error;
}
@ -182,20 +265,6 @@ void main(void)
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
led_blink(0, 1); // switch blue LED on to show firmware is working
/*
#if !(DEBUG)
// show watchdog information
printf("watchdog set to (%.2fs)\n",WATCHDOG_PERIOD/1000.0);
if (FLASH_OBR&FLASH_OBR_OPTERR) {
printf("option bytes not set in flash: software wachtdog used (not started at reset)\n");
} else if (FLASH_OBR&FLASH_OBR_WDG_SW) {
printf("software wachtdog used (not started at reset)\n");
} else {
printf("hardware wachtdog used (started at reset)\n");
}
#endif
*/
busvoodoo_setup(); // setup BusVoodoo board
printf("\nwelcome to BusVoodoo ("); // print welcome message
if (busvoodoo_full) {
@ -206,7 +275,6 @@ void main(void)
printf(" version)\n");
// main loop
printf("command input: ready\n");
terminal_prefix = "BV: "; // set terminal prefix
terminal_process = &process_command;
terminal_setup(); // start terminal