busvoodoo: add name and description to mode interface

This commit is contained in:
King Kévin 2018-01-23 23:41:41 +01:00
parent 6b300b5ed9
commit 657df679a3
3 changed files with 89 additions and 17 deletions

View File

@ -36,10 +36,17 @@
#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
#include "busvoodoo_hiz.h" // BusVoodoo HiZ mode
/** all supported BusVoodoo modes */
static struct busvoodoo_mode_t* busvoodoo_modes[] = {
&busvoodoo_hiz_mode,
};
/** current BusVoodoo mode */
static struct busvoodoo_mode_t* busvoodoo_mode = NULL;
/** is mode setup complete */
static bool busvoodoo_mode_complete = false;
size_t putc(char c)
{
@ -80,10 +87,32 @@ bool wait_space(void)
}
}
/** switch BusVoddoo mode
* @param[in] mode mode to switch to
*/
static void switch_mode(struct busvoodoo_mode_t* mode)
{
if (busvoodoo_mode) {
(*busvoodoo_mode->exit)(); // exit current mode
}
busvoodoo_safe_state(); // return to safe state
if (NULL==mode) { // no mode provided
busvoodoo_mode = &busvoodoo_hiz_mode; // use default mode
} else { // mode provided
busvoodoo_mode = mode; // set provided mode a current mode
}
busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, NULL); // start setup
terminal_send(0); // update the terminal prompt
}
/** command to show help
* @param[in] argument no argument required
*/
static void command_help(void* argument);
/** command to select mode
* @param[in] argument mode to select
*/
static void command_mode(void* argument);
/** command to quit current BusVoodoo mode
* @param[in] argument no argument required
*/
@ -108,12 +137,12 @@ static void command_version(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,
'm',
"mode",
"select mode",
MENU_ARGUMENT_STRING,
"[mode]",
&command_mode,
},
{
'q',
@ -155,6 +184,14 @@ static const struct menu_command_t menu_commands[] = {
NULL,
&command_version,
},
{
'h',
"help",
"display help",
MENU_ARGUMENT_NONE,
NULL,
&command_help,
},
};
static void command_help(void* argument)
@ -166,6 +203,31 @@ static void command_help(void* argument)
menu_print_commands(busvoodoo_mode->commands, busvoodoo_mode->commands_nb); // print BusVoodoo mode commands
}
static void command_mode(void* argument)
{
if (NULL==argument || 0==strlen(argument)) { // no mode provided: list all modes
printf("available modes:\n");
for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
printf("%s\t%s\n", busvoodoo_modes[i]->name, busvoodoo_modes[i]->description); // display mode information
}
} else { // mode provided
bool mode_found = false; // to know if we found the matching mode
for (uint8_t i=0; i<LENGTH(busvoodoo_modes); i++) { // go through all modes
if (0==strcmp(argument, busvoodoo_modes[i]->name)) { // check for corresponding mode
(*busvoodoo_mode->exit)(); // exit current mode
busvoodoo_mode = busvoodoo_modes[i]; // set matching mode as current mode
busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, NULL); // start setup
terminal_send(0); // update the terminal prompt
mode_found = true; // remember we found the mode
break; // stop searching for mode
}
}
if (!mode_found) {
printf("unknown mode: %s\n", argument);
}
}
}
static void command_quit(void* argument)
{
(void)argument; // we won't use the argument
@ -234,10 +296,18 @@ static void process_command(char* str)
return;
}
// handle user input
if (!menu_handle_command(str, busvoodoo_mode->commands, busvoodoo_mode->commands_nb)) {
if (!menu_handle_command(str, busvoodoo_global_commands, LENGTH(busvoodoo_global_commands))) {
if (!menu_handle_command(str, menu_commands, LENGTH(menu_commands))) {
printf("command not recognized. enter help to list commands\n");
if (NULL==busvoodoo_mode) { // no mode set
switch_mode(NULL); // set default mode
}
if (!busvoodoo_mode_complete) { // mode setup is not complete
busvoodoo_mode_complete = (*busvoodoo_mode->setup)(&terminal_prefix, str); // continue setup
terminal_send(0); // update the terminal prompt
} else {
if (!menu_handle_command(str, busvoodoo_mode->commands, busvoodoo_mode->commands_nb)) {
if (!menu_handle_command(str, busvoodoo_global_commands, LENGTH(busvoodoo_global_commands))) {
if (!menu_handle_command(str, menu_commands, LENGTH(menu_commands))) {
printf("command not recognized. enter help to list commands\n");
}
}
}
}
@ -277,10 +347,8 @@ void main(void)
terminal_process = &process_command; // set central function to process commands
terminal_setup(); // start terminal
// setup default HiZ mode
busvoodoo_mode = &busvoodoo_hiz_mode; // set HiZ mode as current mode
(*busvoodoo_mode->setup)(&terminal_prefix, NULL); // setup BusVoodoo mode
terminal_send(0); // update the terminal prompt
// setup default mode
switch_mode(NULL);
// main loop
bool action = false; // if an action has been performed don't go to sleep

View File

@ -88,6 +88,8 @@
/** BusVoodoo mode interface */
struct busvoodoo_mode_t {
const char* name;
const char* description;
bool (*setup)(char** prefix, const char* line);
const struct menu_command_t* commands;
const uint8_t commands_nb;

View File

@ -13,7 +13,7 @@
*
*/
/** BusVoodoo high impedance (HiZ) default mode (code)
* @file busvoodoo_hiz.h
* @file busvoodoo_hiz.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
*/
@ -48,7 +48,7 @@
static bool busvoodoo_hiz_setup(char** prefix, const char* line)
{
(void)line; // no configuration is required
*prefix = "HiZ: "; // set command line prefix
*prefix = "HiZ"; // set command line prefix
return true;
}
@ -694,6 +694,8 @@ static const struct menu_command_t busvoodoo_hiz_commands[] = {
};
struct busvoodoo_mode_t busvoodoo_hiz_mode = {
"hiz",
"high impedance",
&busvoodoo_hiz_setup,
busvoodoo_hiz_commands,
LENGTH(busvoodoo_hiz_commands),