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.

This commit is contained in:
Fabian Schlenz 2019-10-09 05:57:20 +02:00
parent 205a0df842
commit b4aa711940
4 changed files with 146 additions and 7 deletions

View File

@ -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();

View File

@ -58,13 +58,53 @@ void http_server_setup() {
http_server.send(200, "text/html", message);
});
http_server.on("/settings", HTTP_GET, [&]() {
String message = "<html><head><title>Pitrix settings</title></head><body><h1>Pitrix settings</h1><a href='/'>Back to main page</a><table>";
String message = "<html><head><title>Pitrix settings</title></head><body><h1>Pitrix settings</h1><a href='/'>Back to main page</a><table>\n";
for (int i=0; i<all_settings_size; i++) {
Setting s = all_settings[i];
uint16_t default_value = setting_default(&s);
uint16_t value = *(s.value);
message += "<tr><td>";
message += all_settings[i].name;
if (default_value != value) {
message += "<strong>";
}
message += s.name;
if (default_value != value) {
message += "<strong>";
}
message += "</td><td>";
message += *all_settings[i].value;
message += "</td></tr>";
message += value;
if (default_value != value) {
message += " (";
message += default_value;
message += ")";
}
message += "</td><td><form method='POST' action='/settings?redir=1'><input type='hidden' name='key' value='";
message += s.name;
message += "'/>";
if (s.type==TYPE_UINT8 || s.type==TYPE_UINT16) {
message += "<input type='number' name='value' value='";
message += value;
message += "' min='0' max='";
if (s.type==TYPE_UINT8) {
message += "255";
} else {
message += "65535";
}
message += "' />";
} else if (s.type==TYPE_BOOL) {
message += "<input type='radio' name='value' value='0'";
if (value==0) {
message += " checked='checked'";
}
message += "> Off / <input type='radio' name='value' value='1'";
if (value==1) {
message += " checked='checked'";
}
message += "> On";
}
message += "<input type='submit' value='Save' />";
message += "</form></td></tr>\n";
}
message += "</table></body></html>";
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)) {
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) {

View File

@ -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");
}

View File

@ -1,5 +1,6 @@
#include "settings.h"
#include "config.h"
#include <FS.h>
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; i<all_settings_size; i++) {
if (strcmp(key, all_settings[i].name)==0) {
@ -69,6 +70,72 @@ bool change_setting(const char* key, uint16_t new_value) {
}
*(s->value) = 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<all_settings_size; i++) {
Setting s = all_settings[i];
uint16_t value = *(s.value);
uint16_t default_value = setting_default(&s);
if (default_value != value) {
char buf[50];
snprintf(buf, 50, "%s=%d", s.name, value);
LOGln("Saving: %s", buf);
f.println(buf);
}
}
f.close();
return true;
}
bool load_settings() {
if (!SPIFFS.begin()) {
LOGln("Settings * Could not open SPIFFS for loading.");
return false;
}
File f = SPIFFS.open("/pitrix_settings.txt", "r");
if (!f) {
LOGln("Settings * Could not open /pitrix_settings.txt for reading.");
return false;
}
String s = f.readString();
f.close();
while (s.length() > 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;
}