Loading...
Searching...
No Matches
status_led.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
16
17#ifndef _PICO_STATUS_LED_H
18#define _PICO_STATUS_LED_H
19
20#include "hardware/gpio.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#if defined(CYW43_WL_GPIO_LED_PIN)
27#include "cyw43.h"
28#endif
29
30struct async_context;
31
32
33// PICO_CONFIG: PICO_STATUS_LED_AVAILABLE, Indicate whether a single-color status LED is available, type=bool, default=1 if PICO_DEFAULT_LED_PIN or CYW43_WL_GPIO_LED_PIN is defined; may be set by the user to 0 to not use either even if they are available, group=pico_status_led
34#ifndef PICO_STATUS_LED_AVAILABLE
35#if defined(PICO_DEFAULT_LED_PIN) || defined(CYW43_WL_GPIO_LED_PIN)
36#define PICO_STATUS_LED_AVAILABLE 1
37#else
38#define PICO_STATUS_LED_AVAILABLE 0
39#endif
40#endif
41
42// PICO_CONFIG: PICO_COLORED_STATUS_LED_AVAILABLE, Indicate whether a colored status LED is available, type=bool, default=1 if PICO_DEFAULT_WS2812_PIN is defined; may be set by the user to 0 to not use the colored status LED even if available, group=pico_status_led
43#ifndef PICO_COLORED_STATUS_LED_AVAILABLE
44#ifdef PICO_DEFAULT_WS2812_PIN
45#define PICO_COLORED_STATUS_LED_AVAILABLE 1
46#else
47#define PICO_COLORED_STATUS_LED_AVAILABLE 0
48#endif
49#endif
50
51// PICO_CONFIG: PICO_STATUS_LED_VIA_COLORED_STATUS_LED, Indicate if the colored status LED should be used for both status_led and colored_status_led APIs, type=bool, default=1 if PICO_COLORED_STATUS_LED_AVAILABLE is 1 and PICO_STATUS_LED_AVAILABLE is 0, group=pico_status_led
52#ifndef PICO_STATUS_LED_VIA_COLORED_STATUS_LED
53#define PICO_STATUS_LED_VIA_COLORED_STATUS_LED (PICO_COLORED_STATUS_LED_AVAILABLE && !PICO_STATUS_LED_AVAILABLE)
54#endif
55
56// PICO_CONFIG: PICO_COLORED_STATUS_LED_USES_WRGB, Indicate if the colored status LED supports WRGB, type=bool, default=0, group=pico_status_led
57#ifndef PICO_COLORED_STATUS_LED_USES_WRGB
58#define PICO_COLORED_STATUS_LED_USES_WRGB 0
59#endif
60
64#ifndef PICO_COLORED_STATUS_LED_COLOR_FROM_RGB
65#define PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b))
66#endif
67
73#ifndef PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB
74#define PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB(w, r, g, b) (((w) << 24) | ((r) << 16) | ((g) << 8) | (b))
75#endif
76
77// PICO_CONFIG: PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR, the default pixel color value of the colored status LED when it is on, type=int, group=pico_status_led
78#ifndef PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR
79#if PICO_COLORED_STATUS_LED_USES_WRGB
80#define PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB(0xaa, 0, 0, 0)
81#else
82#define PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(0xaa, 0xaa, 0xaa)
83#endif
84#endif
85
102bool status_led_init(void);
103
115bool status_led_init_with_context(struct async_context *context);
116
123static inline bool colored_status_led_supported(void) {
124 return PICO_COLORED_STATUS_LED_AVAILABLE;
125}
126
132static inline bool status_led_via_colored_status_led(void) {
133 return PICO_STATUS_LED_VIA_COLORED_STATUS_LED;
134}
135
144static inline bool status_led_supported(void) {
147 }
148 return PICO_STATUS_LED_AVAILABLE;
149}
150
159bool colored_status_led_set_state(bool led_on);
160
169
178bool colored_status_led_set_on_with_color(uint32_t color);
179
188
197static inline bool status_led_set_state(bool led_on) {
199 return colored_status_led_set_state(led_on);
200 } else if (status_led_supported()) {
201#if defined(PICO_DEFAULT_LED_PIN)
202 #if PICO_DEFAULT_LED_PIN_INVERTED
203 gpio_put(PICO_DEFAULT_LED_PIN, !led_on);
204 #else
205 gpio_put(PICO_DEFAULT_LED_PIN, led_on);
206 #endif
207 return true;
208#elif defined(CYW43_WL_GPIO_LED_PIN)
209 cyw43_gpio_set(&cyw43_state, CYW43_WL_GPIO_LED_PIN, led_on);
210 return true;
211#endif
212 }
213 return false;
214}
215
223static inline bool status_led_get_state() {
226 } else if (status_led_supported()) {
227#if defined(PICO_DEFAULT_LED_PIN)
228 #if PICO_DEFAULT_LED_PIN_INVERTED
229 return !gpio_get(PICO_DEFAULT_LED_PIN);
230 #else
231 return gpio_get(PICO_DEFAULT_LED_PIN);
232 #endif
233#elif defined CYW43_WL_GPIO_LED_PIN
234 bool value = false;
235 cyw43_gpio_get(&cyw43_state, CYW43_WL_GPIO_LED_PIN, &value);
236 return value;
237#endif
238 }
239 return false;
240}
241
247void status_led_deinit();
248
249#ifdef __cplusplus
250}
251#endif
252
253#endif
CYW43 driver interface.
int cyw43_gpio_get(cyw43_t *self, int gpio, bool *val)
Get the value of the cyw43 gpio.
Definition cyw43_ctrl.c:749
int cyw43_gpio_set(cyw43_t *self, int gpio, bool val)
Set the value of the cyw43 gpio.
Definition cyw43_ctrl.c:737
static void gpio_put(uint gpio, bool value)
Drive a single GPIO high/low.
Definition gpio.h:1180
static bool gpio_get(uint gpio)
Get state of a single specified GPIO.
Definition gpio.h:894
static bool status_led_set_state(bool led_on)
Set the status LED on or off.
Definition status_led.h:197
bool status_led_init_with_context(struct async_context *context)
Initialise the status LED(s).
Definition status_led.c:235
static bool status_led_supported(void)
Determine if the single-color status_led_ APIs are supported (i.e. if there is a regular LED,...
Definition status_led.h:144
bool colored_status_led_set_on_with_color(uint32_t color)
Ensure the colored status LED is on, with the specified color.
Definition status_led.c:151
static bool status_led_get_state()
Get the state of the status LED.
Definition status_led.h:223
void status_led_deinit()
De-initialize the status LED(s).
Definition status_led.c:240
static bool status_led_via_colored_status_led(void)
Determine if the colored status LED is being used for the single-color status_led_ APIs.
Definition status_led.h:132
bool colored_status_led_get_state(void)
Get the state of the colored status LED.
Definition status_led.c:177
uint32_t colored_status_led_get_on_color(void)
Get the color used for the status LED value when it is on.
Definition status_led.c:156
bool colored_status_led_set_state(bool led_on)
Set the colored status LED on or off.
Definition status_led.c:160
static bool colored_status_led_supported(void)
Determine if the colored_status_led_ APIs are supported (i.e. if there is a colored status LED,...
Definition status_led.h:123
bool status_led_init(void)
Initialize the status LED(s).
Definition status_led.c:231
Base structure type of all async_contexts. For details about its use, see pico_async_context.
Definition async_context.h:179