This commit is contained in:
Dave Allie 2025-12-22 17:10:35 +11:00
parent fcf37907c8
commit 8c0d631ce2
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
3 changed files with 11 additions and 9 deletions

View File

@ -193,17 +193,12 @@ void OtaUpdateActivity::loop() {
if (state == WAITING_CONFIRMATION) { if (state == WAITING_CONFIRMATION) {
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) { if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
Serial.printf("[%lu] [OTA] New update available, starting download...\n", millis()); Serial.printf("[%lu] [OTA] New update available, starting download...\n", millis());
int lastUpdatePercentage = 0;
xSemaphoreTake(renderingMutex, portMAX_DELAY); xSemaphoreTake(renderingMutex, portMAX_DELAY);
state = UPDATE_IN_PROGRESS; state = UPDATE_IN_PROGRESS;
xSemaphoreGive(renderingMutex); xSemaphoreGive(renderingMutex);
updateRequired = true; updateRequired = true;
vTaskDelay(10 / portTICK_PERIOD_MS); vTaskDelay(10 / portTICK_PERIOD_MS);
const auto res = updater.installUpdate([this, &lastUpdatePercentage](const size_t progress, const size_t total) { const auto res = updater.installUpdate([this](const size_t, const size_t) { updateRequired = true; });
// Only trigger display updates every 2% at most
updateRequired = true;
// vTaskDelay(10 / portTICK_PERIOD_MS);
});
if (res != OtaUpdater::OK) { if (res != OtaUpdater::OK) {
Serial.printf("[%lu] [OTA] Update failed: %d\n", millis(), res); Serial.printf("[%lu] [OTA] Update failed: %d\n", millis(), res);

View File

@ -18,12 +18,15 @@ class OtaUpdateActivity : public ActivityWithSubactivity {
SHUTTING_DOWN SHUTTING_DOWN
}; };
// Can't initialize this to 0 or the first render doesn't happen
static constexpr unsigned int UNINITIALIZED_PERCENTAGE = 111;
TaskHandle_t displayTaskHandle = nullptr; TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr; SemaphoreHandle_t renderingMutex = nullptr;
bool updateRequired = false; bool updateRequired = false;
const std::function<void()> goBack; const std::function<void()> goBack;
State state = WIFI_SELECTION; State state = WIFI_SELECTION;
unsigned int lastUpdaterPercentage = 111; // Can't initialize this to 0 or the first render doesn't happen unsigned int lastUpdaterPercentage = UNINITIALIZED_PERCENTAGE;
OtaUpdater updater; OtaUpdater updater;
void onWifiSelectionComplete(bool success); void onWifiSelectionComplete(bool success);

View File

@ -27,7 +27,7 @@ OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() {
} }
JsonDocument doc; JsonDocument doc;
const DeserializationError error = deserializeJson(doc, http.getStream()); const DeserializationError error = deserializeJson(doc, *client);
http.end(); http.end();
if (error) { if (error) {
Serial.printf("[%lu] [OTA] JSON parse failed: %s\n", millis(), error.c_str()); Serial.printf("[%lu] [OTA] JSON parse failed: %s\n", millis(), error.c_str());
@ -121,6 +121,7 @@ OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(s
if (httpCode != HTTP_CODE_OK) { if (httpCode != HTTP_CODE_OK) {
Serial.printf("[%lu] [OTA] Download failed: %d\n", millis(), httpCode); Serial.printf("[%lu] [OTA] Download failed: %d\n", millis(), httpCode);
http.end();
return HTTP_ERROR; return HTTP_ERROR;
} }
@ -129,12 +130,14 @@ OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(s
if (contentLength != otaSize) { if (contentLength != otaSize) {
Serial.printf("[%lu] [OTA] Invalid content length\n", millis()); Serial.printf("[%lu] [OTA] Invalid content length\n", millis());
http.end();
return HTTP_ERROR; return HTTP_ERROR;
} }
// 3. Begin the ESP-IDF Update process // 3. Begin the ESP-IDF Update process
if (!Update.begin(otaSize)) { if (!Update.begin(otaSize)) {
Serial.printf("[%lu] [OTA] Not enough space. Error: %s\n", millis(), Update.errorString()); Serial.printf("[%lu] [OTA] Not enough space. Error: %s\n", millis(), Update.errorString());
http.end();
return INTERNAL_UPDATE_ERROR; return INTERNAL_UPDATE_ERROR;
} }
@ -146,11 +149,12 @@ OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(s
onProgress(progress, total); onProgress(progress, total);
}); });
const size_t written = Update.writeStream(*client); const size_t written = Update.writeStream(*client);
http.end();
if (written == otaSize) { if (written == otaSize) {
Serial.printf("[%lu] [OTA] Successfully written %u bytes\n", millis(), written); Serial.printf("[%lu] [OTA] Successfully written %u bytes\n", millis(), written);
} else { } else {
Serial.printf("[%lu] [OTA] Written only %u/%u bytes. Error: %s\n", millis(), written, contentLength, Serial.printf("[%lu] [OTA] Written only %u/%u bytes. Error: %s\n", millis(), written, otaSize,
Update.errorString()); Update.errorString());
return INTERNAL_UPDATE_ERROR; return INTERNAL_UPDATE_ERROR;
} }