From 863d5eff78e6c49345d4020697f5eee19d7f57cf Mon Sep 17 00:00:00 2001 From: Kenneth Barbour Date: Fri, 12 Jun 2020 21:35:13 -0400 Subject: [PATCH] commit changes to eeprom --- src/config.cpp | 7 ++++++- src/main.cpp | 2 +- src/web.cpp | 13 +++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index f0d0957..bab4da5 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -6,12 +6,14 @@ cUWeatherConfig Config; const uint8_t CONFIG_SIG[2] = {0xAB, 0xCD}; const uint8_t CONFIG_VERSION = 1; +const size_t CONFIG_EEPROM_SECTOR_SZ = 256; uint8_t CONFIG_EEPROM_ADDR = 0; const uint8_t CONFIG_FLAG_HTTP = 1 << 0; void cUWeatherConfig::begin() { + EEPROM.begin(CONFIG_EEPROM_SECTOR_SZ); if (!load()) { Serial.println("Unable to find config. Loading defaults"); loadDefaults(); @@ -58,8 +60,11 @@ bool cUWeatherConfig::load() bool cUWeatherConfig::save() { + Serial.println("Saving config."); EEPROM.put(CONFIG_EEPROM_ADDR, _data); - return true; + bool result = EEPROM.commit(); + Serial.println(result ? "OK" : "ERROR"); + return result; } size_t cUWeatherConfig::print(Stream& out) diff --git a/src/main.cpp b/src/main.cpp index 293cc55..d8c1461 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,7 +37,7 @@ void setup() uweather_print_version_info(Serial); // Load configuration - Serial.print( F("Loading config")); + Serial.println( F("Loading config")); Config.begin(); diff --git a/src/web.cpp b/src/web.cpp index aba7245..7c260ff 100644 --- a/src/web.cpp +++ b/src/web.cpp @@ -139,6 +139,18 @@ void handle_config(HttpRequest& req, HttpResponse& res) return; } +void handle_config_commit(HttpRequest& req, HttpResponse& res) +{ + bool result = Config.save(); + res.headers.set("Content-Type", "application/json"); + if (!result) { + res.code = 500; + content.println("{error: true, message: \"Unable to commit configuration\"}"); + } else { + Config.print(content); + } +} + void handle_config_set(HttpRequest& req, HttpResponse& res) { const char* key = req.getUrl()+8; @@ -244,6 +256,7 @@ Route routes[] = { { GET, "/", handle_index}, { GET, "/setup", handle_setup}, { GET, "/config", handle_config}, + { GET | PUT | POST, "/config/commit", handle_config_commit}, { GET | PUT | POST | DELETE, "/config/#", handle_config_set}, { GET, "/wifi/scan", handle_wifi_scan}, { GET | POST | DELETE, "/wifi", handle_wifi_state},