From b7881355a277d4f8a9f408d3394055b9fe3f9362 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Thu, 17 Jul 2025 20:57:50 +0200 Subject: [PATCH] Config class as singleton with initializer in setup --- src/config.h | 14 ++++++++++++-- src/main.cpp | 16 +++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/config.h b/src/config.h index 51efa7e..3444033 100644 --- a/src/config.h +++ b/src/config.h @@ -41,11 +41,21 @@ public: class Config { + public: + static Config &getInstance() + { + static Config instance; + return instance; + } + +private: + Config() = default; Config(const Config &) = delete; Config &operator=(const Config &) = delete; - Config() +public: + void init() { FSmount mount; // scoped mount of the filesystem @@ -102,7 +112,7 @@ public: LOG_ERROR("Unable to remove config.json"); } LOG_WARN("Configuration reset, Restarting"); - }; //filesystem is unmounted here + }; // filesystem is unmounted here delay(500); esp_restart(); } diff --git a/src/main.cpp b/src/main.cpp index 880b6bb..7c05d02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,10 @@ #include "utils.h" +/////////////// GLOBALS /////////////// +Config& conf = Config::getInstance(); +/////////////// GLOBALS /////////////// + void callback(char *topic, uint8_t *payload, unsigned int length) { std::string pl; @@ -42,13 +46,11 @@ void setup() { Serial.begin(9600); LOG_ATTACH_SERIAL(Serial); + conf.init(); } void loop() { - /////////////// GLOBALS /////////////// - Config conf = Config(); - /////////////// GLOBALS /////////////// const uint8_t baseRegister(0x00); uint16_t k(0); uint8_t sensors(0); @@ -79,11 +81,11 @@ void loop() mqtt.setServer(conf.m_mqttHost.c_str(), conf.m_mqttPort); mqtt.setCallback(callback); //////////////// NETWORK //////////////// - - //////////////// NETWORK //////////////// - /////////////// CALLBACK //////////////// + + //////////////// NETWORK //////////////// + /////////////// CALLBACK //////////////// Network.onEvent( - [&conf, ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void + [ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void { eth.onEvent(event, info); // Arduino Ethernet event handler if (!eth.isConnected())