From 1df91707195afb2084ca670e457d6d14186d3e75 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 and gpio commands --- examples/leds_shell/Makefile | 32 +--- examples/leds_shell/README.md | 69 +++----- examples/leds_shell/main.c | 297 +++++----------------------------- 3 files changed, 63 insertions(+), 335 deletions(-) diff --git a/examples/leds_shell/Makefile b/examples/leds_shell/Makefile index f74f22516cf41..64ee5ebc129c8 100644 --- a/examples/leds_shell/Makefile +++ b/examples/leds_shell/Makefile @@ -1,11 +1,3 @@ -#### -#### Sample Makefile for building applications with the RIOT OS -#### -#### The example file system layout is: -#### ./application Makefile -#### ../../RIOT -#### - # Set the name of your application: APPLICATION = leds_shell @@ -15,17 +7,10 @@ BOARD ?= native # This has to be the absolute path to the RIOT base directory: RIOTBASE ?= $(CURDIR)/../../ -# Uncomment this to enable scheduler statistics for ps: -#CFLAGS += -DSCHEDSTATISTICS - -# If you want to use native with valgrind, you should recompile native -# with the target all-valgrind instead of all: -# make -B clean all-valgrind - # Uncomment this to enable code in RIOT that does safety checking # which is not needed in a production environment but helps in the # development process: -#DEVELHELP = 1 +DEVELHELP = 1 # Change this to 0 to show compiler invocation lines by default: QUIET ?= 1 @@ -34,19 +19,4 @@ QUIET ?= 1 USEMODULE += shell USEMODULE += periph_gpio -# If your application is very simple and doesn't use modules that use -# messaging, it can be disabled to save some memory: - -#DISABLE_MODULE += core_msg - -#INCLUDES += -Iapplication_include - -# Specify custom dependencies for your application here ... -# APPDEPS = app_data.h config.h - include $(RIOTBASE)/Makefile.include - -# ... and define them here (after including Makefile.include, -# otherwise you modify the standard target): -#proj_data.h: script.py data.tar.gz -# ./script.py diff --git a/examples/leds_shell/README.md b/examples/leds_shell/README.md index a70b537065243..15b36e5ee2851 100644 --- a/examples/leds_shell/README.md +++ b/examples/leds_shell/README.md @@ -4,7 +4,8 @@ The application `leds_shell` is a basic example, which allows easy, interactive control of internal board LEDs, and basic GPIO for externally connected simple -devices (for e.g. additional LEDs, relay, motors - via dedicated drivers, etc.). +devices (for e.g. additional LEDs, relay, motors - via dedicated drivers, etc.) +via the shell. In particular, this example shows: - on/off and toggle internal board LEDs. @@ -15,44 +16,24 @@ 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. -- `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. -- `clear`: sets GPIO port stet to LOW. +- `led`: allows switching on/off or toggle internal board LEDs. +- `gpio`: allows initialization of GPIO port and set state to HIGH/LOW. - `help`: default RIOT command, which shows available commands. -## Example on `native` +## Usage on `BOARD=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: ``` -$ make term +$ make all term [...] RIOT native interrupts/signals initialized. RIOT native board initialized. RIOT native hardware initialization complete. main(): This is RIOT! (Version: 2021.07-devel-10893-gb2e97-example-leds_shell) -leds_shell, version 1.0.0 -> -``` - -Or run it directly without terminal. Go to `RIOT/examples/leds_shell` and run -commands: - -``` -$ make -[...] -$ ./bin/native/leds_shell.elf -RIOT native interrupts/signals initialized. -RIOT native board initialized. -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 > ``` @@ -62,45 +43,39 @@ leds_shell, version 1.0.0 help 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 +gpio GPIO pin initialization and set port state HIGH/LOW +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 +## Usage on actual hardware - `BOARD=stm32f469i-disco` - Build and flash `leds_shell` example application on sample board, for example `stm32f469i-disco` target, which has 4 internal LEDs: ``` -$ make BOARD=stm32f469i-disco flash term +$ make all 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 --------------------------------------- -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 +gpio GPIO pin initialization and set port state HIGH/LOW +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 `led` command and appropriate LED id. +- Initialize GPIO port using `gpio init`. +- Change state of GPIO port using `gpio set` and `gpio clear`. diff --git a/examples/leds_shell/main.c b/examples/leds_shell/main.c index b5500c2bea5d9..c465babf43931 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 * @@ -25,302 +25,85 @@ #include "led.h" #include -static int init_command(int argc, char **argv) +static int gpio_command(int argc, char **argv) { - if (argc < 3) { - printf("usage: %s \n", argv[0]); + if (argc < 4) { + printf("usage: %s \n", argv[0]); return -1; } - int port_no = atoi(argv[1]); - int pin_no = atoi(argv[2]); + int port_no = atoi(argv[2]); + int pin_no = atoi(argv[3]); - printf("GPIO initialization PORT %d, PIN %d\n", port_no, pin_no); + if (strcmp(argv[1], "init") == 0) { + printf("GPIO initialization PORT %d, PIN %d\n", port_no, pin_no); - int result; + int result; - result = gpio_init(GPIO_PIN(port_no, pin_no), GPIO_OUT); + result = gpio_init(GPIO_PIN(port_no, pin_no), GPIO_OUT); - if (result == 0) { - printf("Success!\n"); - } - else if (result == -1) { - printf("Failure!\n"); + if (result == 0) { + printf("Success!\n"); + } + else { + printf("Failure!\n"); + } } - - return 0; -} - -static int set_command(int argc, char **argv) -{ - if (argc < 3) { - printf("usage: %s \n", argv[0]); - return -1; + else if (strcmp(argv[1], "set") == 0) { + printf("Set HIGH to PORT %d, PIN %d\n", port_no, pin_no); + gpio_set(GPIO_PIN(port_no, pin_no)); } - - int port_no = atoi(argv[1]); - int pin_no = atoi(argv[2]); - - printf("Set HIGH to PORT %d, PIN %d\n", port_no, pin_no); - - gpio_set(GPIO_PIN(port_no, pin_no)); - - return 0; -} - -static int clear_command(int argc, char **argv) -{ - if (argc < 3) { - printf("usage: %s \n", argv[0]); - return -1; - } - - int port_no = atoi(argv[1]); - int pin_no = atoi(argv[2]); - - printf("Set LOW to PORT %d, PIN %d\n", port_no, pin_no); - - gpio_clear(GPIO_PIN(port_no, pin_no)); - - return 0; -} - -#ifdef LED0_IS_PRESENT -static int led0_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 if (strcmp(argv[1], "clear") == 0) { + printf("Set LOW to PORT %d, PIN %d\n", port_no, pin_no); + gpio_clear(GPIO_PIN(port_no, pin_no)); } else { - printf("usage: %s \n", argv[0]); + printf("usage: %s \n", argv[0]); } return 0; } -#endif -#ifdef LED1_IS_PRESENT -static int led1_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) { - 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]); - } - - return 0; -} -#endif + int led_id = atoi(argv[1]); -#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 + { "gpio", "GPIO pin initialization and set port state HIGH/LOW", gpio_command }, + { "led", "Switch on/off or toggle on-board LEDs", led_command}, { NULL, NULL, NULL } }; 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);