From d22a9e1ee4772d002e1b0397985719ef95ce0f70 Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Tue, 28 May 2024 22:03:51 +0200 Subject: [PATCH] Clear memory before doing OTA --- CYD-Klipper/src/core/files_query.cpp | 12 +++++++--- CYD-Klipper/src/core/files_query.h | 3 ++- CYD-Klipper/src/core/macros_query.cpp | 32 +++++++++++++++++---------- CYD-Klipper/src/core/macros_query.h | 4 +++- CYD-Klipper/src/ui/ota_setup.cpp | 8 +++++++ 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CYD-Klipper/src/core/files_query.cpp b/CYD-Klipper/src/core/files_query.cpp index 57e0983..204124b 100644 --- a/CYD-Klipper/src/core/files_query.cpp +++ b/CYD-Klipper/src/core/files_query.cpp @@ -9,9 +9,8 @@ // Always has +1 entry with a null'd name FILESYSTEM_FILE* last_query = NULL; -FILESYSTEM_FILE* get_files(int limit){ - freeze_request_thread(); - +void clear_files() +{ if (last_query != NULL){ FILESYSTEM_FILE* current = last_query; @@ -21,7 +20,14 @@ FILESYSTEM_FILE* get_files(int limit){ } free(last_query); + last_query = NULL; } +} + +FILESYSTEM_FILE* get_files(int limit) +{ + freeze_request_thread(); + clear_files(); Serial.printf("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()); std::list files; diff --git a/CYD-Klipper/src/core/files_query.h b/CYD-Klipper/src/core/files_query.h index efb8b12..822cad8 100644 --- a/CYD-Klipper/src/core/files_query.h +++ b/CYD-Klipper/src/core/files_query.h @@ -19,4 +19,5 @@ typedef struct _FILESYSTEM_FILE { float modified; } FILESYSTEM_FILE; -FILESYSTEM_FILE* get_files(int limit); \ No newline at end of file +FILESYSTEM_FILE* get_files(int limit); +void clear_files(); \ No newline at end of file diff --git a/CYD-Klipper/src/core/macros_query.cpp b/CYD-Klipper/src/core/macros_query.cpp index f3998b3..c1d5fa8 100644 --- a/CYD-Klipper/src/core/macros_query.cpp +++ b/CYD-Klipper/src/core/macros_query.cpp @@ -12,6 +12,15 @@ static char* power_devices[16] = {0}; static bool power_device_states[16] = {0}; static unsigned int stored_power_devices_count = 0; +void macros_clear() +{ + for (int i = 0; i < macros_count; i++){ + free(macros[i]); + } + + macros_count = 0; +} + MACROSQUERY macros_query(PRINTER_CONFIG * config) { HTTPClient client; @@ -24,11 +33,7 @@ MACROSQUERY macros_query(PRINTER_CONFIG * config) deserializeJson(doc, client.getStream()); auto result = doc["result"].as(); - for (int i = 0; i < macros_count; i++){ - free(macros[i]); - } - - macros_count = 0; + macros_clear(); for (JsonPair i : result){ const char *key = i.key().c_str(); @@ -85,6 +90,15 @@ unsigned int macro_count() return macro_count(get_current_printer_config()); } +void power_devices_clear() +{ + for (int i = 0; i < stored_power_devices_count; i++){ + free(power_devices[i]); + } + + stored_power_devices_count = 0; +} + POWERQUERY power_devices_query(PRINTER_CONFIG * config) { HTTPClient client; @@ -97,11 +111,7 @@ POWERQUERY power_devices_query(PRINTER_CONFIG * config) deserializeJson(doc, client.getStream()); auto result = doc["result"]["devices"].as(); - for (int i = 0; i < stored_power_devices_count; i++){ - free(power_devices[i]); - } - - stored_power_devices_count = 0; + power_devices_clear(); for (auto i : result){ const char * device_name = i["device"]; @@ -154,8 +164,6 @@ unsigned int power_devices_count() return power_devices_count(get_current_printer_config()); } - - bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config) { HTTPClient client; diff --git a/CYD-Klipper/src/core/macros_query.h b/CYD-Klipper/src/core/macros_query.h index 13ae98a..9022cc5 100644 --- a/CYD-Klipper/src/core/macros_query.h +++ b/CYD-Klipper/src/core/macros_query.h @@ -22,4 +22,6 @@ POWERQUERY power_devices_query(); unsigned int power_devices_count(PRINTER_CONFIG * config); unsigned int power_devices_count(); bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config); -bool set_power_state(const char* device_name, bool state); \ No newline at end of file +bool set_power_state(const char* device_name, bool state); +void macros_clear(); +void power_devices_clear(); \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ota_setup.cpp b/CYD-Klipper/src/ui/ota_setup.cpp index 40f9398..c381002 100644 --- a/CYD-Klipper/src/ui/ota_setup.cpp +++ b/CYD-Klipper/src/ui/ota_setup.cpp @@ -5,6 +5,9 @@ #include "../core/data_setup.h" #include "../conf/global_config.h" #include "ota_setup.h" +#include "../core/macros_query.h" +#include "../core/files_query.h" +#include "gcode_img.h" //const char *ota_url = "https://gist.githubusercontent.com/suchmememanyskill/ece418fe199e155340de6c224a0badf2/raw/0d6762d68bc807cbecc71e40d55b76692397a7b3/update.json"; // Test url const char *ota_url = "https://suchmememanyskill.github.io/CYD-Klipper/OTA.json"; // Prod url @@ -74,6 +77,11 @@ void ota_do_update(bool variant_automatic) lv_timer_handler(); lv_task_handler(); + macros_clear(); + power_devices_clear(); + clear_files(); + clear_img_mem(); + ota_pull.SetCallback(do_update_callback); ota_pull.CheckForOTAUpdate(ota_url, REPO_VERSION, ESP32OTAPull::ActionType::UPDATE_AND_BOOT); }