Skip to content

Commit

Permalink
CLI welcome at connection
Browse files Browse the repository at this point in the history
  • Loading branch information
whowechina committed Jun 18, 2024
1 parent f59fecd commit 8c84a99
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
26 changes: 25 additions & 1 deletion firmware/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 21 additions & 20 deletions firmware/src/cli.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c84a99

Please sign in to comment.