From 1113be9e05bedf8443b15a20a84a1a732eede890 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Wed, 10 Jul 2024 09:58:42 +0200 Subject: [PATCH 1/4] examples/LEDs: addition of new example appplication --- examples/LEDs/Makefile | 52 +++++++ examples/LEDs/main.c | 328 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 380 insertions(+) create mode 100644 examples/LEDs/Makefile create mode 100644 examples/LEDs/main.c diff --git a/examples/LEDs/Makefile b/examples/LEDs/Makefile new file mode 100644 index 000000000000..c289a2d81261 --- /dev/null +++ b/examples/LEDs/Makefile @@ -0,0 +1,52 @@ +#### +#### 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 + +# If no BOARD is found in the environment, use this default: +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 + +# Change this to 0 to show compiler invocation lines by default: +QUIET ?= 1 + +# Modules to include: +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/main.c b/examples/LEDs/main.c new file mode 100644 index 000000000000..9413b2a90bb3 --- /dev/null +++ b/examples/LEDs/main.c @@ -0,0 +1,328 @@ +/* + * Copyright (C) 2024 Krzysztof Cabaj + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup examples + * @{ + * + * @file + * @brief LEDs - sample application for demonstrating internal + * board LEDs on/off and basic GPIO + * + * @author Krzysztof Cabaj + * + * @} + */ + +#include "stdio.h" +#include "stdlib.h" +#include "shell.h" +#include "led.h" +#include + +static int init_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("GPIO initialization PORT %d, PIN %d\n", port_no, pin_no); + + int result; + + result = gpio_init(GPIO_PIN(port_no, pin_no), GPIO_OUT); + + if (result == 0) { + printf("Success!\n"); + } + else if (result == -1) { + printf("Failure!\n"); + } + + return 0; +} + +static int set_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 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 { + 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]); + 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 + +#ifdef LED7_IS_PRESENT +static int led7_command(int argc, char **argv) +{ + if (argc < 2) { + printf("usage: %s \n", argv[0]); + return -1; + } + + if (strcmp(argv[1], "on") == 0) { + LED7_ON; + } + else if (strcmp(argv[1], "off") == 0) { + LED7_OFF; + } + else if (strcmp(argv[1], "toggle") == 0) { + LED7_TOGGLE; + } + else { + 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 + { NULL, NULL, NULL } +}; + +int main(void) +{ + char line_buf[SHELL_DEFAULT_BUFSIZE]; + printf("LEDs, version 1.0.0\n"); + + shell_run(commands, line_buf, SHELL_DEFAULT_BUFSIZE); + + return 0; +} From 9aea4bdb55b6ce48abc879c7f1fcbc18159bde15 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Wed, 10 Jul 2024 10:42:29 +0200 Subject: [PATCH 2/4] examples/LEDs: add README.md file --- examples/LEDs/README.md | 106 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 examples/LEDs/README.md diff --git a/examples/LEDs/README.md b/examples/LEDs/README.md new file mode 100644 index 000000000000..59856eb6942f --- /dev/null +++ b/examples/LEDs/README.md @@ -0,0 +1,106 @@ +# LEDs and basic GPIO example application + +## Description + +This basic example 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.). + +In particular, this example shows: +- on/off and toggle internal board LEDs. +- initialize GPIO port in output mode. +- set GPIO port state to HIGH or LOW. + +## Shell command + +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. +- `help`: default RIOT command, which shows available commands. + +## Example on `native` + +- Build and run `LEDs` example application on the `native` target, +as Linux application: + +``` +$ make 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) +LEDs, version 1.0.0 +> +``` + +Or run it directly without terminal. Go to `RIOT/examples/LEDs` and run +commands: + +``` +$ make +[...] +$ ./bin/native/LEDs.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) +LEDs, version 1.0.0 +> +``` + +- List the available commands: +``` +> 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 +``` + +- Enable internal LED0 and LED1 (the `native` target has two virtual LEDs): + +``` +> led0 on +led0 on +LED_RED_ON + +> led1 on +led1 on +LED_GREEN_ON +``` +## Example on sample board - stm32f469i-disco + +- Build and flash `LEDs` example application on sample board, for example + `stm32f469i-disco` target, which has 4 internal LEDs: + +``` +$ make BOARD=stm32f469i-disco flash term +[...] +main(): This is RIOT! (Version: 2021.07-devel-10894-g2ad22b9-example-LEDs) +LEDs, version 1.0.0 +> 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 +``` + +- Switch on/off internal LEDs using commands `led0`, `led1`, `led2` or `led3`. \ No newline at end of file From 4273f970a71877948d5432e5d9225f5533c696af Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Fri, 12 Jul 2024 18:11:03 +0200 Subject: [PATCH 3/4] examples/leds_shell: change application name --- examples/{LEDs => leds_shell}/Makefile | 2 +- examples/{LEDs => leds_shell}/README.md | 24 ++++++++++++------------ examples/{LEDs => leds_shell}/main.c | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) rename examples/{LEDs => leds_shell}/Makefile (98%) rename examples/{LEDs => leds_shell}/README.md (81%) rename examples/{LEDs => leds_shell}/main.c (99%) diff --git a/examples/LEDs/Makefile b/examples/leds_shell/Makefile similarity index 98% rename from examples/LEDs/Makefile rename to examples/leds_shell/Makefile index c289a2d81261..f74f22516cf4 100644 --- a/examples/LEDs/Makefile +++ b/examples/leds_shell/Makefile @@ -7,7 +7,7 @@ #### # Set the name of your application: -APPLICATION = LEDs +APPLICATION = leds_shell # If no BOARD is found in the environment, use this default: BOARD ?= native diff --git a/examples/LEDs/README.md b/examples/leds_shell/README.md similarity index 81% rename from examples/LEDs/README.md rename to examples/leds_shell/README.md index 59856eb6942f..a70b53706524 100644 --- a/examples/LEDs/README.md +++ b/examples/leds_shell/README.md @@ -2,9 +2,9 @@ ## Description -This basic example 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.). +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.). In particular, this example shows: - on/off and toggle internal board LEDs. @@ -35,24 +35,24 @@ 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) -LEDs, version 1.0.0 +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` and run +Or run it directly without terminal. Go to `RIOT/examples/leds_shell` and run commands: ``` $ make [...] -$ ./bin/native/LEDs.elf +$ ./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) -LEDs, version 1.0.0 +main(): This is RIOT! (Version: 2021.07-devel-10893-gb2e97-example-leds_shell) +leds_shell, version 1.0.0 > ``` @@ -82,14 +82,14 @@ LED_GREEN_ON ``` ## Example on sample board - stm32f469i-disco -- Build and flash `LEDs` example application on sample board, for example +- 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 [...] -main(): This is RIOT! (Version: 2021.07-devel-10894-g2ad22b9-example-LEDs) -LEDs, version 1.0.0 +main(): This is RIOT! (Version: 2021.07-devel-10894-g2ad22b9-example-leds_shell) +leds_shell, version 1.0.0 > help help Command Description diff --git a/examples/LEDs/main.c b/examples/leds_shell/main.c similarity index 99% rename from examples/LEDs/main.c rename to examples/leds_shell/main.c index 9413b2a90bb3..b5500c2bea5d 100644 --- a/examples/LEDs/main.c +++ b/examples/leds_shell/main.c @@ -320,7 +320,7 @@ static const shell_command_t commands[] = { int main(void) { char line_buf[SHELL_DEFAULT_BUFSIZE]; - printf("LEDs, version 1.0.0\n"); + printf("leds_shell, version 1.0.0\n"); shell_run(commands, line_buf, SHELL_DEFAULT_BUFSIZE); From 9959e629bf9fd2e5ef67f3436a515a949d608e53 Mon Sep 17 00:00:00 2001 From: krzysztof-cabaj Date: Fri, 12 Jul 2024 22:30:04 +0200 Subject: [PATCH 4/4] examples/leds_shell: code refactoring-one led and gpio commands --- examples/leds_shell/Makefile | 32 +--- examples/leds_shell/Makefile.ci | 3 + examples/leds_shell/README.md | 69 +++----- examples/leds_shell/main.c | 297 +++++--------------------------- 4 files changed, 66 insertions(+), 335 deletions(-) create mode 100644 examples/leds_shell/Makefile.ci diff --git a/examples/leds_shell/Makefile b/examples/leds_shell/Makefile index f74f22516cf4..64ee5ebc129c 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/Makefile.ci b/examples/leds_shell/Makefile.ci new file mode 100644 index 000000000000..72db76ccb5cc --- /dev/null +++ b/examples/leds_shell/Makefile.ci @@ -0,0 +1,3 @@ +BOARD_INSUFFICIENT_MEMORY := \ + atmega8 \ + # diff --git a/examples/leds_shell/README.md b/examples/leds_shell/README.md index a70b53706524..15b36e5ee285 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 b5500c2bea5d..c465babf4393 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);