Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-led.h File Reference

Detailed Description

Design Philosophy

LED code is based on Espressif's RMT Transmit Example - LED Strip.

Each LED has a red, green, and blue component. Each component ranges from 0 to 255.

LED brightness is not linear, so differences in brightness are more noticeable between small values (like 1 and 10) than between large values (like 240 and 250).

The number of LEDs are configurable by "idf.py menuconfig" and is accessible in code with the CONFIG_NUM_LEDS macro.

Usage

You don't need to call initLeds() or deinitLeds(). The system does so at the appropriate time.

You should call setLeds() any time you want to set the LEDs. There is no buffer, so the LEDs are immediately set to the values given. setLeds() takes a pointer to an array of led_t as an argument. These structs each have a red, green, and blue field.

setLedBrightness() may be called to adjust overall LED brightness. Brightness is adjusted per-color-channel, so dimming may produce different colors. setLedBrightnessSetting() should be called instead if the brightness change should be persistent through reboots.

flushLeds() may be called to wait until all pending LED transactions are completed. This does not need to be called under normal operation. The RMT peripheral handles updating LEDs in the background automatically, but transactions must be flushed before entering light sleep. If they are not, garbage data may be sent after light sleep begins, resulting in indeterminate LED behavior.

Example

Set the LEDs to a rough rainbow:

led_t leds[CONFIG_NUM_LEDS] = {0};
for (uint8_t i = 0; i < CONFIG_NUM_LEDS; i++)
{
leds[i].r = (255 * ((i + 0) % CONFIG_NUM_LEDS)) / (CONFIG_NUM_LEDS - 1);
leds[i].g = (255 * ((i + 3) % CONFIG_NUM_LEDS)) / (CONFIG_NUM_LEDS - 1);
leds[i].b = (255 * ((i + 6) % CONFIG_NUM_LEDS)) / (CONFIG_NUM_LEDS - 1);
}
setLeds(leds, CONFIG_NUM_LEDS);
esp_err_t setLeds(led_t *leds, uint8_t numLeds)
Set the RGB LEDs to the given values.
Definition hdw-led.c:107
uint8_t g
The green component, 0-255.
Definition hdw-led.h:64
uint8_t b
The blue component, 0-255.
Definition hdw-led.h:66
uint8_t r
The red component, 0-255.
Definition hdw-led.h:65
LED colors, with red, green, and blue components.
Definition hdw-led.h:63

Go to the source code of this file.

Data Structures

struct  led_t
 LED colors, with red, green, and blue components. More...
 

Macros

#define MAX_LED_BRIGHTNESS   8
 The maximum LED brightness setting.
 

Functions

esp_err_t initLeds (gpio_num_t gpio, gpio_num_t gpioAlt, uint8_t brightness)
 Initialize the RGB LEDs.
 
esp_err_t deinitLeds (void)
 Deinitialize LEDs.
 
esp_err_t setLeds (led_t *leds, uint8_t numLeds)
 Set the RGB LEDs to the given values.
 
void setLedBrightness (uint8_t brightness)
 Set the global LED brightness. setLedBrightnessSetting() should be called instead if the new volume should be persistent through a reboot.
 
uint8_t getLedState (led_t *leds, uint8_t numLeds)
 Write the current LED state into the given array.
 
void flushLeds (void)
 Wait until any pending LED transactions are finished, then return.
 

Data Structure Documentation

◆ led_t

struct led_t
Data Fields
uint8_t g The green component, 0-255.
uint8_t r The red component, 0-255.
uint8_t b The blue component, 0-255.

Macro Definition Documentation

◆ MAX_LED_BRIGHTNESS

#define MAX_LED_BRIGHTNESS   8

The maximum LED brightness setting.

Function Documentation

◆ initLeds()

esp_err_t initLeds ( gpio_num_t gpio,
gpio_num_t gpioAlt,
uint8_t brightness )

Initialize the RGB LEDs.

Parameters
gpioThe GPIO the LEDs are attached to
gpioAltA GPIO to mirror the LED output to
brightnessThe brightness to start the LEDs at
Returns
ESP_OK if the LEDs initialized, or a nonzero value if they did not

◆ deinitLeds()

esp_err_t deinitLeds ( void )

Deinitialize LEDs.

Returns
ESP_OK

◆ setLeds()

esp_err_t setLeds ( led_t * leds,
uint8_t numLeds )

Set the RGB LEDs to the given values.

Parameters
ledsA pointer to an array of led_t structs to set the LEDs to. The array must have at least numLeds elements
numLedsThe number of LEDs to set, probably CONFIG_NUM_LEDS
Returns
ESP_OK if the LEDs were set, or a nonzero value if they did were not

◆ setLedBrightness()

void setLedBrightness ( uint8_t brightness)

Set the global LED brightness. setLedBrightnessSetting() should be called instead if the new volume should be persistent through a reboot.

Parameters
brightness0 (off) to MAX_LED_BRIGHTNESS (max bright)

◆ getLedState()

uint8_t getLedState ( led_t * leds,
uint8_t numLeds )

Write the current LED state into the given array.

Parameters
[out]ledsThe LED array to write the state into
numLedsThe maximum number of LEDs to write
Returns
uint8_t The number of LEDs actually written to the array

◆ flushLeds()

void flushLeds ( void )

Wait until any pending LED transactions are finished, then return.