Skip to content

Commit 1d8c9ac

Browse files
committed
Net debug optimizations
Fix ESP8266 (unaligned progmem flash string reads) Do not send an extra package for \n in println Only resolve IP/hostname once
1 parent 7fcc8be commit 1d8c9ac

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

wled00/net_debug.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
NetworkDebugPrinter NetDebug;
66

7-
void NetworkDebugPrinter::printchar(char s) {
8-
debugUdp.write(s);
9-
}
7+
void NetworkDebugPrinter::print(const char *s, bool newline) {
8+
if (!WLED_CONNECTED || !udpConnected || s == nullptr) return;
109

11-
void NetworkDebugPrinter::print(const char *s) {
12-
if (!WLED_CONNECTED || s == nullptr) {
13-
return;
14-
}
15-
IPAddress debugPrintHostIP;
16-
if (!debugPrintHostIP.fromString(netDebugPrintHost)) {
10+
if (!debugPrintHostIP && !debugPrintHostIP.fromString(netDebugPrintHost)) {
1711
#ifdef ESP8266
1812
WiFi.hostByName(netDebugPrintHost, debugPrintHostIP, 750);
1913
#else
@@ -24,49 +18,49 @@ void NetworkDebugPrinter::print(const char *s) {
2418
#endif
2519
#endif
2620
}
21+
22+
WiFiUDP debugUdp;
2723
debugUdp.beginPacket(debugPrintHostIP, netDebugPrintPort);
28-
//for (size_t i=0; i<strlen(s); i++) printchar(s[i]);
2924
debugUdp.write(reinterpret_cast<const uint8_t *>(s), strlen(s));
25+
if (newline) debugUdp.write('\n');
3026
debugUdp.endPacket();
3127
}
3228

33-
void NetworkDebugPrinter::print(const __FlashStringHelper* s) {
34-
print(reinterpret_cast<const char *>(s));
29+
void NetworkDebugPrinter::print(const __FlashStringHelper* s, bool newline) {
30+
char buf[512];
31+
strncpy_P(buf, (PGM_P)s, 512);
32+
print(buf, newline);
3533
}
3634

3735
void NetworkDebugPrinter::print(String s) {
3836
print(s.c_str());
3937
}
4038

41-
void NetworkDebugPrinter::print(unsigned int n) {
39+
void NetworkDebugPrinter::print(unsigned int n, bool newline) {
4240
char s[10];
4341
snprintf_P(s, sizeof(s), PSTR("%d"), n);
4442
s[9] = '\0';
45-
print(s);
43+
print(s, newline);
4644
}
4745

4846
void NetworkDebugPrinter::println() {
49-
print("\n");
47+
print("", true);
5048
}
5149

5250
void NetworkDebugPrinter::println(const char *s) {
53-
print(s);
54-
print("\n");
51+
print(s, true);
5552
}
5653

5754
void NetworkDebugPrinter::println(const __FlashStringHelper* s) {
58-
print(s);
59-
print("\n");
55+
print(s, true);
6056
}
6157

6258
void NetworkDebugPrinter::println(String s) {
63-
print(s);
64-
print("\n");
59+
print(s.c_str(), true);
6560
}
6661

6762
void NetworkDebugPrinter::println(unsigned int n) {
68-
print(n);
69-
print("\n");
63+
print(n, true);
7064
}
7165

7266
void NetworkDebugPrinter::printf(const char *fmt...) {

wled00/net_debug.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
class NetworkDebugPrinter {
88
private:
9-
WiFiUDP debugUdp;
10-
void printchar(char c);
9+
IPAddress debugPrintHostIP;
1110
public:
12-
void print(const char *s);
13-
void print(const __FlashStringHelper* s);
11+
void print(const char *s, bool newline = false);
12+
void print(const __FlashStringHelper* s, bool newline = false);
1413
void print(String s);
15-
void print(unsigned int n);
14+
void print(unsigned int n, bool newline = false);
1615
void println();
1716
void println(const char *s);
1817
void println(const __FlashStringHelper* s);

0 commit comments

Comments
 (0)