Heuristic:78 Xiaozhi esp32 Optional Capability Null Pattern
| Knowledge Sources | |
|---|---|
| Domains | Embedded, Design_Patterns |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Optional hardware capabilities (display, backlight, camera, battery) return `nullptr` from the Board interface so core code can null-check rather than compile-time gate.
Description
The `Board` abstract interface declares virtual accessors for optional hardware: `GetBacklight()`, `GetDisplay()`, `GetCamera()`, `GetBatteryLevel()`. The base class defaults return `nullptr` for these. Concrete board implementations override only the accessors for hardware they actually have. Core modules (Application, display controllers, LED) always null-check the return value before using the capability. This is a runtime nullability pattern — not a compile-time `#ifdef` — so the same firmware binary's core code works across boards with and without displays, cameras, or batteries.
Usage
Use this heuristic when adding a new optional capability to the Board interface. Provide a base implementation returning `nullptr`, override it only in boards that have the hardware, and always guard the consumer with a null check. Never assume a capability is present based on the chip family — some ESP32-S3 boards have displays, some do not.
The Insight (Rule of Thumb)
- Action: Return `nullptr` from `Board::GetXxx()` for absent capabilities; check for null at every call site.
- Value: Base class defaults: `GetBacklight() → nullptr`, `GetDisplay() → nullptr` (overridden to create one), `GetCamera() → nullptr`, `GetBatteryLevel() → false`.
- Trade-off: Adds a runtime branch at every use site vs. compile-time `#ifdef`. Trades a tiny bit of overhead for binary compatibility across boards.
- Compatibility: Works on all targets. The AGENTS.md rule "Treat camera, backlight, display, LED, battery, and similar capabilities as optional" codifies this pattern.
Reasoning
The firmware supports 138+ board directories with varying hardware. Some have OLED/LCD displays, some have no display at all. Some have cameras, some have battery monitors. If every capability were compile-time gated, the build matrix would explode. Instead, the Board interface uses runtime nullability: the core code asks `board.GetDisplay()` and if it gets `nullptr`, it skips display updates. This keeps core modules board-agnostic and allows the same `application.cc` to run on a bare ESP32-C3 breadboard or a fully-featured M5Stack CoreS3.
Code Evidence
Optional capability declarations from `main/boards/common/board.h:70-80`:
virtual Backlight* GetBacklight() { return nullptr; }
virtual Led* GetLed();
virtual AudioCodec* GetAudioCodec() = 0;
virtual bool GetTemperature(float& esp32temp);
virtual Display* GetDisplay();
virtual Camera* GetCamera();
virtual NetworkInterface* GetNetwork() = 0;
virtual void StartNetwork() = 0;
virtual void SetNetworkEventCallback(NetworkEventCallback callback) { (void)callback; }
virtual const char* GetNetworkStateIcon() = 0;
virtual bool GetBatteryLevel(int &level, bool& charging, bool& discharging);
Note that `GetBacklight()` explicitly returns `nullptr` in the base, while `GetDisplay()` and `GetCamera()` delegate to the `.cc` implementation which also returns `nullptr` when no display/camera is configured. `GetAudioCodec()` and `GetNetwork()` are pure virtual — every board must provide audio and networking.
The AGENTS.md rule from the project instructions:
Treat camera, backlight, display, LED, battery, and similar capabilities as optional.
Core code depends on `Board` interfaces, never a concrete board class or board `config.h`.