Skip to content

Commit cde5667

Browse files
Demonstrate using the status_led API (#687)
1 parent d467f4b commit cde5667

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ add_subdirectory(pwm)
8686
add_subdirectory(reset)
8787
add_subdirectory(rtc)
8888
add_subdirectory(spi)
89+
add_subdirectory(status_led)
8990
add_subdirectory(system)
9091
add_subdirectory(timer)
9192
add_subdirectory(uart)

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,13 @@ App|Description
371371
[max7219_8x7seg_spi](spi/max7219_8x7seg_spi) | Attaching a Max7219 driving an 8 digit 7 segment display via SPI.
372372
[max7219_32x8_spi](spi/max7219_32x8_spi) | Attaching a Max7219 driving an 32x8 LED display via SPI.
373373

374+
### Status LED
375+
376+
App|Description
377+
---|---
378+
[status_blink](status_led/status_blink) | Blink the onboard LED using the status_led API.
379+
[color_blink](status_led/color_blink) | Blink the onboard colored (WS2812) LED using the colored_status_led API if supported by the board.
380+
374381
### System
375382

376383
App|Description

status_led/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(status_blink)
2+
add_subdirectory(color_blink)

status_led/color_blink/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Blink the colored "status" LED connected to the GPIO defined by PICO_DEFAULT_WS2812_PIN for your board
2+
add_executable(color_blink
3+
color_blink.c
4+
)
5+
# You can define PICO_DEFAULT_WS2812_PIN yourself to add a WS2812 led to a normal GPIO
6+
#target_compile_definitions(color_blink PRIVATE
7+
# PICO_DEFAULT_WS2812_PIN=16
8+
#)
9+
target_link_libraries(color_blink
10+
pico_stdlib
11+
pico_status_led
12+
)
13+
pico_add_extra_outputs(color_blink)
14+
example_auto_set_url(color_blink)

status_led/color_blink/color_blink.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2025 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/stdlib.h"
8+
#include "pico/status_led.h"
9+
10+
#if !PICO_COLORED_STATUS_LED_AVAILABLE
11+
#warning The color_blink example requires a board with a WS2812 LED
12+
#endif
13+
14+
#ifndef LED_DELAY_MS
15+
#define LED_DELAY_MS 250
16+
#endif
17+
18+
int main() {
19+
bool rc = status_led_init();
20+
hard_assert(rc);
21+
hard_assert(colored_status_led_supported()); // This assert fails if your board does not have WS2812 support
22+
uint32_t count = 0;
23+
while (true) {
24+
// flash red then green then blue
25+
uint32_t color = PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(count % 3 == 0 ? 0xaa : 0, count % 3 == 1 ? 0xaa : 0, count % 3 == 2 ? 0xaa : 0);
26+
colored_status_led_set_on_with_color(color);
27+
count++;
28+
sleep_ms(LED_DELAY_MS);
29+
assert(colored_status_led_get_state());
30+
colored_status_led_set_state(false);
31+
sleep_ms(LED_DELAY_MS);
32+
assert(!colored_status_led_get_state());
33+
}
34+
status_led_deinit();
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Blink the "status" LED either connected to the GPIO defined by PICO_DEFAULT_LED_PIN for your board
2+
# or via the WiFi chip on boards like Pico 2 or Pico 2 W
3+
4+
add_executable(status_blink
5+
status_blink.c
6+
)
7+
# You can define PICO_DEFAULT_LED_PIN yourself to add a led to a different GPIO
8+
#target_compile_definitions(status_blink PRIVATE
9+
# PICO_DEFAULT_LED_PIN=15
10+
#)
11+
target_link_libraries(status_blink
12+
pico_stdlib
13+
pico_status_led
14+
)
15+
pico_add_extra_outputs(status_blink)
16+
example_auto_set_url(status_blink)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2025 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/stdlib.h"
8+
#include "pico/status_led.h"
9+
10+
#ifndef LED_DELAY_MS
11+
#define LED_DELAY_MS 250
12+
#endif
13+
14+
int main() {
15+
bool rc = status_led_init();
16+
hard_assert(rc);
17+
while (true) {
18+
status_led_set_state(true);
19+
sleep_ms(LED_DELAY_MS);
20+
assert(status_led_get_state());
21+
status_led_set_state(false);
22+
sleep_ms(LED_DELAY_MS);
23+
assert(!status_led_get_state());
24+
}
25+
status_led_deinit();
26+
}

0 commit comments

Comments
 (0)