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 Assets

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

Overview

Concrete tool for downloading and applying firmware assets (speech recognition models, fonts, UI themes) provided by the XiaoZhi firmware, managing a dedicated flash partition with strategy-based application for LVGL and Emote display paths.

Description

The Assets singleton manages non-firmware resources stored on a separate flash partition. It supports:

  • Download(url, callback): Downloads an assets binary via HTTP, validates a 12-byte header, erases the assets partition, and writes the binary in sector-sized chunks. Reports progress via callback.
  • Apply(refresh_display_theme): Delegates to a strategy pattern (AssetStrategy) based on the display type:
    • LvglStrategy: Memory-maps the partition, loads font bundles, applies LVGL themes, and loads speech recognition models (srmodels.bin) via LoadSrmodelsFromIndex.
    • EmoteStrategy: Applies assets for the non-LVGL Emote display path.
  • GetAssetData(name, ptr, size): Retrieves a named asset from the partition.
  • partition_valid(): Returns true if the assets partition has valid content.

After applying, speech recognition models are loaded into models_list_ and passed to AudioService::SetModelsList() for use by the audio engine.

Usage

Reference this implementation when:

  • Understanding asset download and partition management
  • Tracing how speech recognition models are loaded
  • Implementing a new display strategy for assets
  • Understanding the asset partition validation

Code Reference

Source Location

Signature

struct Asset { size_t size; size_t offset; };
struct TextFontCapability {
    bool glyph_push = false;
    std::string bundle;
    std::string charset;
    int size = 0;
    int bpp = 0;
};

class Assets {
public:
    static Assets& GetInstance();
    ~Assets();

    bool Download(std::string url,
                  std::function<void(int progress, size_t speed)> progress_callback);
    bool Apply(bool refresh_display_theme = true);
    bool GetAssetData(const std::string& name, void*& ptr, size_t& size);

    bool partition_valid() const;
    std::string default_assets_url() const;
    TextFontCapability text_font_capability() const;

private:
    Assets();
    bool InitializePartition();
    void UnApplyPartition();
    static bool FindPartition(Assets* assets);
    static bool LoadSrmodelsFromIndex(Assets* assets, cJSON* root = nullptr);
    // Strategy pattern for LVGL vs Emote
    class AssetStrategy { /* ... */ };
    class LvglStrategy : public AssetStrategy { /* ... */ };
    class EmoteStrategy : public AssetStrategy { /* ... */ };
    std::unique_ptr<AssetStrategy> strategy_;
    const esp_partition_t* partition_ = nullptr;
    bool partition_valid_ = false;
    srmodel_list_t* models_list_ = nullptr;
};

Import

#include "assets.h"

I/O Contract

Inputs

Name Type Required Description
url std::string HTTP URL of assets binary
progress_callback std::function<void(int, size_t)> Progress (percentage, bytes/sec)
refresh_display_theme bool Whether to refresh LVGL theme after apply

Outputs

Name Type Description
Download() True if assets written to partition successfully
Apply() True if assets applied (fonts, themes, models loaded)
GetAssetData() True if named asset found; sets ptr and size
models_list_ Loaded SR models, passed to AudioService

Usage Examples

Downloading and Applying Assets

// In Application::CheckAssetsVersion()
auto& assets = Assets::GetInstance();
std::string url = settings.GetString("download_url");
if (!url.empty()) {
    bool success = assets.Download(url, [](int progress, size_t speed) {
        ESP_LOGI(TAG, "Assets download: %d%%", progress);
    });
    if (success) {
        assets.Apply(true);  // refresh display theme
    }
}

Loading Speech Recognition Models

// After Assets::Apply(), models are loaded
auto& assets = Assets::GetInstance();
if (assets.partition_valid()) {
    // Models loaded internally; pass to audio service
    // (done inside Apply via LoadSrmodelsFromIndex)
    audio_service_.SetModelsList(assets_models_list);
}

Related Pages

Implements Principle

Requires Environment

Page Connections

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