/* Setup the display */ auto display = board.GetDisplay(); // ...
/* Wait for the network to be ready */ board.StartNetwork();
// Check for new firmware version or get the MQTT broker address CheckNewVersion();
// Initialize the protocol display->SetStatus(Lang::Strings::LOADING_PROTOCOL);
if (ota_.HasMqttConfig()) { protocol_ = std::make_unique<MqttProtocol>(); } elseif (ota_.HasWebsocketConfig()) { protocol_ = std::make_unique<WebsocketProtocol>(); } else { ESP_LOGW(TAG, "No protocol specified in the OTA config, using MQTT"); protocol_ = std::make_unique<MqttProtocol>(); }
// ... 协议初始化和回调设置 ...
// Wait for the new version check to finish xEventGroupWaitBits(event_group_, CHECK_NEW_VERSION_DONE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY); SetDeviceState(kDeviceStateIdle);
if (protocol_started) { std::string message = std::string(Lang::Strings::VERSION) + ota_.GetCurrentVersion(); display->ShowNotification(message.c_str()); display->SetChatMessage("system", ""); // Play the success sound to indicate the device is ready ResetDecoder(); PlaySound(Lang::Sounds::P3_SUCCESS); } // Enter the main event loop MainEventLoop(); }
2. MainEventLoop() 主事件循环
执行Start() 后,程序会进入 MainEventLoop():
1 2 3 4 5 6 7 8 9 10 11 12 13 14
voidApplication::MainEventLoop(){ while (true) { auto bits = xEventGroupWaitBits(event_group_, SCHEDULE_EVENT, pdTRUE, pdFALSE, portMAX_DELAY);