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 settingsPitrix settings
Back to main page";
+ String message = "Pitrix settingsPitrix settings
Back to main page\n";
for (int i=0; i";
- message += all_settings[i].name;
+ if (default_value != value) {
+ message += "";
+ }
+ message += s.name;
+ if (default_value != value) {
+ message += "";
+ }
message += " | ";
- message += *all_settings[i].value;
- message += " | ";
+ message += value;
+ if (default_value != value) {
+ message += " (";
+ message += default_value;
+ message += ")";
+ }
+ message += " | \n";
}
message += "
";
http_server.send(200, "text/html", message);
@@ -78,11 +118,38 @@ void http_server_setup() {
uint16_t value = http_server.arg("value").toInt();
if (change_setting(name.c_str(), value)) {
- http_server.send(200, "text/plain", "OK");
+ if (http_server.hasArg("redir")) {
+ http_server.sendHeader("Location", "/settings");
+ http_server.send(301);
+ } else {
+ http_server.send(200, "text/plain", "OK");
+ }
+ save_settings();
} else {
http_server.send(400, "text/plain", "Could not change setting.");
}
});
+ http_server.on("/settings/load", HTTP_POST, [&]() {
+ load_settings();
+ http_server.send(200, "text/plain", "OK");
+ });
+ http_server.on("/settings/save", HTTP_POST, [&]() {
+ save_settings();
+ http_server.send(200, "text/plain", "OK");
+ });
+ http_server.on("/settings.txt", HTTP_GET, [&]() {
+ File f;
+ if (SPIFFS.begin()) {
+ f=SPIFFS.open("/pitrix_settings.txt", "r");
+ if (f) {
+ String s = f.readString();
+ f.close();
+ http_server.send(200, "text/plain", s);
+ return;
+ }
+ }
+ http_server.send(500, "text/plain", "Could not read settings.");
+ });
http_server.on("/delete", HTTP_GET, [&]() {
LOGln("HTTP * GET /delete");
if (http_server.args()==0) {
diff --git a/src/pitrix.cpp b/src/pitrix.cpp
index 65b4e2c..6a74bfa 100644
--- a/src/pitrix.cpp
+++ b/src/pitrix.cpp
@@ -10,6 +10,7 @@
#include "effects.h"
#include "http_server.h"
#include "recorder.h"
+#include "settings.h"
uint8_t starting_up = OTA_STARTUP_DELAY;
int loop_timeouts = 0;
@@ -53,6 +54,7 @@ void setup() {
recorder = new Recorder();
#endif
SPIFFS.begin();
+ load_settings();
LOGln("Core * Setup complete");
}
diff --git a/src/settings.cpp b/src/settings.cpp
index fd0ca02..287ca07 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1,5 +1,6 @@
#include "settings.h"
#include "config.h"
+#include
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;
}