Skip to content

Commit 64f3ff0

Browse files
committed
Demonstrate using the status led API
1 parent 9b4c332 commit 64f3ff0

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ add_subdirectory(pio)
8585
add_subdirectory(pwm)
8686
add_subdirectory(reset)
8787
add_subdirectory(rtc)
88+
add_subdirectory(status_led)
8889
add_subdirectory(spi)
8990
add_subdirectory(system)
9091
add_subdirectory(timer)

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) | Blink the onboard LED using the status LED API.
379+
[color_blink](status_led) | Blink the onboard colored (WS2812) LED using the status LED API if supported by the board.
380+
374381
### System
375382

376383
App|Description

status_led/CMakeLists.txt

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

status_led/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+
}

status_led/status_blink.c

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)