Implementation:78 Xiaozhi esp32 Board Interface
| Knowledge Sources | |
|---|---|
| Domains | Hardware_Abstraction, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for abstracting board-specific hardware provided by the XiaoZhi firmware, defining the Board abstract interface, the DECLARE_BOARD factory macro, and network event/power save enums.
Description
The Board class is the central hardware abstraction interface. Core firmware code retrieves the singleton via Board::GetInstance(), which calls the board factory function create_board() (expanded by DECLARE_BOARD). The interface provides access to:
- Audio codec:
GetAudioCodec() -> AudioCodec*(pure virtual, must implement) - Display:
GetDisplay() -> Display*(returnsnullptrif absent) - Network:
GetNetwork() -> NetworkInterface*(pure virtual) - LED:
GetLed() -> Led*(base impl returns a default) - Camera, Backlight, Battery: Optional, return
nullptrorfalse - Network control:
StartNetwork()(pure virtual),SetNetworkEventCallback() - Power management:
SetPowerSaveLevel(PowerSaveLevel)(pure virtual)
The DECLARE_BOARD(BOARD_CLASS_NAME) macro expands to a create_board() function that heap-constructs the concrete board class. Exactly one DECLARE_BOARD must appear in each build.
Board base classes (WifiBoard, Ml307Board, EthernetBoard, DualNetworkBoard, RndisBoard) provide shared network implementation; concrete boards inherit from one and implement the remaining pure virtuals.
Usage
Reference this implementation when:
- Adding a new board variant (implement
Boardinterface) - Accessing hardware from core code (use
Board::GetInstance().GetAudioCodec()) - Understanding which capabilities are optional vs mandatory
- Tracing the network event callback wiring
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Files:
main/boards/common/board.h,main/boards/common/board.cc,main/boards/common/wifi_board.cc
Signature
// Network event types
enum class NetworkEvent {
Scanning, Connecting, Connected, Disconnected,
WifiConfigModeEnter, WifiConfigModeExit,
ModemDetecting, ModemErrorNoSim, ModemErrorRegDenied,
ModemErrorInitFailed, ModemErrorTimeout
};
enum class PowerSaveLevel { LOW_POWER, BALANCED, PERFORMANCE };
using NetworkEventCallback = std::function<void(NetworkEvent, const std::string&)>;
class Board {
public:
static Board& GetInstance();
virtual ~Board() = default;
virtual std::string GetBoardType() = 0;
virtual std::string GetUuid();
virtual Backlight* GetBacklight();
virtual Led* GetLed();
virtual AudioCodec* GetAudioCodec() = 0;
virtual bool GetTemperature(float& temp);
virtual Display* GetDisplay();
virtual Camera* GetCamera();
virtual NetworkInterface* GetNetwork() = 0;
virtual void StartNetwork() = 0;
virtual void SetNetworkEventCallback(NetworkEventCallback callback);
virtual const char* GetNetworkStateIcon() = 0;
virtual bool GetBatteryLevel(int& level, bool& charging, bool& discharging);
virtual std::string GetSystemInfoJson();
virtual void SetPowerSaveLevel(PowerSaveLevel level) = 0;
virtual std::string GetBoardJson() = 0;
virtual std::string GetDeviceStatusJson() = 0;
};
#define DECLARE_BOARD(BOARD_CLASS_NAME) \
void* create_board() { return new BOARD_CLASS_NAME(); }
Import
#include "board.h"
// For a concrete board implementation:
#include "wifi_board.h" // or ml307_board.h, ethernet_board.h, etc.
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| callback | NetworkEventCallback | No | Unified network event handler |
| level | PowerSaveLevel | Power saving level to set |
Outputs
| Name | Type | Description |
|---|---|---|
| GetInstance() | Singleton board instance (lazy via create_board())
| |
| GetAudioCodec() | Audio codec instance (never null) | |
| GetDisplay() | Display instance or nullptr | |
| GetNetwork() | Network interface (never null) |
Usage Examples
Accessing Hardware from Core Code
// Core code always uses the abstract interface
auto& board = Board::GetInstance();
auto codec = board.GetAudioCodec();
auto display = board.GetDisplay();
if (display != nullptr) {
display->SetupUI();
}
board.StartNetwork();
Implementing a New Board
#include "wifi_board.h"
#include "config.h"
class MyCustomBoard : public WifiBoard {
public:
std::string GetBoardType() override {
return "my-custom-board";
}
AudioCodec* GetAudioCodec() override {
// Return singleton codec for this board
static MyCodec codec;
return &codec;
}
const char* GetNetworkStateIcon() override {
return "wifi";
}
void SetPowerSaveLevel(PowerSaveLevel level) override {
// Adjust WiFi power saving based on level
}
std::string GetBoardJson() override {
return "{\"audio_codec\":\"my_codec\"}";
}
std::string GetDeviceStatusJson() override {
return "{}";
}
};
// Exactly one DECLARE_BOARD per build
DECLARE_BOARD(MyCustomBoard)