feat(shell): Add new `key` shell command.

* Allow tap/press/release of a given key position
  within the Zephyr shell.
This commit is contained in:
Peter Johanson 2022-05-26 17:00:03 +00:00
parent 118359c83e
commit cd27866c95
5 changed files with 92 additions and 0 deletions

View File

@ -107,6 +107,8 @@ target_sources_ifdef(CONFIG_ZMK_LOW_PRIORITY_WORK_QUEUE app PRIVATE src/workqueu
target_sources(app PRIVATE src/main.c)
add_subdirectory(src/display/)
add_subdirectory(src/shell/)
add_subdirectory_ifdef(CONFIG_SETTINGS src/settings/)
if (CONFIG_ZMK_STUDIO_RPC)

View File

@ -474,6 +474,7 @@ endmenu
menu "Advanced"
rsource "src/boot/Kconfig"
rsource "src/shell/Kconfig"
menu "Initialization Priorities"

View File

@ -0,0 +1 @@
target_sources_ifdef(CONFIG_ZMK_SHELL_KEY_POSITIONS app PRIVATE key_positions.c)

8
app/src/shell/Kconfig Normal file
View File

@ -0,0 +1,8 @@
menu "Shell"
config ZMK_SHELL_KEY_POSITIONS
bool "Key Position commands"
default y if !ZMK_SPLIT || ZMK_SPLIT_ROLE_CENTRAL
depends on SHELL
endmenu

View File

@ -0,0 +1,80 @@
#include <stdlib.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
#define HELP_NONE "[key_position]"
static int parse_key_position(const struct shell *shell, char *pos_str) {
int position;
char *endptr;
position = strtoul(pos_str, &endptr, 10);
if (endptr == pos_str) {
shell_error(shell, "Enter an integer key position");
return -EINVAL;
}
return position;
}
static int parse_and_raise(const struct shell *shell, char *pos_str, bool pressed) {
int position = parse_key_position(shell, pos_str);
if (position < 0) {
return position;
}
raise_zmk_position_state_changed(
(struct zmk_position_state_changed){.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL,
.state = pressed,
.position = position,
.timestamp = k_uptime_get()});
return position;
}
static int cmd_key_tap(const struct shell *shell, size_t argc, char **argv) {
if (argc != 2) {
return -EINVAL;
}
parse_and_raise(shell, argv[1], true);
k_sleep(K_MSEC(50));
parse_and_raise(shell, argv[1], false);
return 0;
};
static int cmd_key_press(const struct shell *shell, size_t argc, char **argv) {
if (argc != 2) {
return -EINVAL;
}
parse_and_raise(shell, argv[1], true);
return 0;
};
static int cmd_key_release(const struct shell *shell, size_t argc, char **argv) {
if (argc != 2) {
return -EINVAL;
}
parse_and_raise(shell, argv[1], false);
return 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);