rgbwifi/src/settings/system.cpp

63 lines
1.5 KiB
C++

/*
* ESP8266 RGBW controller
* Copyright 2020 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/RGBWifi
*/
#include "./system.h"
#include <ArduinoJson.h>
#include <FS.h>
#include "../debug.h"
#include "../global.h"
#include "../config.h"
void SystemSettings::toJson(Print &print)
{
DynamicJsonDocument jsonDocument(1024);
JsonObject pins = jsonDocument.createNestedObject("pins");
pins["ledAP"] = pinLEDAP();
pins["ledSTA"] = pinLEDSTA();
pins["apButton"] = pinAPButton();
jsonDocument["ledCount"] = ledCount();
serializeJson(jsonDocument, print);
}
bool SystemSettings::fromJson(JsonVariant &jsonDocument, bool* changed)
{
if (changed != nullptr)
*changed = false;
JsonObject pins = jsonDocument["pins"];
uint8_t jsonPinLEDAP = pins["ledAP"];
uint8_t jsonPinLEDSTA = pins["ledSTA"];
uint8_t jsonPinAPButton = pins["apButton"];
uint16_t jsonLEDCount = jsonDocument["ledCount"];
if (jsonPinLEDAP == 0) jsonPinLEDAP = pinLEDAP();
if (jsonPinLEDSTA == 0) jsonPinLEDSTA = pinLEDSTA();
if (jsonPinAPButton == 0) jsonPinAPButton = pinAPButton();
if (jsonLEDCount == 0) jsonLEDCount = ledCount();
if ((jsonPinLEDAP != pinLEDAP()) ||
(jsonPinLEDSTA != pinLEDSTA()) ||
(jsonPinAPButton != pinAPButton()) ||
(jsonLEDCount != ledCount()))
{
pinLEDAP(jsonPinLEDAP);
pinLEDSTA(jsonPinLEDSTA);
pinAPButton(jsonPinAPButton);
ledCount(jsonLEDCount);
if (changed != nullptr)
*changed = true;
}
return true;
}