add wait and exit commands to the shell

This commit is contained in:
Dylan MacKenzie 2023-11-13 21:15:08 -08:00
parent 42169c5004
commit 255f9b7350

View file

@ -6,25 +6,24 @@
#include <zmk/events/position_state_changed.h> #include <zmk/events/position_state_changed.h>
#define HELP_NONE "[key_position]" #define HELP_NONE "[key_position]"
#define TAP_EVENT_SPACING K_MSEC(50)
static int parse_key_position(const struct shell *shell, char *pos_str) { static int parse_positive_int(char *s) {
int position;
char *endptr; char *endptr;
unsigned long value = strtoul(s, &endptr, 10);
position = strtoul(pos_str, &endptr, 10); if (endptr == s)
if (endptr == pos_str) {
shell_error(shell, "Enter an integer key position");
return -EINVAL; return -EINVAL;
} if (value > INT_MAX)
return -ERANGE;
return position; return value;
} }
static int parse_and_raise(const struct shell *shell, char *pos_str, bool pressed) { 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) { if (position < 0) {
shell_error(shell, "Enter an integer key position");
return position; return position;
} }
@ -34,7 +33,7 @@ static int parse_and_raise(const struct shell *shell, char *pos_str, bool presse
.position = position, .position = position,
.timestamp = k_uptime_get()})); .timestamp = k_uptime_get()}));
return position; return 0;
} }
static int cmd_key_tap(const struct shell *shell, size_t argc, char **argv) { 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); parse_and_raise(shell, argv[1], true);
k_sleep(K_MSEC(50)); k_sleep(TAP_EVENT_SPACING);
parse_and_raise(shell, argv[1], false); parse_and_raise(shell, argv[1], false);
@ -71,6 +70,25 @@ static int cmd_key_release(const struct shell *shell, size_t argc, char **argv)
return 0; 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_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(press, NULL, HELP_NONE, cmd_key_press),
SHELL_CMD(release, NULL, HELP_NONE, cmd_key_release), SHELL_CMD(release, NULL, HELP_NONE, cmd_key_release),
@ -78,3 +96,5 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_key, SHELL_CMD(tap, NULL, HELP_NONE, cmd_key_
); );
SHELL_CMD_REGISTER(key, &sub_key, "Key commands", NULL); 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);