Writing a Code-Driven Application: ESP32 (CMake)#
Build and entrypoint deltas for converting
examples/all-devices-app/esp32/ into a single-device product
application. See Writing a Code-Driven Application
for architecture and device class implementation.
1. CMake Build Deltas (esp32/main/CMakeLists.txt)#
Remove Simulator & Example Scaffolding:
Delete
include("${ALL_DEVICES_COMMON_DIR}/device-factory/enabled_devices.cmake")and remove${ALL_DEVICES_EXTRA_INCLUDE_DIRS},${ALL_DEVICES_DEVICE_SOURCES}, and${ALL_DEVICES_CLUSTER_SOURCES}.Remove simulator device-factory, shell device-switching, and example peripheral directories/files (such as
display/anddevices/chime/) fromesp32/main/andCMakeLists.txt.
Append Base & Product Device Sources:
Product
.cppfiles placed inesp32/main/are compiled automatically viaSRC_DIRS_LIST("${CMAKE_CURRENT_LIST_DIR}"). KeepAPP_TOPLEVEL_EXTRA_SRCS(which selectsThreadFeature.cpporWifiFeature.cppbased onCONFIG_ENABLE_MATTER_OVER_THREAD) and append thedevice/api/*base sources,RootNode.cpp, and the single base device.cpp(plus any underlyingdevice/capabilities/<capability>/*.cppsource if the device wraps a shared capability; checkenabled_devices.cmakefor each device’s source list; note thatconfig/esp32/components/chipalready compiles the SDK cluster servers intolibCHIP.a):
list(APPEND APP_TOPLEVEL_EXTRA_SRCS
"${ALL_DEVICES_COMMON_DIR}/device/api/Interface.cpp"
"${ALL_DEVICES_COMMON_DIR}/device/api/SingleEndpoint.cpp"
"${ALL_DEVICES_COMMON_DIR}/device/types/root-node/RootNode.cpp"
"${ALL_DEVICES_COMMON_DIR}/device/types/speaker/Speaker.cpp"
)
2. Entrypoint Deltas (esp32/main/main.cpp)#
Keep:
Platform initialization, persistence setup,
dataModelProvidercreation, andgRootNode(WifiRootNode) registration onkRootEndpointId(0) inPopulateCodeDrivenDataModelProvider().Factory data provider wiring (
ESP32FactoryDataProviderandESP32DeviceInfoProvider), enablingCONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER,CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER, andCONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDERin production builds soapp_main()does not fall back toExamples::GetExampleDACProvider().
Remove:
All
AppDeviceFactory/DeviceFactoryregistration,<app_config/enabled_devices.h>, NVSdev-typedevice-selection state, shell device-switching commands, and sample display UI hooks inmain.cpp.
Replace:
After the existing
gRootNode->Register(...)call inPopulateCodeDrivenDataModelProvider(), instantiate and register the product device onEndpointId(1):
// ... keep existing gRootNode->Register(...) above ...
gProductDevice = std::make_unique<MyProductSpeaker>(gTimerDelegate);
if (gProductDevice->Register(EndpointId(1), dataModelProvider) != CHIP_NO_ERROR)
{
gRootNode->Unregister(dataModelProvider);
return nullptr;
}
return &dataModelProvider;