Skip to content

Commit

Permalink
examples/leds_shell: code refactoring - one led command with led id
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-cabaj committed Jul 12, 2024
1 parent 4273f97 commit db70901
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 224 deletions.
26 changes: 12 additions & 14 deletions examples/leds_shell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

```
Expand All @@ -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
>
```

Expand All @@ -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
>
```

Expand All @@ -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
Expand All @@ -90,17 +90,15 @@ $ 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
---------------------------------------
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`.
- Switch on/off internal LEDs using commands `led` command and appropriate id.
227 changes: 17 additions & 210 deletions examples/leds_shell/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
Expand Down Expand Up @@ -85,242 +85,49 @@ 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 <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED1_IS_PRESENT
static int led1_command(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED2_IS_PRESENT
static int led2_command(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED3_IS_PRESENT
static int led3_command(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED4_IS_PRESENT
static int led4_command(int argc, char **argv)
{
if (argc <= 1) {
printf("usage: %s <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED5_IS_PRESENT
static int led5_command(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
}

return 0;
}
#endif

#ifdef LED6_IS_PRESENT
static int led6_command(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s <on|off|toggle>\n", argv[0]);
if (argc < 3) {
printf("usage: %s <id> <on|off|toggle>\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 <on|off|toggle>\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 <on|off|toggle>\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 <on|off|toggle>\n", argv[0]);
printf("usage: %s <id> <on|off|toggle>\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 }
};

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);

Expand Down

0 comments on commit db70901

Please sign in to comment.