diff --git a/CMakeLists.txt b/CMakeLists.txt index 33992dec8..ab8f3f34b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,7 @@ add_subdirectory(pwm) add_subdirectory(reset) add_subdirectory(rtc) add_subdirectory(spi) +add_subdirectory(status_led) add_subdirectory(system) add_subdirectory(timer) add_subdirectory(uart) diff --git a/README.md b/README.md index 9d1a8b195..810895fca 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,13 @@ App|Description [max7219_8x7seg_spi](spi/max7219_8x7seg_spi) | Attaching a Max7219 driving an 8 digit 7 segment display via SPI. [max7219_32x8_spi](spi/max7219_32x8_spi) | Attaching a Max7219 driving an 32x8 LED display via SPI. +### Status LED + +App|Description +---|--- +[status_blink](status_led/status_blink) | Blink the onboard LED using the status_led API. +[color_blink](status_led/color_blink) | Blink the onboard colored (WS2812) LED using the colored_status_led API if supported by the board. + ### System App|Description diff --git a/status_led/CMakeLists.txt b/status_led/CMakeLists.txt new file mode 100644 index 000000000..e1cfd1e68 --- /dev/null +++ b/status_led/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(status_blink) +add_subdirectory(color_blink) diff --git a/status_led/color_blink/CMakeLists.txt b/status_led/color_blink/CMakeLists.txt new file mode 100644 index 000000000..80fa4639b --- /dev/null +++ b/status_led/color_blink/CMakeLists.txt @@ -0,0 +1,14 @@ +# Blink the colored "status" LED connected to the GPIO defined by PICO_DEFAULT_WS2812_PIN for your board +add_executable(color_blink + color_blink.c +) +# You can define PICO_DEFAULT_WS2812_PIN yourself to add a WS2812 led to a normal GPIO +#target_compile_definitions(color_blink PRIVATE +# PICO_DEFAULT_WS2812_PIN=16 +#) +target_link_libraries(color_blink + pico_stdlib + pico_status_led +) +pico_add_extra_outputs(color_blink) +example_auto_set_url(color_blink) diff --git a/status_led/color_blink/color_blink.c b/status_led/color_blink/color_blink.c new file mode 100644 index 000000000..dbdfe896d --- /dev/null +++ b/status_led/color_blink/color_blink.c @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2025 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "pico/stdlib.h" +#include "pico/status_led.h" + +#if !PICO_COLORED_STATUS_LED_AVAILABLE +#warning The color_blink example requires a board with a WS2812 LED +#endif + +#ifndef LED_DELAY_MS +#define LED_DELAY_MS 250 +#endif + +int main() { + bool rc = status_led_init(); + hard_assert(rc); + hard_assert(colored_status_led_supported()); // This assert fails if your board does not have WS2812 support + uint32_t count = 0; + while (true) { + // flash red then green then blue + 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); + colored_status_led_set_on_with_color(color); + count++; + sleep_ms(LED_DELAY_MS); + assert(colored_status_led_get_state()); + colored_status_led_set_state(false); + sleep_ms(LED_DELAY_MS); + assert(!colored_status_led_get_state()); + } + status_led_deinit(); +} diff --git a/status_led/status_blink/CMakeLists.txt b/status_led/status_blink/CMakeLists.txt new file mode 100644 index 000000000..531edad90 --- /dev/null +++ b/status_led/status_blink/CMakeLists.txt @@ -0,0 +1,16 @@ +# Blink the "status" LED either connected to the GPIO defined by PICO_DEFAULT_LED_PIN for your board +# or via the WiFi chip on boards like Pico 2 or Pico 2 W + +add_executable(status_blink + status_blink.c +) +# You can define PICO_DEFAULT_LED_PIN yourself to add a led to a different GPIO +#target_compile_definitions(status_blink PRIVATE +# PICO_DEFAULT_LED_PIN=15 +#) +target_link_libraries(status_blink + pico_stdlib + pico_status_led +) +pico_add_extra_outputs(status_blink) +example_auto_set_url(status_blink) diff --git a/status_led/status_blink/status_blink.c b/status_led/status_blink/status_blink.c new file mode 100644 index 000000000..e798f865d --- /dev/null +++ b/status_led/status_blink/status_blink.c @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2025 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "pico/stdlib.h" +#include "pico/status_led.h" + +#ifndef LED_DELAY_MS +#define LED_DELAY_MS 250 +#endif + +int main() { + bool rc = status_led_init(); + hard_assert(rc); + while (true) { + status_led_set_state(true); + sleep_ms(LED_DELAY_MS); + assert(status_led_get_state()); + status_led_set_state(false); + sleep_ms(LED_DELAY_MS); + assert(!status_led_get_state()); + } + status_led_deinit(); +}