CLI welcome at connection

This commit is contained in:
whowechina 2024-06-18 11:32:05 +08:00
parent f59fecdbb3
commit 8c84a99134
2 changed files with 46 additions and 21 deletions

View File

@ -56,11 +56,12 @@ int cli_match_prefix(const char *str[], int num, const char *prefix)
return match;
}
const char *built_time = __DATE__ " " __TIME__;
static void handle_help(int argc, char *argv[])
{
printf("%s", cli_logo);
printf("\tSN: %016llx\n", board_id_64());
printf("\tBuilt: %s %s\n\n", __DATE__, __TIME__);
printf("\tBuilt: %s\n\n", built_time);
printf("Available commands:\n");
for (int i = 0; i < num_commands; i++) {
printf("%*s: %s\n", max_cmd_len + 2, commands[i], helps[i]);
@ -146,10 +147,33 @@ static void process_cmd()
void cli_run()
{
static bool was_connected = false;
static uint64_t connect_time = 0;
static bool welcomed = false;
bool connected = stdio_usb_connected();
bool just_connected = connected && !was_connected;
was_connected = connected;
if (!connected) {
return;
}
if (just_connected) {
connect_time = time_us_64();
welcomed = false;
return;
}
if (!welcomed && (time_us_64() - connect_time > 200000)) {
welcomed = true;
cmd_len = 0;
handle_help(0, NULL);
printf("\n%s", cli_prompt);
}
int c = getchar_timeout_us(0);
if (c == EOF) {
return;
}
if (c == 0) {
return;
}
if (c == '\b' || c == 127) { // both backspace and delete
if (cmd_len > 0) {

View File

@ -1,20 +1,21 @@
/*
* Chu Controller Command Line Framework
* WHowe <github.com/whowechina>
*/
#ifndef CLI_H
#define CLI_H
typedef void (*cmd_handler_t)(int argc, char *argv[]);
void cli_init(const char *prompt, const char *logo);
void cli_register(const char *cmd, cmd_handler_t handler, const char *help);
void cli_run();
void cli_fps_count(int core);
int cli_extract_non_neg_int(const char *param, int len);
int cli_match_prefix(const char *str[], int num, const char *prefix);
#endif
/*
* Chu Controller Command Line Framework
* WHowe <github.com/whowechina>
*/
#ifndef CLI_H
#define CLI_H
typedef void (*cmd_handler_t)(int argc, char *argv[]);
void cli_init(const char *prompt, const char *logo);
void cli_register(const char *cmd, cmd_handler_t handler, const char *help);
void cli_run();
void cli_fps_count(int core);
int cli_extract_non_neg_int(const char *param, int len);
int cli_match_prefix(const char *str[], int num, const char *prefix);
extern const char *built_time;
#endif