diff --git a/wled00/bus_manager.h b/wled00/bus_manager.h index bfae90ac..5f604321 100644 --- a/wled00/bus_manager.h +++ b/wled00/bus_manager.h @@ -15,13 +15,19 @@ uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb); void colorRGBtoRGBW(byte* rgb); // enable additional debug output +#if defined(WLED_DEBUG_HOST) + #define DEBUGOUT NetDebug +#else + #define DEBUGOUT Serial +#endif + #ifdef WLED_DEBUG #ifndef ESP8266 #include #endif - #define DEBUG_PRINT(x) Serial.print(x) - #define DEBUG_PRINTLN(x) Serial.println(x) - #define DEBUG_PRINTF(x...) Serial.printf(x) + #define DEBUG_PRINT(x) DEBUGOUT.print(x) + #define DEBUG_PRINTLN(x) DEBUGOUT.println(x) + #define DEBUG_PRINTF(x...) DEBUGOUT.printf(x) #else #define DEBUG_PRINT(x) #define DEBUG_PRINTLN(x) diff --git a/wled00/net_debug.cpp b/wled00/net_debug.cpp new file mode 100644 index 00000000..9f451ca5 --- /dev/null +++ b/wled00/net_debug.cpp @@ -0,0 +1,75 @@ +#include "wled.h" + +#ifdef WLED_DEBUG_HOST + +NetworkDebugPrinter NetDebug; + +void NetworkDebugPrinter::print(const char *s, bool newline) { + if (!WLED_CONNECTED || !udpConnected || s == nullptr) return; + + if (!debugPrintHostIP && !debugPrintHostIP.fromString(netDebugPrintHost)) { + #ifdef ESP8266 + WiFi.hostByName(netDebugPrintHost, debugPrintHostIP, 750); + #else + #ifdef WLED_USE_ETHERNET + ETH.hostByName(netDebugPrintHost, debugPrintHostIP); + #else + WiFi.hostByName(netDebugPrintHost, debugPrintHostIP); + #endif + #endif + } + + WiFiUDP debugUdp; + debugUdp.beginPacket(debugPrintHostIP, netDebugPrintPort); + debugUdp.write(reinterpret_cast(s), strlen(s)); + if (newline) debugUdp.write('\n'); + debugUdp.endPacket(); +} + +void NetworkDebugPrinter::print(const __FlashStringHelper* s, bool newline) { + char buf[512]; + strncpy_P(buf, (PGM_P)s, 512); + print(buf, newline); +} + +void NetworkDebugPrinter::print(String s) { + print(s.c_str()); +} + +void NetworkDebugPrinter::print(unsigned int n, bool newline) { + char s[10]; + snprintf_P(s, sizeof(s), PSTR("%d"), n); + s[9] = '\0'; + print(s, newline); +} + +void NetworkDebugPrinter::println() { + print("", true); +} + +void NetworkDebugPrinter::println(const char *s) { + print(s, true); +} + +void NetworkDebugPrinter::println(const __FlashStringHelper* s) { + print(s, true); +} + +void NetworkDebugPrinter::println(String s) { + print(s.c_str(), true); +} + +void NetworkDebugPrinter::println(unsigned int n) { + print(n, true); +} + +void NetworkDebugPrinter::printf(const char *fmt...) { + va_list args; + va_start(args, fmt); + char s[1024]; + vsnprintf(s, sizeof(s), fmt, args); + va_end(args); + print(s); +} + +#endif diff --git a/wled00/net_debug.h b/wled00/net_debug.h new file mode 100644 index 00000000..1d9aa6c2 --- /dev/null +++ b/wled00/net_debug.h @@ -0,0 +1,25 @@ +#ifndef WLED_NET_DEBUG_H +#define WLED_NET_DEBUG_H + +#include +#include + +class NetworkDebugPrinter { +private: + IPAddress debugPrintHostIP; +public: + void print(const char *s, bool newline = false); + void print(const __FlashStringHelper* s, bool newline = false); + void print(String s); + void print(unsigned int n, bool newline = false); + void println(); + void println(const char *s); + void println(const __FlashStringHelper* s); + void println(String s); + void println(unsigned int n); + void printf(const char *fmt, ...); +}; + +extern NetworkDebugPrinter NetDebug; + +#endif \ No newline at end of file diff --git a/wled00/wled.h b/wled00/wled.h index 44327176..7f7928ca 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -95,6 +95,10 @@ #include "my_config.h" #endif +#ifdef WLED_DEBUG_HOST +#include "net_debug.h" +#endif + #include #ifdef WLED_ADD_EEPROM_SUPPORT #include @@ -669,13 +673,27 @@ WLED_GLOBAL StaticJsonDocument doc; WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); // enable additional debug output +#if defined(WLED_DEBUG_HOST) + // On the host side, use netcat to receive the log statements: nc -l 7868 -u + // use -D WLED_DEBUG_HOST='"192.168.xxx.xxx"' or FQDN within quotes + #define DEBUGOUT NetDebug + WLED_GLOBAL char netDebugPrintHost[33] _INIT(WLED_DEBUG_HOST); + #if defined(WLED_DEBUG_NET_PORT) + WLED_GLOBAL int netDebugPrintPort _INIT(WLED_DEBUG_PORT); + #else + WLED_GLOBAL int netDebugPrintPort _INIT(7868); + #endif +#else + #define DEBUGOUT Serial +#endif + #ifdef WLED_DEBUG #ifndef ESP8266 #include #endif - #define DEBUG_PRINT(x) Serial.print(x) - #define DEBUG_PRINTLN(x) Serial.println(x) - #define DEBUG_PRINTF(x...) Serial.printf(x) + #define DEBUG_PRINT(x) DEBUGOUT.print(x) + #define DEBUG_PRINTLN(x) DEBUGOUT.println(x) + #define DEBUG_PRINTF(x...) DEBUGOUT.printf(x) #else #define DEBUG_PRINT(x) #define DEBUG_PRINTLN(x) @@ -683,9 +701,9 @@ WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0); #endif #ifdef WLED_DEBUG_FS - #define DEBUGFS_PRINT(x) Serial.print(x) - #define DEBUGFS_PRINTLN(x) Serial.println(x) - #define DEBUGFS_PRINTF(x...) Serial.printf(x) + #define DEBUGFS_PRINT(x) DEBUGOUT.print(x) + #define DEBUGFS_PRINTLN(x) DEBUGOUT.println(x) + #define DEBUGFS_PRINTF(x...) DEBUGOUT.printf(x) #else #define DEBUGFS_PRINT(x) #define DEBUGFS_PRINTLN(x) diff --git a/wled00/wled00.vcxproj b/wled00/wled00.vcxproj index 9e30b6f1..638e69b8 100644 --- a/wled00/wled00.vcxproj +++ b/wled00/wled00.vcxproj @@ -227,6 +227,7 @@ + VisualMicroDebugger