From b4aa711940c95dc2e91a099621e74399d3333273 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 9 Oct 2019 05:57:20 +0200 Subject: [PATCH] Lots of settings stuff: The webinterface at /settings now lets you change the settings. Also, these changes will be saved to SPIFFS and loaded after a reboot. --- include/settings.h | 3 ++ src/http_server.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++--- src/pitrix.cpp | 2 ++ src/settings.cpp | 71 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 146 insertions(+), 7 deletions(-) diff --git a/include/settings.h b/include/settings.h index bf1b3f0..e2e4c64 100644 --- a/include/settings.h +++ b/include/settings.h @@ -81,3 +81,6 @@ extern Setting all_settings[]; extern const uint8_t all_settings_size; bool change_setting(const char* key, uint16_t new_value); +uint16_t setting_default(Setting* s); +bool save_settings(); +bool load_settings(); diff --git a/src/http_server.cpp b/src/http_server.cpp index 6846230..20c90ab 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -58,13 +58,53 @@ void http_server_setup() { http_server.send(200, "text/html", message); }); http_server.on("/settings", HTTP_GET, [&]() { - String message = "Pitrix settings

Pitrix settings

Back to main page"; + String message = "Pitrix settings

Pitrix settings

Back to main page
\n"; for (int i=0; i Settings settings; @@ -45,7 +46,7 @@ Setting all_settings[] = { const uint8_t all_settings_size = 25; bool change_setting(const char* key, uint16_t new_value) { - LOGln("Settings * Trying to set setting %s to new value %d...", key, new_value); + LOGln("Settings * Setting %s to new value %d.", key, new_value); Setting* s = NULL; for (uint8_t i=0; ivalue) = new_value; - LOGln("Settings * Success. New value for %s is %d.", key, new_value); + return true; +} + +uint16_t setting_default(Setting* s) { + Settings defaults; + auto p_settings = std::addressof(settings); + auto p_defaults = std::addressof(defaults); + uint16_t* p_this = s->value; + uint16_t diff = p_this - (uint16_t*)p_settings; + uint16_t default_value = *((uint16_t*)p_defaults + diff); + return default_value; +} + +bool save_settings() { + if (!SPIFFS.begin()) { + LOGln("Settings * Could not open SPIFFS for saving."); + return false; + } + + File f = SPIFFS.open("/pitrix_settings.txt", "w"); + if (!f) { + LOGln("Settings * Could not open /pitrix_settings.txt for writing."); + return false; + } + + for (int i=0; i 0) { + int pos = s.lastIndexOf('\n'); + String part = s.substring(pos + 1); + if (pos==-1) pos=0; + s.remove(pos); + pos = part.indexOf('='); + if (pos < 1) continue; + String key = part.substring(0, pos); + uint16_t value = part.substring(pos + 1).toInt(); + change_setting(key.c_str(), value); + } return true; }