From ec67767ca68f894bab4dee9f6b29c2b669bfb773 Mon Sep 17 00:00:00 2001 From: andreas Date: Sat, 16 Oct 2021 20:18:03 +0200 Subject: [PATCH] intermediate: config handler implementation --- lib/README | 46 +++++++++++++++++++++ lib/config/GWConfig.cpp | 88 +++++++++++++++++++++++++++++++++++++++++ lib/config/GWConfig.h | 75 +++++++++++++++++++++++++++++++++++ src/main.cpp | 16 +++++++- 4 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 lib/README create mode 100644 lib/config/GWConfig.cpp create mode 100644 lib/config/GWConfig.h diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/lib/config/GWConfig.cpp b/lib/config/GWConfig.cpp new file mode 100644 index 0000000..53b93fb --- /dev/null +++ b/lib/config/GWConfig.cpp @@ -0,0 +1,88 @@ +#include "GWConfig.h" +#include + +#define B(v) (v?"true":"false") + +bool isTrue(const char * value){ + return (strcasecmp(value,"true") == 0); +} + +class DummyConfig : public ConfigItem{ + public: + DummyConfig():ConfigItem("dummy",""){} + virtual void fromString(const String v){ + }; + virtual void reset(){ + } +}; + +DummyConfig dummyConfig; +String ConfigHandler::toString() const{ + String rt; + rt+="Config: "; + for (int i=0;igetName(); + rt+="="; + rt+=configs[i]->asString(); + rt+=", "; + } + return rt; + } + +String ConfigHandler::toJson() const{ + String rt; + DynamicJsonDocument jdoc(50); + for (int i=0;igetName()]=configs[i]->asString(); + } + serializeJson(jdoc,rt); + return rt; +} +ConfigItem * ConfigHandler::findConfig(const String name, bool dummy){ + for (int i=0;igetName() == name) return configs[i]; + } + if (!dummy) return NULL; + return &dummyConfig; +} + +#define PREF_NAME "gwprefs" +ConfigHandler::ConfigHandler(){ + +} +bool ConfigHandler::loadConfig(){ + prefs.begin(PREF_NAME,true); + for (int i=0;igetName().c_str(),configs[i]->getDefault()); + } + prefs.end(); + return true; +} +bool ConfigHandler::saveConfig(){ + prefs.begin(PREF_NAME,false); + for (int i=0;igetName().c_str(),configs[i]->asString()); + } + prefs.end(); + return true; +} +bool ConfigHandler::updateValue(const char *name, const char * value){ + return false; +} +bool ConfigHandler::reset(bool save){ + for (int i=0;ireset(); + } + if (!save) return true; + return saveConfig(); +} +String ConfigHandler::getString(const String name){ + ConfigItem *i=findConfig(name); + if (!i) return String(); + return i->asString(); +} +bool ConfigHandler::getBool(const String name){ + ConfigItem *i=findConfig(name); + if (!i) return false; + return i->asBoolean(); +} \ No newline at end of file diff --git a/lib/config/GWConfig.h b/lib/config/GWConfig.h new file mode 100644 index 0000000..94b346f --- /dev/null +++ b/lib/config/GWConfig.h @@ -0,0 +1,75 @@ +#ifndef _GWCONFIG_H +#define _GWCONFIG_H +#include +#include + +class ConfigItem{ + private: + String name; + String initialValue; + String value; + public: + ConfigItem(const String &name, const String initialValue){ + this->name=name; + this->initialValue=initialValue; + this->value=initialValue; + } + ConfigItem(const String &name, bool initalValue): + ConfigItem(name,initalValue?String("true"):String("false")){}; + String asString() const{ + return value; + } + virtual void fromString(const String v){ + value=v; + }; + bool asBoolean() const{ + return strcasecmp(value.c_str(),"true") == 0; + } + String getName() const{ + return name; + } + virtual void reset(){ + value=initialValue; + } + bool changed() const{ + return value != initialValue; + } + String getDefault() const { + return initialValue; + } +}; + + +class ConfigHandler{ + private: + Preferences prefs; + public: + public: + const String sendUsb="sendUsb"; + const String receiveUsb="receiveUsb"; + const String wifiClient="wifiClient"; + const String wifiPass="wifiPass"; + const String wifiSSID="wifiSSID"; + ConfigHandler(); + bool loadConfig(); + bool saveConfig(); + bool updateValue(const char *name, const char * value); + bool reset(bool save); + String toString() const; + String toJson() const; + String getString(const String name); + bool getBool(const String name); + ConfigItem * findConfig(const String name, bool dummy=false); + private: + ConfigItem* configs[5]={ + new ConfigItem(sendUsb,true), + new ConfigItem (receiveUsb,false), + new ConfigItem (wifiClient,false), + new ConfigItem (wifiSSID,""), + new ConfigItem (wifiPass,"") + }; + int getNumConfig() const{ + return sizeof(configs)/sizeof(ConfigItem*); + } +}; +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7f82788..0191443 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,6 +37,8 @@ #include "List.h" #include "BoatData.h" +#include "GWConfig.h" + #define ENABLE_DEBUG_LOG 0 // Debug log, set to 1 to enable AIS forward on USB-Serial / 2 for ADC voltage to support calibration @@ -49,6 +51,7 @@ #define WLAN_CLIENT 0 // Set to 1 to enable client network. 0 to act as AP only +ConfigHandler config; // Wifi cofiguration Client and Access Point const char *AP_ssid = "MyESP32"; // ESP32 as AP @@ -177,12 +180,20 @@ void js_status(){ webserver.send(200,F("application/json"),buf); } +void js_config(){ + webserver.send(200,F("application/json"),config.toJson()); +} + +void web_config(){ +} + void handleNotFound() { webserver.send(404, "text/plain", "File Not Found\n\n"); } +ConfigItem *sendUsb=NULL; void setup() { @@ -195,6 +206,9 @@ void setup() { // Init USB serial port Serial.begin(115200); Serial.println("Starting..."); + config.loadConfig(); + Serial.println(config.toString()); + sendUsb=config.findConfig(config.sendUsb,true); // Init AIS serial port 2 //Serial2.begin(baudrate, rs_config); //NMEA0183.Begin(&Serial2, 3, baudrate); @@ -330,7 +344,7 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg) { char buf[MAX_NMEA0183_MESSAGE_SIZE]; if ( !NMEA0183Msg.GetMessage(buf, MAX_NMEA0183_MESSAGE_SIZE) ) return; SendBufToClients(buf); - if (NMEA_TO_SERIAL){ + if (sendUsb->asBoolean()){ Serial.println(buf); } }