Skip to content

Commit

Permalink
Don't write config to flash if it hasn't changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rovo89 committed Oct 11, 2024
1 parent 1c0cfac commit 1ff7d31
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -843,6 +844,7 @@ void readConfigFromFlash() {
buffer[size - 2] != (crc & 0xFF))
return;

config_crc_in_flash = crc;
applyConfig(buffer, size);
free(buffer);
}

0 comments on commit 1ff7d31

Please sign in to comment.