From 255f9b73509afb09c6d9bea9a4bb373c8bc7a5bf Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Mon, 13 Nov 2023 21:15:08 -0800 Subject: [PATCH] add wait and exit commands to the shell --- app/src/shell/key_positions.c | 46 +++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/app/src/shell/key_positions.c b/app/src/shell/key_positions.c index 12277fa4..26ea96ba 100644 --- a/app/src/shell/key_positions.c +++ b/app/src/shell/key_positions.c @@ -6,25 +6,24 @@ #include #define HELP_NONE "[key_position]" +#define TAP_EVENT_SPACING K_MSEC(50) -static int parse_key_position(const struct shell *shell, char *pos_str) { - int position; +static int parse_positive_int(char *s) { char *endptr; + unsigned long value = strtoul(s, &endptr, 10); - position = strtoul(pos_str, &endptr, 10); - - if (endptr == pos_str) { - shell_error(shell, "Enter an integer key position"); + if (endptr == s) return -EINVAL; - } - - return position; + if (value > INT_MAX) + return -ERANGE; + return value; } static int parse_and_raise(const struct shell *shell, char *pos_str, bool pressed) { - int position = parse_key_position(shell, pos_str); + long position = parse_positive_int(pos_str); if (position < 0) { + shell_error(shell, "Enter an integer key position"); return position; } @@ -34,7 +33,7 @@ static int parse_and_raise(const struct shell *shell, char *pos_str, bool presse .position = position, .timestamp = k_uptime_get()})); - return position; + return 0; } static int cmd_key_tap(const struct shell *shell, size_t argc, char **argv) { @@ -44,7 +43,7 @@ static int cmd_key_tap(const struct shell *shell, size_t argc, char **argv) { parse_and_raise(shell, argv[1], true); - k_sleep(K_MSEC(50)); + k_sleep(TAP_EVENT_SPACING); parse_and_raise(shell, argv[1], false); @@ -71,10 +70,31 @@ static int cmd_key_release(const struct shell *shell, size_t argc, char **argv) return 0; }; +static int cmd_sleep(const struct shell *shell, size_t argc, char **argv) { + if (argc != 2) { + return -EINVAL; + } + + int sleep_ms = parse_positive_int(argv[1]); + + if (sleep_ms < 0) { + shell_error(shell, "Enter a positive number of milliseconds"); + return sleep_ms; + } + + k_sleep(K_MSEC(sleep_ms)); + + return 0; +}; + +static int cmd_exit(const struct shell *shell, size_t argc, char **argv) { exit(0); }; + SHELL_STATIC_SUBCMD_SET_CREATE(sub_key, SHELL_CMD(tap, NULL, HELP_NONE, cmd_key_tap), SHELL_CMD(press, NULL, HELP_NONE, cmd_key_press), SHELL_CMD(release, NULL, HELP_NONE, cmd_key_release), SHELL_SUBCMD_SET_END /* Array terminated. */ ); -SHELL_CMD_REGISTER(key, &sub_key, "Key commands", NULL); \ No newline at end of file +SHELL_CMD_REGISTER(key, &sub_key, "Key commands", NULL); +SHELL_CMD_REGISTER(sleep, NULL, "Sleep (milliseconds)", cmd_sleep); +SHELL_CMD_REGISTER(exit, NULL, "Exit", cmd_exit); \ No newline at end of file