Skip to content
Snippets Groups Projects
Commit 077ad517 authored by Jan Grewe's avatar Jan Grewe
Browse files

save/load via webinterface

parent b7f6ab89
Branches
No related tags found
No related merge requests found
......@@ -34,14 +34,20 @@
<input type="range" class="form-range rangeConfig" id="rangeSpeed" data-name="interval" min="1" max="100"
steps="10" disabled>
</div>
<div class="row mb-3 px-3 mt-5">
<button type="button" class="btn btn-primary btnAction" data-action="addGlider" disabled>Add
Glider</button>
</div>
</div>
<div class="card-footer text-body-secondary py-3">
<div class="row">
<div class="col-6">
<button type="button" class="btn btn-sm btn-primary btnAction" data-action="addGlider" disabled>Add
Glider</button>
<button type="button" class="btn btn-sm btn-success btnAction" data-action="load" disabled>Load</button>
</div>
<div class="col-6">
<button type="button" class="btn btn-sm btn-success btnAction" data-action="save" disabled>Save</button>
</div>
<div class="col-6 text-end">
<button type="button" class="btn btn-sm btn-outline-secondary me-1" data-bs-toggle="collapse"
......
var wsUrl = 'ws://' + document.location.host + '/ws'
var wsUrl = 'ws://' + document.location.host + '/ws';
var config = {
brightness: 0,
......@@ -81,6 +81,8 @@ function startSocket() {
onOpen: function (e) {
console.log("WS: connected");
addMessage("✅ connected");
getConfig();
uiEnabled(true);
},
onClose: function (e) {
uiEnabled(false);
......@@ -95,16 +97,23 @@ function startSocket() {
});
ws.listen(function (data) {
console.log(data);
var json = JSON.stringify(data);
var msg = "⬅️ " + json;
addMessage(msg);
if (data.config) {
loadConfig(data.config);
uiEnabled(true);
}
});
}
function getConfig() {
var payload = {
'action': 'load'
};
wsSend(payload);
}
function loadConfig(data) {
config.brightness = data.brightness;
config.interval = data.interval;
......
#include <Arduino.h>
#include <Preferences.h>
#include "utils.h"
#include "network.h"
#include "display.h"
#include "gameoflife.h"
Preferences preferences;
#include "settings.h"
int defaultBrightness;
int gameInterval;
......@@ -19,18 +17,12 @@ void showTitle() {
showMessage(msg);
}
void setupSettings() {
preferences.begin("settings", true); // true = readonly
defaultBrightness = preferences.getUInt("brightness", 50);
gameInterval = preferences.getUInt("interval", 100);
preferences.end();
}
void setup()
{
Serial.begin(115200);
logLine("", true);
setupSettings();
loadSettings();
setupDisplay();
showTitle();
setupNetwork();
......
......@@ -5,6 +5,7 @@
#include "network.h"
#include "display.h"
#include "gameoflife.h"
#include "settings.h"
MDNSResponder mdns;
AsyncWebServer server(80);
......@@ -57,15 +58,14 @@ void setupOTA()
{
runGame = false;
ws.enable(false);
ws.textAll("OTA Update Started");
ws.closeAll();
logLine("Update Start");
logLine("OTA Update Start");
clearDisplay(); });
ArduinoOTA.onEnd([]()
{
otaProgress = 0;
runGame = true;
logLine("Update End");
logLine("OTA Update End");
clearDisplay(); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
......@@ -198,13 +198,6 @@ void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType
logLine("WS<< connect");
logLine("WS>> ping");
client->ping();
DynamicJsonDocument doc = getConfigJson();
size_t strsize = measureJson(doc) + 1;
char json[strsize];
serializeJson(doc, json, strsize);
logLine("WS>> ", false);
logLine(json);
ws.text(client->id(), json);
}
else if (type == WS_EVT_DISCONNECT)
{
......@@ -250,7 +243,16 @@ void handleJson(uint8_t *data)
if (doc.containsKey("action"))
{
if (doc["action"] == "addGlider")
if (doc["action"] == "load")
{
loadSettings();
sendConfig();
}
else if (doc["action"] == "save")
{
saveSettings();
}
else if (doc["action"] == "addGlider")
{
addGlider();
}
......@@ -266,8 +268,13 @@ void updateConfig(StaticJsonDocument<200U> doc)
gameInterval = doc["config"]["interval"];
defaultBrightness = doc["config"]["brightness"];
displayBrightness(defaultBrightness);
preferences.begin("settings", false);
preferences.putUInt("brightness", defaultBrightness);
preferences.putUInt("interval", gameInterval);
preferences.end();
}
void sendConfig()
{
DynamicJsonDocument doc = getConfigJson();
size_t strsize = measureJson(doc) + 1;
char json[strsize];
serializeJson(doc, json, strsize);
ws.textAll(json);
}
......@@ -32,5 +32,6 @@ void onTelnetInput(String str);
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
void handleJson(uint8_t* data);
void updateConfig(StaticJsonDocument<200U> doc);
void sendConfig();
#endif
#include <Arduino.h>
#include "settings.h"
#include "display.h"
Preferences preferences;
void loadSettings() {
preferences.begin("settings", true); // true = readonly
defaultBrightness = preferences.getUInt("brightness", 50);
gameInterval = preferences.getUInt("interval", 100);
preferences.end();
displayBrightness(defaultBrightness);
}
void saveSettings() {
preferences.begin("settings", false);
preferences.putUInt("brightness", defaultBrightness);
preferences.putUInt("interval", gameInterval);
preferences.end();
}
#ifndef SETTINGS_H
#define SETTINGS_H
#include <Arduino.h>
#include <Preferences.h>
extern int defaultBrightness;
extern int gameInterval;
void loadSettings();
void saveSettings();
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment