Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: shell tap command #1318

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ target_sources(app PRIVATE src/workqueue.c)
target_sources(app PRIVATE src/main.c)

add_subdirectory(src/display/)
add_subdirectory(src/shell/)

zephyr_cc_option(-Wfatal-errors)
2 changes: 2 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ endmenu

menu "Advanced"

rsource "src/shell/Kconfig"

menu "Initialization Priorities"

if USB_DEVICE_STACK
Expand Down
1 change: 1 addition & 0 deletions app/boards/native_posix_64.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
rows = <2>;
columns = <2>;
exit-after;
events = <>;
};
};
1 change: 1 addition & 0 deletions app/src/shell/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources_ifdef(CONFIG_ZMK_SHELL_KEY_POSITIONS app PRIVATE key_positions.c)
8 changes: 8 additions & 0 deletions app/src/shell/Kconfig
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions app/src/shell/key_positions.c
Original file line number Diff line number Diff line change
@@ -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;
}

ZMK_EVENT_RAISE(new_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);
Loading