|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** \file pico/status_led.h |
| 8 | + * \defgroup pico_status_led pico_status_led |
| 9 | + * |
| 10 | + * \brief Enables access to the on-board status LED(s) |
| 11 | + * |
| 12 | + * Boards usually have access to one or two on-board status LEDs which are configured via the board header (PICO_DEFAULT_LED_PIN, CYW43_WL_GPIO_LED_PIN and/or PICO_DEFAULT_WS2812_PIN). |
| 13 | + * This library hides the low-level details so you can use the status LEDs for all boards without changing your code. |
| 14 | + * \note If your board has both a single-color LED and a colored LED, you can independently control the single-color LED with the `status_led_` APIs, and the colored LED with the `colored_status_led_` APIs |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef _PICO_STATUS_LED_H |
| 18 | +#define _PICO_STATUS_LED_H |
| 19 | + |
| 20 | +#include "pico.h" |
| 21 | + |
| 22 | +#ifdef __cplusplus |
| 23 | +extern "C" { |
| 24 | +#endif |
| 25 | + |
| 26 | +// 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 |
| 27 | +#ifndef PICO_STATUS_LED_AVAILABLE |
| 28 | +#define PICO_STATUS_LED_AVAILABLE 1 |
| 29 | +#endif |
| 30 | + |
| 31 | +// 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 |
| 32 | +#ifndef PICO_COLORED_STATUS_LED_AVAILABLE |
| 33 | +#define PICO_COLORED_STATUS_LED_AVAILABLE 1 |
| 34 | +#endif |
| 35 | + |
| 36 | +// 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 |
| 37 | +#ifndef PICO_STATUS_LED_VIA_COLORED_STATUS_LED |
| 38 | +#define PICO_STATUS_LED_VIA_COLORED_STATUS_LED (PICO_COLORED_STATUS_LED_AVAILABLE && !PICO_STATUS_LED_AVAILABLE) |
| 39 | +#endif |
| 40 | + |
| 41 | +// PICO_CONFIG: PICO_COLORED_STATUS_LED_USES_WRGB, Indicate if the colored status LED supports WRGB, type=bool, default=0, group=pico_status_led |
| 42 | +#ifndef PICO_COLORED_STATUS_LED_USES_WRGB |
| 43 | +#define PICO_COLORED_STATUS_LED_USES_WRGB 0 |
| 44 | +#endif |
| 45 | + |
| 46 | +/*! \brief Generate an RGB color value for /ref colored_status_led_set_on_with_color |
| 47 | + * \ingroup pico_status_led |
| 48 | + */ |
| 49 | +#ifndef PICO_COLORED_STATUS_LED_COLOR_FROM_RGB |
| 50 | +#define PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(r, g, b) (((r) << 16) | ((g) << 8) | (b)) |
| 51 | +#endif |
| 52 | + |
| 53 | +/*! \brief Generate an WRGB color value for \ref colored_status_led_set_on_with_color |
| 54 | + * \ingroup pico_status_led |
| 55 | + * |
| 56 | + * \note If your hardware does not support a white pixel, the white component is ignored |
| 57 | + */ |
| 58 | +#ifndef PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB |
| 59 | +#define PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB(w, r, g, b) (((w) << 24) | ((r) << 16) | ((g) << 8) | (b)) |
| 60 | +#endif |
| 61 | + |
| 62 | +// 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 |
| 63 | +#ifndef PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR |
| 64 | +#if PICO_COLORED_STATUS_LED_USES_WRGB |
| 65 | +#define PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR PICO_COLORED_STATUS_LED_COLOR_FROM_WRGB(0xaa, 0, 0, 0) |
| 66 | +#else |
| 67 | +#define PICO_DEFAULT_COLORED_STATUS_LED_ON_COLOR PICO_COLORED_STATUS_LED_COLOR_FROM_RGB(0xaa, 0xaa, 0xaa) |
| 68 | +#endif |
| 69 | +#endif |
| 70 | + |
| 71 | +struct async_context; |
| 72 | + |
| 73 | +/*! \brief Initialize the status LED(s) |
| 74 | + * \ingroup pico_status_led |
| 75 | + * |
| 76 | + * Initialize the status LED(s) and the resources they need before use. On some devices (e.g. Pico W, Pico 2 W) accessing |
| 77 | + * the status LED requires talking to the WiFi chip, which requires an \ref async_context. |
| 78 | + * This method will create an async_context for you. |
| 79 | + * |
| 80 | + * However an application should only use a single \ref async_context instance to talk to the WiFi chip. |
| 81 | + * If the application already has an async context (e.g. created by cyw43_arch_init) you should use \ref |
| 82 | + * status_led_init_with_context instead and pass it the \ref async_context already created by your application |
| 83 | + * |
| 84 | + * \note You must call this function (or \ref status_led_init_with_context) before using any other pico_status_led functions. |
| 85 | + * |
| 86 | + * \return Returns true if the LED was initialized successfully, otherwise false on failure |
| 87 | + * \sa status_led_init_with_context |
| 88 | + */ |
| 89 | +bool status_led_init(void); |
| 90 | + |
| 91 | +/*! \brief Initialise the status LED(s) |
| 92 | + * \ingroup pico_status_led |
| 93 | + * |
| 94 | + * Initialize the status LED(s) and the resources they need before use. |
| 95 | + * |
| 96 | + * \note You must call this function (or \ref status_led_init) before using any other pico_status_led functions. |
| 97 | + * |
| 98 | + * \param context An \ref async_context used to communicate with the status LED (e.g. on Pico W or Pico 2 W) |
| 99 | + * \return Returns true if the LED was initialized successfully, otherwise false on failure |
| 100 | + * \sa status_led_init_with_context |
| 101 | + */ |
| 102 | +bool status_led_init_with_context(struct async_context *context); |
| 103 | + |
| 104 | +/*! \brief Determine if the `colored_status_led_` APIs are supported (i.e. if there is a colored status LED, and its |
| 105 | + * use isn't disabled via \ref PICO_COLORED_STATUS_LED_AVAILABLE being set to 0 |
| 106 | + * \ingroup pico_status_led |
| 107 | + * \return true if the colored status LED API is available and expected to produce visible results |
| 108 | + * \sa PICO_COLORED_STATUS_LED_AVAILABLE |
| 109 | + */ |
| 110 | +static inline bool colored_status_led_supported(void) { |
| 111 | + return PICO_COLORED_STATUS_LED_AVAILABLE; |
| 112 | +} |
| 113 | + |
| 114 | +/*! \brief Determine if the colored status LED is being used for the single-color `status_led_` APIs |
| 115 | + * \ingroup pico_status_led |
| 116 | + * \return true if the colored status LED is being used for the single-color `status_led_` API |
| 117 | + * \sa PICO_STATUS_LED_VIA_COLORED_STATUS_LED |
| 118 | + */ |
| 119 | +static inline bool status_led_via_colored_status_led(void) { |
| 120 | + return PICO_STATUS_LED_VIA_COLORED_STATUS_LED; |
| 121 | +} |
| 122 | + |
| 123 | +/*! \brief Determine if the single-color `status_led_` APIs are supported (i.e. if there is a regular LED, and its |
| 124 | + * use isn't disabled via \ref PICO_STATUS_LED_AVAILABLE being set to 0, or if the colored status LED is being used for |
| 125 | + * the single-color `status_led_` APIs |
| 126 | + * \ingroup pico_status_led |
| 127 | + * \return true if the single-color status LED API is available and expected to produce visible results |
| 128 | + * \sa PICO_STATUS_LED_AVAILABLE |
| 129 | + * \sa PICO_STATUS_LED_VIA_COLORED_STATUS_LED |
| 130 | + */ |
| 131 | +static inline bool status_led_supported(void) { |
| 132 | + if (status_led_via_colored_status_led()) { |
| 133 | + return colored_status_led_supported(); |
| 134 | + } |
| 135 | + return PICO_STATUS_LED_AVAILABLE; |
| 136 | +} |
| 137 | + |
| 138 | +/*! \brief Set the colored status LED on or off |
| 139 | + * \ingroup pico_status_led |
| 140 | + * |
| 141 | + * \note If your hardware does not support a colored status LED (PICO_DEFAULT_WS2812_PIN), this function does nothing and returns false. |
| 142 | + * |
| 143 | + * \param led_on true to turn the colored LED on. Pass false to turn the colored LED off |
| 144 | + * \return true if the colored status LED could be set, otherwise false |
| 145 | + */ |
| 146 | +bool colored_status_led_set_state(bool led_on); |
| 147 | + |
| 148 | +/*! \brief Get the state of the colored status LED |
| 149 | + * \ingroup pico_status_led |
| 150 | + * |
| 151 | + * \note If your hardware does not support a colored status LED (PICO_DEFAULT_WS2812_PIN), this function returns false. |
| 152 | + * |
| 153 | + * \return true if the colored status LED is on, or false if the colored status LED is off |
| 154 | + */ |
| 155 | +bool colored_status_led_get_state(void); |
| 156 | + |
| 157 | +/*! \brief Ensure the colored status LED is on, with the specified color |
| 158 | + * \ingroup pico_status_led |
| 159 | + * |
| 160 | + * \note If your hardware does not support a colored status LED (PICO_DEFAULT_WS2812_PIN), this function does nothing and returns false. |
| 161 | + * |
| 162 | + * \param color The color to use for the colored status LED when it is on, in 0xWWRRGGBB format |
| 163 | + * \return true if the colored status LED could be set, otherwise false on failure |
| 164 | + */ |
| 165 | +bool colored_status_led_set_on_with_color(uint32_t color); |
| 166 | + |
| 167 | +/*! \brief Get the color used for the status LED value when it is on |
| 168 | + * \ingroup pico_status_led |
| 169 | + * |
| 170 | + * \note If your hardware does not support a colored status LED (PICO_DEFAULT_WS2812_PIN), this function always returns 0x0. |
| 171 | + * |
| 172 | +* \return The color used for the colored status LED when it is on, in 0xWWRRGGBB format |
| 173 | +*/ |
| 174 | +uint32_t colored_status_led_get_on_color(void); |
| 175 | + |
| 176 | +/*! \brief Set the status LED on or off |
| 177 | +* \ingroup pico_status_led |
| 178 | +* |
| 179 | +* \note If your hardware does not support a status LED, this function does nothing and returns false. |
| 180 | +* |
| 181 | +* \param led_on true to turn the LED on. Pass false to turn the LED off |
| 182 | +* \return true if the status LED could be set, otherwise false |
| 183 | +*/ |
| 184 | +bool status_led_set_state(bool led_on); |
| 185 | + |
| 186 | +/*! \brief Get the state of the status LED |
| 187 | + * \ingroup pico_status_led |
| 188 | + * |
| 189 | + * \note If your hardware does not support a status LED, this function always returns false. |
| 190 | + * |
| 191 | + * \return true if the status LED is on, or false if the status LED is off |
| 192 | + */ |
| 193 | +bool status_led_get_state(); |
| 194 | + |
| 195 | +/*! \brief De-initialize the status LED(s) |
| 196 | + * \ingroup pico_status_led |
| 197 | + * |
| 198 | + * De-initializes the status LED(s) when they are no longer needed. |
| 199 | + */ |
| 200 | +void status_led_deinit(); |
| 201 | + |
| 202 | +#ifdef __cplusplus |
| 203 | +} |
| 204 | +#endif |
| 205 | + |
| 206 | +#endif |
0 commit comments