diff --git a/include/debugutil.hpp b/include/debugutil.hpp new file mode 100644 index 0000000..007866f --- /dev/null +++ b/include/debugutil.hpp @@ -0,0 +1,30 @@ +#pragma once +#include + +#ifndef ENABLE_SERIAL_DEBUG + #define ENABLE_SERIAL_DEBUG 0 +#endif + +struct Debug { + static constexpr bool enabled = static_cast(ENABLE_SERIAL_DEBUG); + static inline void begin(unsigned long baud) { + if constexpr (enabled) Serial.begin(baud); + } + template + static inline void print(Args&&... args) { + if constexpr (enabled) { (Serial.print(std::forward(args)), ...); } + } + template + static inline void println(Args&&... args) { + if constexpr (enabled) { + (Serial.print(std::forward(args)), ...); + Serial.println(); + } + } + template + static inline void printf(const char *fmt, Args&&... args) { + if constexpr (enabled) { + Serial.printf(fmt, std::forward(args)...); + } + } +}; \ No newline at end of file diff --git a/lib/light/light.cpp b/lib/light/light.cpp index 16020f9..10ae1ff 100644 --- a/lib/light/light.cpp +++ b/lib/light/light.cpp @@ -1,4 +1,5 @@ #include +#include "debugutil.hpp" #include "light.h" #include "mqtt.h" @@ -166,11 +167,11 @@ void Light::subscribeToMqttTopics() { } void Light::handleCommand(const std::string& command) { - Serial.println("Received command: " + String(command.c_str())); + Debug::println("Received command: " + String(command.c_str())); JsonDocument commandJson; deserializeJson(commandJson, command); if (commandJson.isNull()) { - Serial.println("Invalid command JSON"); + Debug::println("Invalid command JSON"); return; } if (commandJson["state"].is()) { @@ -240,11 +241,11 @@ void Light::publishCurrentState() { std::string stateJson; serializeJson(stateInfo, stateJson); - Serial.println("Publishing current state: " + String(stateJson.c_str())); + Debug::println("Publishing current state: " + String(stateJson.c_str())); Mqtt::publish(lightInfo.stateTopic, stateJson); std::string attributeJson; serializeJson(attributeInfo, attributeJson); - Serial.println("Publishing current attributes: " + String(attributeJson.c_str())); + Debug::println("Publishing current attributes: " + String(attributeJson.c_str())); Mqtt::publish(lightInfo.jsonAttributesTopic, attributeJson); } diff --git a/lib/ota/ota.cpp b/lib/ota/ota.cpp index 4f73ac3..c8c98b4 100644 --- a/lib/ota/ota.cpp +++ b/lib/ota/ota.cpp @@ -1,29 +1,30 @@ #include +#include "debugutil.hpp" #include "ota.h" OTAHandler::OTAHandler(std::string_view hostname) { ArduinoOTA.setHostname(hostname.data()); ArduinoOTA.onStart([]() { - Serial.println("OTA Start"); + Debug::println("OTA Start"); }); ArduinoOTA.onEnd([]() { - Serial.println("OTA End"); + Debug::println("OTA End"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("OTA Progress: %u%%\n", (progress / (total / 100))); + Debug::printf("OTA Progress: %u%%\n", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("OTA Error[%u]: ", error); + Debug::printf("OTA Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { - Serial.println("Auth Failed"); + Debug::println("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { - Serial.println("Begin Failed"); + Debug::println("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { - Serial.println("Connect Failed"); + Debug::println("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { - Serial.println("Receive Failed"); + Debug::println("Receive Failed"); } else if (error == OTA_END_ERROR) { - Serial.println("End Failed"); + Debug::println("End Failed"); } }); ArduinoOTA.begin(); diff --git a/platformio.ini b/platformio.ini index 5149aaa..804ea98 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,8 +15,8 @@ framework = arduino monitor_speed = 115200 build_unflags = -std=gnu++11 build_flags = - -std=c++17 -std=gnu++17 + -I include lib_deps = martinverges/ESP32 Wifi Manager@^1.5.0 esp32async/ESPAsyncWebServer@^3.7.10 @@ -25,6 +25,9 @@ lib_deps = arkhipenko/TaskScheduler@^3.8.5 [env:esp32dev-serial] +build_flags = + ${env.build_flags} + -D ENABLE_SERIAL_DEBUG=1 [env:esp32dev-ota] upload_protocol = espota diff --git a/src/main.cpp b/src/main.cpp index 82c4ae6..becbf36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include "appcontext.hpp" #include "config.h" +#include "debugutil.hpp" #include "light.h" #include "mqtt.h" #include "network.h" @@ -41,9 +42,8 @@ Scheduler *scheduler = nullptr; void initializeScheduler(); void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - Serial.println("Starting Smart RGB ESP32..."); + Debug::begin(115200); + Debug::println("Starting Smart RGB ESP32..."); stateMachine = new StateMachine(); initializeScheduler(); appContext->pinR = pinR; diff --git a/src/states.hpp b/src/states.hpp index 0230a04..32397a1 100644 --- a/src/states.hpp +++ b/src/states.hpp @@ -1,6 +1,7 @@ #include #include "appcontext.hpp" #include "config.h" +#include "debugutil.hpp" #include "mqtt.h" #include "pin.h" #include "statemachine.hpp" @@ -51,7 +52,7 @@ public: NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {} void onEnter(StateMachineBase &stateMachine) override { - Serial.println("Entering NetworkInitializeState"); + Debug::println("Entering NetworkInitializeState"); if (appContext && !appContext->network) { appContext->network = new Network(hostName, friendlyName); } else if (appContext && appContext->network) { @@ -91,7 +92,7 @@ public: } void onExit(StateMachineBase &stateMachine) override { - Serial.println("Exiting RunningState"); + Debug::println("Exiting RunningState"); } void onUpdate(StateMachineBase &stateMachine) override {