Implementation:78 Xiaozhi esp32 LED Framework
| Knowledge Sources | |
|---|---|
| Domains | LED_Indicator, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete LED indicator implementations for XiaoZhi voice assistant devices, providing three driver types — single RGB addressable LED, multi-LED circular strip, and PWM GPIO LED — with visual effects including blink, breathe, scroll, and rainbow.
Description
The LED framework under main/led/ provides three Led subclasses:
| Class | File | Purpose |
|---|---|---|
SingleLed |
single_led.h/.cc | Single RGB addressable LED (WS2812/etc.) via led_strip RMT driver; supports SetColor(r,g,b), blink, continuous blink
|
CircularStrip |
circular_strip.h/.cc | Multi-LED circular addressable strip via led_strip; supports per-LED color setting (SetSingleColor, SetMultiColors), blink, breathe, scroll, and rainbow effects with brightness control
|
GpioLed |
gpio_led.h/.cc | Single-color GPIO LED via LEDC PWM with brightness control, blink, fade effects; supports output inversion and custom LEDC timer/channel |
All three implement OnStateChanged() from the Led interface to reflect device state changes (connecting, listening, speaking, idle, etc.). They use esp_timer and FreeRTOS tasks for animation effects.
Usage
Reference this implementation when:
- Adding a board with an LED indicator (single RGB, circular strip, or GPIO PWM LED)
- Understanding LED visual effect APIs (blink, breathe, scroll, rainbow, fade)
- Configuring LED brightness levels for different power states
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Files: single_led.h/.cc, circular_strip.h/.cc, gpio_led.h/.cc
Signature
// Single RGB addressable LED
class SingleLed : public Led {
public:
SingleLed(gpio_num_t gpio);
void OnStateChanged() override;
private:
void SetColor(uint8_t r, uint8_t g, uint8_t b);
void Blink(int times, int interval_ms);
void StartContinuousBlink(int interval_ms);
led_strip_handle_t led_strip_;
};
// Multi-LED circular strip
struct StripColor { uint8_t red = 0, green = 0, blue = 0; };
class CircularStrip : public Led {
public:
CircularStrip(gpio_num_t gpio, uint16_t max_leds);
void OnStateChanged() override;
void SetBrightness(uint8_t default_brightness, uint8_t low_brightness);
void SetAllColor(StripColor color);
void SetSingleColor(uint8_t index, StripColor color);
void SetMultiColors(const std::vector<StripColor>& colors);
void Blink(StripColor color, int interval_ms);
void Breathe(StripColor low, StripColor high, int interval_ms);
void Scroll(StripColor low, StripColor high, int length, int interval_ms);
};
// PWM GPIO LED
class GpioLed : public Led {
public:
GpioLed(gpio_num_t gpio);
GpioLed(gpio_num_t gpio, int output_invert);
GpioLed(gpio_num_t gpio, int output_invert, ledc_timer_t timer_num, ledc_channel_t channel);
void OnStateChanged() override;
void TurnOn();
void TurnOff();
void SetBrightness(uint8_t brightness);
};
Import
#include "led/single_led.h" // SingleLed
#include "led/circular_strip.h" // CircularStrip
#include "led/gpio_led.h" // GpioLed
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| gpio | gpio_num_t | Yes | GPIO pin for LED data/signal |
| max_leds | uint16_t | CircularStrip | Number of LEDs in the strip |
| output_invert | int | GpioLed | Invert PWM polarity (default 0) |
| timer_num/channel | ledc_timer_t/ledc_channel_t | GpioLed | Custom LEDC resources |
| color | StripColor | CircularStrip | RGB color for effects |
| brightness | uint8_t | All | 0-255 brightness level |
Outputs
| Name | Type | Description |
|---|---|---|
| LED state | Physical | LED(s) driven via RMT or LEDC with visual effects |
Usage Examples
Single RGB LED
#include "led/single_led.h"
auto led = std::make_unique<SingleLed>(CONFIG_LED_PIN);
led->OnStateChanged(); // reflects current device state
Board::GetInstance()->SetLed(std::move(led));
Circular Strip with Effects
#include "led/circular_strip.h"
auto led = std::make_unique<CircularStrip>(CONFIG_LED_PIN, 12); // 12 LEDs
led->SetBrightness(32, 4); // default=32, low=4
led->Breathe({0, 0, 255}, {0, 0, 0}, 50); // blue breathe effect
Board::GetInstance()->SetLed(std::move(led));
GPIO PWM LED
#include "led/gpio_led.h"
auto led = std::make_unique<GpioLed>(CONFIG_LED_PIN, 0); // no invert
led->SetBrightness(128);
Board::GetInstance()->SetLed(std::move(led));