From c5dc8bb309989222dd018c8dd15ec96336189007 Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Fri, 18 Mar 2022 15:32:49 +0200 Subject: [PATCH] Initial FW updater code complete and tested --- auto_version.py | 1 + include/Lang.h | 4 +++- include/Updater.h | 1 + include/Version.h | 6 +++--- src/Updater.cpp | 39 ++++++++++++++++++++++++++++----------- version | 2 +- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/auto_version.py b/auto_version.py index fcc70df..854501a 100644 --- a/auto_version.py +++ b/auto_version.py @@ -56,3 +56,4 @@ print ("Unable to generate version header!", file=sys.stderr) env.Exit(1) +print("Building for version: " + version + ", build: " + str(build_no)) diff --git a/include/Lang.h b/include/Lang.h index e466bd9..9d5d90c 100644 --- a/include/Lang.h +++ b/include/Lang.h @@ -54,16 +54,18 @@ namespace TapuinoNext const char S_UPDATE[] = "Update FW"; // Update related strings + const char S_UPDATE_UPDATING[] = "FW updating"; const char S_UPDATE_NOT_FOUND[] = "FW not found!"; const char S_UPDATE_TOO_SMALL[] = "FW too small!"; const char S_UPDATE_WRITTEN_OK[] = "FW write OK"; const char S_UPDATE_UNDERWRITE[] = "FW under write!"; const char S_UPDATE_WRITTEN[] = "FW written"; const char S_UPDATE_SUCCESS[] = "Update success!"; + const char S_UPDATE_STARTED[] = "Update started!"; const char S_UPDATE_REBOOT[] = "Rebooting...!"; const char S_UPDATE_INCOMPLETE[] = "Upd incomplete!"; const char S_UPDATE_FAILED[] = "Update failed!"; - const char S_NO_UPDATE_SPACE[] = "No FW space!"; + const char S_UPDATE_NO_SPACE[] = "No FW space!"; // Option related strings const char S_BTN_CLICK_TIME[] = "Btn Click Time"; diff --git a/include/Updater.h b/include/Updater.h index 33ffb3d..fe7b0d1 100644 --- a/include/Updater.h +++ b/include/Updater.h @@ -18,5 +18,6 @@ namespace TapuinoNext FileLoader* fileLoader; void PerformUpdate(); + void OnProgress(size_t progress, size_t size); }; } // namespace TapuinoNext \ No newline at end of file diff --git a/include/Version.h b/include/Version.h index 435eec7..c175f78 100644 --- a/include/Version.h +++ b/include/Version.h @@ -1,10 +1,10 @@ #ifndef FW_BUILD_NUMBER - #define FW_BUILD_NUMBER "13" + #define FW_BUILD_NUMBER "38" #endif #ifndef FW_VERSION - #define FW_VERSION "0.0.1-alpha+13" + #define FW_VERSION "0.0.1-alpha+38" #endif #ifndef FW_BUILD_TIME - #define FW_BUILD_TIME "2022-03-17 15:28:06.130329" + #define FW_BUILD_TIME "2022-03-18 15:12:16.635465" #endif diff --git a/src/Updater.cpp b/src/Updater.cpp index df7e745..960b1c4 100644 --- a/src/Updater.cpp +++ b/src/Updater.cpp @@ -1,3 +1,4 @@ +#include "config.h" #include "Updater.h" #include "Menu.h" #include "Lang.h" @@ -15,53 +16,67 @@ Updater::Updater(LCDUtils* lcdUtils, InputHandler* inputHandler, FileLoader* fil this->fileLoader = fileLoader; } +void Updater::OnProgress(size_t progress, size_t size) +{ + float prog = ((float) progress / (float) size) * 100; + char buf[I2C_DISP_COLS + 1]; + snprintf(buf, I2C_DISP_COLS + 1, "progress: %d%%", (int) prog); + lcdUtils->Status(buf); +} + // perform the actual update from a given stream void Updater::PerformUpdate() { File updateFile; if (ErrorCodes::OK != fileLoader->OpenFile("/update.bin", updateFile)) { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_NOT_FOUND); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_NOT_FOUND); return; } + Update.onProgress(std::bind(&Updater::OnProgress, this, std::placeholders::_1, std::placeholders::_2)); + size_t updateSize = updateFile.size(); if (updateSize == 0) { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_TOO_SMALL); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_TOO_SMALL); } else if (Update.begin(updateSize)) { + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_STARTED); if (Update.writeStream(updateFile) == updateSize) { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_WRITTEN_OK); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_WRITTEN_OK); } else { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_UNDERWRITE); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_UNDERWRITE); } if (Update.end()) { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_WRITTEN); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_WRITTEN); if (Update.isFinished()) { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_SUCCESS); - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_REBOOT); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_SUCCESS); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_REBOOT); + delay(1000); + ESP.restart(); + delay(1000); } else { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_INCOMPLETE); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_INCOMPLETE); } } else { - lcdUtils->Error(S_UPDATE_MENU, S_UPDATE_FAILED); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_FAILED); Serial.println("Error Occurred. Error #: " + String(Update.getError())); } } else { - lcdUtils->Error(S_UPDATE_MENU, S_NO_UPDATE_SPACE); + lcdUtils->Error(S_UPDATE_UPDATING, S_UPDATE_NO_SPACE); } updateFile.close(); } @@ -74,7 +89,7 @@ void Updater::OnAction() {MenuEntryType::IndexEntry, S_EXIT, NULL}, {MenuEntryType::IndexEntry, S_UPDATE, NULL}, }; - TheMenu inPlayMenu = {S_UPDATE_MENU, (MenuEntry*) inPlayEntries, 3, 0, NULL}; + TheMenu inPlayMenu = {S_UPDATE, (MenuEntry*) inPlayEntries, 2, 0, NULL}; while (true) { @@ -88,6 +103,8 @@ void Updater::OnAction() // Seek case 1: { + PerformUpdate(); + return; } // Abort (-1) default: diff --git a/version b/version index f7eb1a8..cba1987 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.0.1-alpha+13 \ No newline at end of file +0.0.1-alpha+38 \ No newline at end of file