From ed06b0ba747e2e33d458791e5f411a389c007549 Mon Sep 17 00:00:00 2001 From: Hans Binderup Date: Thu, 21 Mar 2024 14:10:09 +0100 Subject: [PATCH] drivers: led: shell: Add devices as sub commands Currently you have to manually type out the device name for each command. This commit adds support for entry tab completion and device lookup if help is enabled. Signed-off-by: Hans Binderup --- drivers/led/led_shell.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/drivers/led/led_shell.c b/drivers/led/led_shell.c index 10364b43a5..3e26c915c3 100644 --- a/drivers/led/led_shell.c +++ b/drivers/led/led_shell.c @@ -307,21 +307,32 @@ cmd_write_channels(const struct shell *sh, size_t argc, char **argv) return err; } -SHELL_STATIC_SUBCMD_SET_CREATE(sub_led, - SHELL_CMD_ARG(off, NULL, " ", cmd_off, 3, 0), - SHELL_CMD_ARG(on, NULL, " ", cmd_on, 3, 0), - SHELL_CMD_ARG(get_info, NULL, " ", cmd_get_info, 3, 0), - SHELL_CMD_ARG(set_brightness, NULL, " ", +static void device_name_get(size_t idx, struct shell_static_entry *entry) +{ + const struct device *dev = shell_device_lookup(idx, NULL); + + entry->syntax = (dev != NULL) ? dev->name : NULL; + entry->handler = NULL; + entry->help = NULL; + entry->subcmd = NULL; +} + +SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get); + +SHELL_STATIC_SUBCMD_SET_CREATE( + sub_led, SHELL_CMD_ARG(off, &dsub_device_name, " ", cmd_off, 3, 0), + SHELL_CMD_ARG(on, &dsub_device_name, " ", cmd_on, 3, 0), + SHELL_CMD_ARG(get_info, &dsub_device_name, " ", cmd_get_info, 3, 0), + SHELL_CMD_ARG(set_brightness, &dsub_device_name, " ", cmd_set_brightness, 4, 0), - SHELL_CMD_ARG(set_color, NULL, - " ... ", - cmd_set_color, 4, MAX_CHANNEL_ARGS - 1), - SHELL_CMD_ARG(set_channel, NULL, " ", + SHELL_CMD_ARG(set_color, &dsub_device_name, + " ... ", cmd_set_color, 4, + MAX_CHANNEL_ARGS - 1), + SHELL_CMD_ARG(set_channel, &dsub_device_name, " ", cmd_set_channel, 4, 0), - SHELL_CMD_ARG(write_channels, NULL, - " ... ", - cmd_write_channels, 4, MAX_CHANNEL_ARGS - 1), - SHELL_SUBCMD_SET_END -); + SHELL_CMD_ARG(write_channels, &dsub_device_name, + " ... ", cmd_write_channels, 4, + MAX_CHANNEL_ARGS - 1), + SHELL_SUBCMD_SET_END); SHELL_CMD_REGISTER(led, &sub_led, "LED commands", NULL);