From db70901e2ca2ce8673fcda6651ee9fe07891a071 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Fri, 12 Jul 2024 22:30:04 +0200 Subject: [PATCH] examples/leds_shell: code refactoring - one led command with led id --- examples/leds_shell/README.md | 26 ++-- examples/leds_shell/main.c | 227 +++------------------------------- 2 files changed, 29 insertions(+), 224 deletions(-) diff --git a/examples/leds_shell/README.md b/examples/leds_shell/README.md index a70b537065243..df2187e396ac9 100644 --- a/examples/leds_shell/README.md +++ b/examples/leds_shell/README.md @@ -15,8 +15,7 @@ In particular, this example shows: The following commands are available: -- `led0`, `led1` ... `led7`: allows switching on/off or toggle internal board - LEDs. Number of commands depends on internal LEDs number. +- `led`: allows switching on/off or toggle internal board LEDs. - `init`: call before interact with GPIO port to initialize chosen port in output mode, which allows setting it to HIGH or LOW state. - `set`: sets GPIO port state to HIGH. @@ -25,7 +24,7 @@ The following commands are available: ## Example on `native` -- Build and run `LEDs` example application on the `native` target, +- Build and run `leds_shell` example application on the `native` target, as Linux application: ``` @@ -37,6 +36,7 @@ RIOT native hardware initialization complete. main(): This is RIOT! (Version: 2021.07-devel-10893-gb2e97-example-leds_shell) leds_shell, version 1.0.0 +This board has 2 LEDs > ``` @@ -53,6 +53,7 @@ RIOT native hardware initialization complete. main(): This is RIOT! (Version: 2021.07-devel-10893-gb2e97-example-leds_shell) leds_shell, version 1.0.0 +This board has 2 LEDs > ``` @@ -65,19 +66,18 @@ Command Description init GPIO pin initialization set Set GPIO pin to HIGH clear Set GPIO pin to LOW -led0 Switch on/off on-board LED0 -led1 Switch on/off on-board LED1 +led Switch on/off or toggle on-board LEDs ``` - Enable internal LED0 and LED1 (the `native` target has two virtual LEDs): ``` -> led0 on -led0 on +> led 0 on +led 0 on LED_RED_ON -> led1 on -led1 on +> led 1 on +led 1 on LED_GREEN_ON ``` ## Example on sample board - stm32f469i-disco @@ -90,6 +90,7 @@ $ make BOARD=stm32f469i-disco flash term [...] main(): This is RIOT! (Version: 2021.07-devel-10894-g2ad22b9-example-leds_shell) leds_shell, version 1.0.0 +This board has 4 LEDs > help help Command Description @@ -97,10 +98,7 @@ Command Description init GPIO pin initialization set Set GPIO pin to HIGH clear Set GPIO pin to LOW -led0 Switch on/off on-board LED0 -led1 Switch on/off on-board LED1 -led2 Switch on/off on-board LED2 -led3 Switch on/off on-board LED3 +led Switch on/off or toggle on-board LEDs ``` -- Switch on/off internal LEDs using commands `led0`, `led1`, `led2` or `led3`. \ No newline at end of file +- Switch on/off internal LEDs using commands `led` command and appropriate id. diff --git a/examples/leds_shell/main.c b/examples/leds_shell/main.c index b5500c2bea5d9..d98935870fa59 100644 --- a/examples/leds_shell/main.c +++ b/examples/leds_shell/main.c @@ -11,8 +11,8 @@ * @{ * * @file - * @brief LEDs - sample application for demonstrating internal - * board LEDs on/off and basic GPIO + * @brief leds_shell - sample application for demonstrating internal + * board LEDs on/off and basic GPIO using interactive RIOT shell * * @author Krzysztof Cabaj * @@ -85,235 +85,41 @@ static int clear_command(int argc, char **argv) return 0; } -#ifdef LED0_IS_PRESENT -static int led0_command(int argc, char **argv) +static int led_command(int argc, char **argv) { - if (argc < 2) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED0_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED0_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED0_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED1_IS_PRESENT -static int led1_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED1_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED1_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED1_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED2_IS_PRESENT -static int led2_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED2_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED2_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED2_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED3_IS_PRESENT -static int led3_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED3_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED3_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED3_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED4_IS_PRESENT -static int led4_command(int argc, char **argv) -{ - if (argc <= 1) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED4_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED4_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED4_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED5_IS_PRESENT -static int led5_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); - return -1; - } - - if (strcmp(argv[1], "on") == 0) { - LED5_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED5_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED5_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } - - return 0; -} -#endif - -#ifdef LED6_IS_PRESENT -static int led6_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); + if (argc < 3) { + printf("usage: %s \n", argv[0]); return -1; } - if (strcmp(argv[1], "on") == 0) { - LED6_ON; - } - else if (strcmp(argv[1], "off") == 0) { - LED6_OFF; - } - else if (strcmp(argv[1], "toggle") == 0) { - LED6_TOGGLE; - } - else { - printf("usage: %s \n", argv[0]); - } + int led_id = atoi(argv[1]); - return 0; -} -#endif - -#ifdef LED7_IS_PRESENT -static int led7_command(int argc, char **argv) -{ - if (argc < 2) { - printf("usage: %s \n", argv[0]); + if (led_id >= LED_NUMOF) { + printf("This board has %d LEDs\n", LED_NUMOF); return -1; } - if (strcmp(argv[1], "on") == 0) { - LED7_ON; + if (strcmp(argv[2], "on") == 0) { + led_on(led_id); } - else if (strcmp(argv[1], "off") == 0) { - LED7_OFF; + else if (strcmp(argv[2], "off") == 0) { + led_off(led_id); } - else if (strcmp(argv[1], "toggle") == 0) { - LED7_TOGGLE; + else if (strcmp(argv[2], "toggle") == 0) { + led_toggle(led_id); } else { - printf("usage: %s \n", argv[0]); + printf("usage: %s \n", argv[0]); } return 0; } -#endif - static const shell_command_t commands[] = { { "init", "GPIO pin initialization", init_command }, { "set", "Set GPIO pin to HIGH", set_command }, { "clear", "Set GPIO pin to LOW", clear_command }, -#ifdef LED0_IS_PRESENT - { "led0", "Switch on/off on-board LED0", led0_command }, -#endif -#ifdef LED1_IS_PRESENT - { "led1", "Switch on/off on-board LED1", led1_command }, -#endif -#ifdef LED2_IS_PRESENT - { "led2", "Switch on/off on-board LED2", led2_command }, -#endif -#ifdef LED3_IS_PRESENT - { "led3", "Switch on/off on-board LED3", led3_command }, -#endif -#ifdef LED4_IS_PRESENT - { "led4", "Switch on/off on-board LED4", led4_command }, -#endif -#ifdef LED5_IS_PRESENT - { "led5", "Switch on/off on-board LED5", led5_command }, -#endif -#ifdef LED6_IS_PRESENT - { "led6", "Switch on/off on-board LED6", led6_command }, -#endif -#ifdef LED7_IS_PRESENT - { "led7", "Switch on/off on-board LED7", led7_command }, -#endif + { "led", "Switch on/off or toggle on-board LEDs", led_command}, { NULL, NULL, NULL } }; @@ -321,6 +127,7 @@ int main(void) { char line_buf[SHELL_DEFAULT_BUFSIZE]; printf("leds_shell, version 1.0.0\n"); + printf("This board has %d LEDs\n", LED_NUMOF); shell_run(commands, line_buf, SHELL_DEFAULT_BUFSIZE);