Overview
Concrete reusable hardware utility classes for boards, provided by the XiaoZhi firmware under main/boards/common/, covering I2C device access, display backlight, buttons, rotary knobs, battery monitoring, power management ICs, power-save timers, sleep timers, system reset, and MCP tool controllers.
Description
The main/boards/common/ directory contains reusable utilities that concrete board implementations instantiate and wire together. They are not board-specific — any board can use them by passing the appropriate pins and I2C addresses from config.h.
| Class |
File |
Purpose
|
I2cDevice |
i2c_device.h/.cc |
Base class for I2C peripherals: ReadReg/WriteReg/ReadRegs/WriteRegs + ResetBus
|
Backlight / PwmBacklight |
backlight.h/.cc |
Display backlight with smooth brightness transitions via esp_timer; PWM implementation using LEDC
|
Button / AdcButton / PowerSaveButton |
button.h/.cc |
GPIO/ADC button wrapper around iot_button with press/long-press/click/double-click/multi-click callbacks
|
Knob |
knob.h/.cc |
Rotary encoder wrapper around iot_knob with OnRotate(bool clockwise) callback
|
AdcBatteryMonitor |
adc_battery_monitor.h/.cc |
ADC-based battery level estimation with voltage divider, charging-pin status, and periodic check timer
|
Sy6970 |
sy6970.h/.cc |
I2C driver for SY6970 battery charger: charging status, battery level, power-off
|
Xpowers |
xpower.h |
Generic XPowers PMU wrapper (AXP192/AXP202/AXP2101) bridging xpowerslib to I2cDevice via static read/write callbacks
|
Axp2101 |
axp2101.h/.cc |
Thin wrapper for AXP2101 PMU using Xpowers base
|
PowerSaveTimer |
power_save_timer.h/.cc |
Inactivity timer that enters sleep mode after N seconds and optionally requests shutdown; fires callbacks on state transitions
|
SleepTimer |
sleep_timer.h/.cc |
Light/deep sleep timer with enter/exit callbacks for each sleep stage
|
SystemReset |
system_reset.h/.cc |
GPIO button monitor for NVS reset and factory reset; calls RestartInSeconds after erasing
|
LampController |
lamp_controller.h |
GPIO lamp controller that auto-registers self.lamp.get_state/turn_on/turn_off MCP tools
|
PressToTalkMcpTool |
press_to_talk_mcp_tool.h/.cc |
MCP tool self.set_press_to_talk that toggles press-to-talk mode and persists to NVS
|
Usage
Reference this implementation when:
- Adding a new board that needs I2C peripherals, buttons, backlight, battery monitoring, or power management
- Understanding the reusable utility layer that sits between the Board interface and board-specific code
- Implementing MCP tools for lamp control or press-to-talk mode
- Configuring power-save or sleep timers for inactivity management
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Directory:
main/boards/common/
- Files: i2c_device.h/.cc, backlight.h/.cc, button.h/.cc, knob.h/.cc, adc_battery_monitor.h/.cc, sy6970.h/.cc, xpower.h, axp2101.h/.cc, power_save_timer.h/.cc, sleep_timer.h/.cc, system_reset.h/.cc, lamp_controller.h, press_to_talk_mcp_tool.h/.cc
Signature
// I2C device base class
class I2cDevice {
public:
I2cDevice(i2c_master_bus_handle_t i2c_bus, uint8_t addr);
protected:
void WriteReg(uint8_t reg, uint8_t value);
void WriteRegs(uint8_t reg, const uint8_t* buffer, size_t length);
uint8_t ReadReg(uint8_t reg);
void ReadRegs(uint8_t reg, uint8_t* buffer, size_t length);
esp_err_t ResetBus(const char* reason);
};
// Display backlight with smooth transitions
class Backlight {
public:
void RestoreBrightness();
void SetBrightness(uint8_t brightness, bool permanent = false);
uint8_t brightness() const;
protected:
virtual void SetBrightnessImpl(uint8_t brightness) = 0;
};
class PwmBacklight : public Backlight {
public:
PwmBacklight(gpio_num_t pin, bool output_invert = false, uint32_t freq_hz = 25000);
};
// GPIO/ADC button
class Button {
public:
Button(gpio_num_t gpio_num, bool active_high = false,
uint16_t long_press_time = 0, uint16_t short_press_time = 0,
bool enable_power_save = false);
void OnPressDown(std::function<void()> callback);
void OnLongPress(std::function<void()> callback);
void OnClick(std::function<void()> callback);
void OnDoubleClick(std::function<void()> callback);
void OnMultipleClick(std::function<void()> callback, uint8_t click_count = 3);
};
// Rotary encoder
class Knob {
public:
Knob(gpio_num_t pin_a, gpio_num_t pin_b);
void OnRotate(std::function<void(bool)> callback); // bool = clockwise
};
// ADC battery monitor
class AdcBatteryMonitor {
public:
AdcBatteryMonitor(adc_unit_t adc_unit, adc_channel_t adc_channel,
float upper_resistor, float lower_resistor,
gpio_num_t charging_pin = GPIO_NUM_NC);
bool IsCharging();
uint8_t GetBatteryLevel();
void OnChargingStatusChanged(std::function<void(bool)> callback);
};
// Power-save timer
class PowerSaveTimer {
public:
PowerSaveTimer(int cpu_max_freq, int seconds_to_sleep = 20, int seconds_to_shutdown = -1);
void SetEnabled(bool enabled);
void OnEnterSleepMode(std::function<void()> callback);
void OnExitSleepMode(std::function<void()> callback);
void OnShutdownRequest(std::function<void()> callback);
void WakeUp();
};
// Sleep timer (light/deep)
class SleepTimer {
public:
SleepTimer(int seconds_to_light_sleep = 20, int seconds_to_deep_sleep = -1);
void SetEnabled(bool enabled);
void OnEnterLightSleepMode(std::function<void()> callback);
void OnEnterDeepSleepMode(std::function<void()> callback);
void WakeUp();
};
// System reset via GPIO buttons
class SystemReset {
public:
SystemReset(gpio_num_t reset_nvs_pin, gpio_num_t reset_factory_pin);
void CheckButtons();
};
// SY6970 battery charger (I2C)
class Sy6970 : public I2cDevice {
public:
Sy6970(i2c_master_bus_handle_t i2c_bus, uint8_t addr);
bool IsCharging();
int GetBatteryLevel();
void PowerOff();
};
// Generic XPowers PMU wrapper
class Xpowers : public I2cDevice {
public:
Xpowers(i2c_master_bus_handle_t i2c_bus, uint8_t addr);
XPowersPMU pmu; // high-level API access
bool IsCharging();
int GetBatteryLevel();
void PowerOff();
};
// Lamp MCP controller
class LampController {
public:
LampController(gpio_num_t gpio_num); // auto-registers MCP tools
};
// Press-to-talk MCP tool
class PressToTalkMcpTool {
public:
PressToTalkMcpTool();
void Initialize();
bool IsPressToTalkEnabled() const;
};
Import
#include "i2c_device.h"
#include "backlight.h"
#include "button.h"
#include "knob.h"
#include "adc_battery_monitor.h"
#include "power_save_timer.h"
#include "sleep_timer.h"
#include "system_reset.h"
#include "sy6970.h"
// For XPowers: define chip macro before include
// #define XPOWERS_CHIP_AXP2101
// #include "xpower.h"
#include "lamp_controller.h"
#include "press_to_talk_mcp_tool.h"
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| i2c_bus |
i2c_master_bus_handle_t |
For I2C devices |
Shared I2C bus handle from board
|
| addr |
uint8_t |
For I2C devices |
Device I2C address
|
| gpio_num |
gpio_num_t |
For GPIO devices |
Pin number from config.h
|
| adc_unit/channel |
adc_unit_t/adc_channel_t |
For ADC |
ADC unit and channel for battery
|
| upper/lower_resistor |
float |
For AdcBatteryMonitor |
Voltage divider resistor values
|
| seconds_to_sleep/shutdown |
int |
For timers |
Inactivity thresholds in seconds
|
Outputs
| Name |
Type |
Description
|
| Callbacks |
std::function |
Event-driven callbacks for button presses, knob rotation, charging status, sleep/shutdown transitions
|
| Battery level |
uint8_t/int |
0-100 percentage
|
| Charging status |
bool |
IsCharging()/IsDischarging()
|
Usage Examples
Board Setup with Backlight, Button, and Battery
#include "backlight.h"
#include "button.h"
#include "adc_battery_monitor.h"
// In board constructor:
backlight_ = std::make_unique<PwmBacklight>(CONFIG_BACKLIGHT_PIN, false, 25000);
backlight_->SetBrightness(100, true);
button_ = std::make_unique<Button>(CONFIG_BUTTON_PIN, false, 1000, 50);
button_->OnClick([this]() { /* handle click */ });
button_->OnLongPress([this]() { /* handle long press */ });
battery_ = std::make_unique<AdcBatteryMonitor>(
ADC_UNIT_1, ADC_CHANNEL_0,
100.0, 100.0, // voltage divider resistors (kOhm)
CONFIG_CHARGING_PIN
);
battery_->OnChargingStatusChanged([](bool charging) {
ESP_LOGI("Board", "Charging: %d", charging);
});
I2C PMU with XPowers
#define XPOWERS_CHIP_AXP2101
#include "xpower.h"
// In board constructor:
pmu_ = std::make_unique<Xpowers>(i2c_bus, 0x34);
if (pmu_->IsBatteryConnect()) {
ESP_LOGI("Board", "Battery: %d%%", pmu_->GetBatteryLevel());
}
// Use pmu_->pmu for chip-specific API:
// pmu_->pmu.setChargerConstantCurr(200);
// pmu_->pmu.enableDC3();
Lamp Controller MCP Tool
#include "lamp_controller.h"
// In board constructor — auto-registers self.lamp.* MCP tools:
lamp_ = std::make_unique<LampController>(CONFIG_LAMP_PIN);
// Now the AI assistant can control the lamp via MCP:
// self.lamp.get_state -> {"power": false}
// self.lamp.turn_on -> true
// self.lamp.turn_off -> true
Related Pages
Implements Principle
Requires Environment