Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:78 Xiaozhi esp32 Display Framework

From Leeroopedia
Knowledge Sources
Domains Display, UI_Framework, Embedded_Systems
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete display framework for XiaoZhi voice assistant devices, providing a class hierarchy from the abstract Display interface through LVGL-based LCD/OLED/E-Paper displays and a non-LVGL Emote display, with theme management, font/image abstractions, and glyph caching.

Description

The display framework spans multiple files under main/display/:

Class File Purpose
LvglDisplay lvgl_display.h/.cc Base LVGL display: status bar, notifications, battery/network icons, power-save lock, dynamic glyph cache, text font
LcdDisplay / SpiLcdDisplay / RgbLcdDisplay / MipiLcdDisplay lcd_display.h/.cc Color LCD with top bar, status bar, content area, side bar, chat messages, emoji, preview image; supports SPI/RGB/MIPI panel interfaces
OledDisplay oled_display.h/.cc Monochrome OLED (128x64 or 128x32) with emotion label and chat message; IsMonochrome() = true
EpdDisplay epd_display.h/.cc E-Paper display via EPDiy, inherits LcdDisplay; supports full/partial refresh with waveform LUTs (abstract GetFullWaveform/GetPartialWaveform)
EmoteDisplay emote_display.h/.cc Non-LVGL display using expression_emote for animation-based emotion rendering; supports animated dialog and asset mounting
LvglTheme / LvglThemeManager lvgl_theme.h/.cc Theme system: colors, fonts, background image, emoji collection; singleton manager with named themes
LvglImage / LvglRawImage / LvglCBinImage / LvglAllocatedImage lvgl_image.h/.cc LVGL image descriptors wrapping raw data, cbin format, or allocated buffers
LvglFont / LvglBuiltInFont / LvglCBinFont lvgl_font.h/.cc Font abstraction wrapping built-in LVGL fonts or cbin format fonts with fallback support
EmojiCollection emoji_collection.h/.cc Named emoji image registry (AddEmoji/GetEmojiImage)
DynamicGlyphCache dynamic_glyph_cache.h/.cc LRU-style glyph cache (max 256 glyphs, 64KB bitmap) that builds a runtime lv_font_t from server-pushed glyph data
TextGlyph / TextGlyphAllocator text_glyph.h Glyph data struct and PSRAM-aware allocator for glyph storage
LvglGif gif/lvgl_gif.h/.cc LVGL GIF widget with play/pause/resume/stop, loop count/delay, frame callback
gif/gifdec.h/.c C API GIF decoder (LZW, palette, frame rendering) used by LvglGif
image_to_jpeg / image_to_jpeg_cb jpg/image_to_jpeg.h/.cpp JPEG encoder: converts RGB565/RGB888/YUYV/YUV420 to JPEG with quality control; callback version for streaming
jpg/jpeg_to_image.h/.c JPEG decoder: decodes JPEG to RGB565 with optional hardware acceleration

Usage

Reference this implementation when:

  • Implementing a board with an LCD, OLED, E-Paper, or Emote display
  • Understanding the display class hierarchy and which subclass to use
  • Customizing themes, fonts, emoji, or glyph rendering
  • Converting camera frames to JPEG for transmission or display

Code Reference

Source Location

  • Repository: XiaoZhi ESP32
  • Directory: main/display/
  • Key Files: lvgl_display.h/.cc, lcd_display.h/.cc, oled_display.h/.cc, epd_display/epd_display.h/.cc, emote_display.h/.cc, lvgl_theme.h/.cc, lvgl_image.h/.cc, lvgl_font.h/.cc, emoji_collection.h/.cc, dynamic_glyph_cache.h/.cc, text_glyph.h, gif/lvgl_gif.h/.cc, gif/gifdec.h/.c, jpg/image_to_jpeg.h/.cpp, jpg/jpeg_to_image.h/.c

Signature

// Base LVGL display
class LvglDisplay : public Display {
public:
    virtual void SetStatus(const char* status);
    virtual void ShowNotification(const char* notification, int duration_ms = 3000);
    virtual void UpdateStatusBar(bool update_all = false);
    virtual void SetPowerSaveMode(bool on);
    virtual bool SupportsGuiOperations() const override { return true; }
protected:
    virtual bool Lock(int timeout_ms = 0) = 0;
    virtual void Unlock() = 0;
    lv_display_t* display_ = nullptr;
    std::unique_ptr<DynamicGlyphCache> dynamic_glyph_cache_;
};

// Color LCD (SPI/RGB/MIPI)
class LcdDisplay : public LvglDisplay {
public:
    virtual void SetEmotion(const char* emotion) override;
    virtual void SetChatMessage(const char* role, const char* content) override;
    virtual void SetPreviewImage(std::unique_ptr<LvglImage> image) override;
    virtual void SetupUI() override;
    virtual void SetTheme(Theme* theme) override;
};

class SpiLcdDisplay : public LcdDisplay {
public:
    SpiLcdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
                  int width, int height, int offset_x, int offset_y,
                  bool mirror_x, bool mirror_y, bool swap_xy);
};

// Monochrome OLED
class OledDisplay : public LvglDisplay {
public:
    OledDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
                int width, int height, bool mirror_x, bool mirror_y);
    virtual bool IsMonochrome() const override { return true; }
};

// E-Paper display
class EpdDisplay : public LcdDisplay {
public:
    EpdDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
               int width, int height, custom_lcd_spi_t _lcd_spi_data);
    void Initialize();
    virtual void EPD_Init();
    virtual void EPD_Display();
    virtual void EPD_Init_Partial();
protected:
    virtual Waveform GetFullWaveform() const = 0;
    virtual Waveform GetPartialWaveform() const = 0;
};

// Non-LVGL Emote display
class EmoteDisplay : public Display {
public:
    EmoteDisplay(esp_lcd_panel_handle_t panel, esp_lcd_panel_io_handle_t panel_io,
                 int width, int height);
    virtual void SetEmotion(const char* emotion) override;
    virtual bool InsertAnimDialog(const char* emoji_name, uint32_t duration_ms) override;
    virtual bool MountAssets(const char* partition_label) override;
};

// Theme system
class LvglTheme : public Theme {
public:
    LvglTheme(const std::string& name);
    static lv_color_t ParseColor(const std::string& color);
    lv_color_t background_color() const;
    std::shared_ptr<LvglFont> text_font() const;
    std::shared_ptr<EmojiCollection> emoji_collection() const;
};

class LvglThemeManager {
public:
    static LvglThemeManager& GetInstance();
    void RegisterTheme(const std::string& theme_name, LvglTheme* theme);
    LvglTheme* GetTheme(const std::string& theme_name);
};

// Dynamic glyph cache
class DynamicGlyphCache {
public:
    static constexpr size_t kMaxGlyphs = 256;
    static constexpr size_t kMaxBitmapBytes = 64 * 1024;
    lv_font_t* EnsureFont(const lv_font_t* base_font, uint8_t bpp);
    bool AddGlyphs(const std::vector<TextGlyph>& glyphs);
    void Clear();
};

// JPEG encoder/decoder
bool image_to_jpeg(uint8_t* src, size_t src_len, uint16_t width, uint16_t height,
                   v4l2_pix_fmt_t format, uint8_t quality, uint8_t** out, size_t* out_len);
esp_err_t jpeg_to_image(const uint8_t* src, size_t src_len, uint8_t** out,
                        size_t* out_len, size_t* width, size_t* height, size_t* stride);

Import

#include "lcd_display.h"       // SpiLcdDisplay, RgbLcdDisplay, MipiLcdDisplay
#include "oled_display.h"      // OledDisplay
#include "epd_display/epd_display.h"  // EpdDisplay
#include "emote_display.h"     // EmoteDisplay
#include "lvgl_display/lvgl_theme.h"  // LvglTheme, LvglThemeManager
#include "lvgl_display/lvgl_image.h"  // LvglImage, LvglRawImage
#include "lvgl_display/lvgl_font.h"   // LvglFont, LvglCBinFont
#include "lvgl_display/dynamic_glyph_cache.h"  // DynamicGlyphCache
#include "lvgl_display/gif/lvgl_gif.h"  // LvglGif
#include "lvgl_display/jpg/image_to_jpeg.h"  // image_to_jpeg
#include "lvgl_display/jpg/jpeg_to_image.h"  // jpeg_to_image

I/O Contract

Inputs

Name Type Required Description
panel_io esp_lcd_panel_io_handle_t Yes SPI/I2C panel IO handle
panel esp_lcd_panel_handle_t Yes LCD panel handle
width/height int Yes Display dimensions in pixels
mirror_x/mirror_y/swap_xy bool No Display orientation flags
theme LvglTheme* Theme object with colors, fonts, emoji
glyphs (AddGlyphs) std::vector<TextGlyph> Server-pushed glyph data for dynamic fonts
src (image_to_jpeg) uint8_t* Yes Raw image pixels to encode
format v4l2_pix_fmt_t Yes Pixel format (RGB565, RGB888, YUYV, etc.)
quality uint8_t Yes JPEG quality (1-100)

Outputs

Name Type Description
LVGL display lv_display_t* Initialized LVGL display with flush callback
JPEG data uint8_t* Encoded JPEG bytes (caller must free)
RGB565 data uint8_t* Decoded image (caller must heap_caps_free)

Usage Examples

SPI LCD Display Setup

#include "lcd_display.h"

// Create SPI panel and IO (board-specific)
esp_lcd_panel_io_handle_t panel_io;
esp_lcd_panel_handle_t panel;
// ... initialize SPI bus and panel ...

auto display = std::make_unique<SpiLcdDisplay>(
    panel_io, panel,
    320, 240,     // width, height
    0, 0,         // offset_x, offset_y
    false, false, // mirror_x, mirror_y
    false         // swap_xy
);
display->SetupUI();
Board::GetInstance()->SetDisplay(std::move(display));

OLED Display (128x64)

#include "oled_display.h"

auto display = std::make_unique<OledDisplay>(
    panel_io, panel,
    128, 64,       // width, height
    false, false   // mirror_x, mirror_y
);
display->SetupUI();

Converting Camera Frame to JPEG

#include "jpg/image_to_jpeg.h"

uint8_t* jpeg_out = nullptr;
size_t jpeg_len = 0;
if (image_to_jpeg(frame_buf, frame_len,
                   width, height,
                   V4L2_PIX_FMT_RGB565, 80,
                   &jpeg_out, &jpeg_len)) {
    // Send jpeg_out over protocol...
    free(jpeg_out);  // caller must free
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment