From 58c1cd81c68f484753abecdc5e75ec0ac964ea8d Mon Sep 17 00:00:00 2001 From: Terrence Date: Mon, 13 Jan 2025 05:00:14 +0800 Subject: [PATCH] reduce memory usage of ota --- main/application.cc | 32 ++++++++++++++++++-------------- main/ota.cc | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/main/application.cc b/main/application.cc index 580004b3..a2cb6726 100644 --- a/main/application.cc +++ b/main/application.cc @@ -62,23 +62,27 @@ void Application::CheckNewVersion() { vTaskDelay(pdMS_TO_TICKS(3000)); } while (GetDeviceState() != kDeviceStateIdle); - SetDeviceState(kDeviceStateUpgrading); - - display->SetIcon(FONT_AWESOME_DOWNLOAD); - display->SetStatus("新版本 " + ota_.GetFirmwareVersion()); + // Use main task to do the upgrade + Schedule([this, &board, display]() { + SetDeviceState(kDeviceStateUpgrading); + + display->SetIcon(FONT_AWESOME_DOWNLOAD); + display->SetStatus("新版本 " + ota_.GetFirmwareVersion()); - // 预先关闭音频输出,避免升级过程有音频操作 - board.GetAudioCodec()->EnableOutput(false); + // 预先关闭音频输出,避免升级过程有音频操作 + board.GetAudioCodec()->EnableOutput(false); + vTaskDelay(pdMS_TO_TICKS(1000)); - ota_.StartUpgrade([display](int progress, size_t speed) { - char buffer[64]; - snprintf(buffer, sizeof(buffer), "%d%% %zuKB/s", progress, speed / 1024); - display->SetStatus(buffer); + ota_.StartUpgrade([display](int progress, size_t speed) { + char buffer[64]; + snprintf(buffer, sizeof(buffer), "%d%% %zuKB/s", progress, speed / 1024); + display->SetStatus(buffer); + }); + + // If upgrade success, the device will reboot and never reach here + ESP_LOGI(TAG, "Firmware upgrade failed..."); + SetDeviceState(kDeviceStateIdle); }); - - // If upgrade success, the device will reboot and never reach here - ESP_LOGI(TAG, "Firmware upgrade failed..."); - SetDeviceState(kDeviceStateIdle); } else { ota_.MarkCurrentVersionValid(); display->ShowNotification("版本 " + ota_.GetCurrentVersion()); diff --git a/main/ota.cc b/main/ota.cc index 5a82adfe..b7e37af7 100644 --- a/main/ota.cc +++ b/main/ota.cc @@ -165,7 +165,7 @@ void Ota::Upgrade(const std::string& firmware_url) { return; } - std::vector buffer(4096); + std::vector buffer(512); size_t total_read = 0, recent_read = 0; auto last_calc_time = esp_timer_get_time(); while (true) {