From 1ff7d3181e6e85466d4faa70444a1be03dea20fa Mon Sep 17 00:00:00 2001 From: Robert Vollmer Date: Fri, 11 Oct 2024 23:33:19 +0200 Subject: [PATCH] Don't write config to flash if it hasn't changed --- Firmware/LowLevel/src/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Firmware/LowLevel/src/main.cpp b/Firmware/LowLevel/src/main.cpp index 4b5edc38..6d113ea9 100644 --- a/Firmware/LowLevel/src/main.cpp +++ b/Firmware/LowLevel/src/main.cpp @@ -120,6 +120,7 @@ uint16_t ui_interval = 1000; // UI send msg (LED/State) interval ( struct ll_high_level_config llhl_config; // LL/HL configuration (is initialized with YF-C500 defaults) const String CONFIG_FILENAME = "/openmower.cfg"; +uint16_t config_crc_in_flash = 0; void sendMessage(void *message, size_t size); void sendUIMessage(void *message, size_t size); @@ -812,7 +813,7 @@ void sendUIMessage(void *message, size_t size) { void saveConfigToFlash() { uint16_t crc = CRC16.ccitt((const uint8_t*) &llhl_config, sizeof(llhl_config)); - // TODO: Return early if CRC is unchanged to avoid flash wear. + if (crc == config_crc_in_flash) return; File f = LittleFS.open(CONFIG_FILENAME, "w"); f.write((const uint8_t*) &llhl_config, sizeof(llhl_config)); @@ -843,6 +844,7 @@ void readConfigFromFlash() { buffer[size - 2] != (crc & 0xFF)) return; + config_crc_in_flash = crc; applyConfig(buffer, size); free(buffer); }