-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.h
64 lines (51 loc) · 1.96 KB
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef PM25_NODEMCU_DEBUG_H
#define PM25_NODEMCU_DEBUG_H
#define DEBUG_SERIAL(m) printDebugMessage(m)
// const char* DEBUG_IP_TARGET = "239.0.0.1";
// const int DEBUG_IP_PORT = 8899;
// #define DEBUG_SERIAL(m)
const char* DEBUG_IP_TARGET = "";
const int DEBUG_IP_PORT = 0;
void printDebugMessage(String msg) {
Serial.println(msg);
}
// Helpers
#define serialUdpDebug(s) DEBUG_SERIAL(s); dbgMsg(s);
#define serialUdpIntDebug(s) DEBUG_SERIAL(s); intDbgMsg(s);
#define dbgMsg(s) sendDebugMessage(s, true);
#define intDbgMsg(s) sendDebugMessage(s, false);
double getCurrentExecutionTime() {
return (((double)millis() - (double)startupMillis) / 1000.0);
}
#define DEBUG_ENABLED (strlen(DEBUG_IP_TARGET) > 0 && DEBUG_IP_PORT > 0)
void sendDebugMessage(String msg, bool withDelay = true) {
if (!DEBUG_ENABLED) {
// DEBUG_SERIAL("UDP Debug not enabled");
return;
}
// If we have some debug target, let us open plain UDP and spit out
// debugging should be done client side with:
// socat STDIO UDP4-RECV:8899,ip-add-memberhsip=239.0.0.1:eth0 | ts "%Y-%m-%d %H:%M:%S"
String _msg = WiFi.hostname() + ": [" + String(getCurrentExecutionTime(), 5) + "] " + msg;
WiFiUDP udp;
IPAddress remote;
if (WiFi.hostByName(DEBUG_IP_TARGET, remote)) {
udp.beginPacketMulticast(remote, DEBUG_IP_PORT, WiFi.localIP(), 3);
if (withDelay) delay(1);
udp.write(_msg.c_str());
if (withDelay) delay(1);
udp.write('\n');
if (withDelay) delay(1);
udp.endPacket();
if (withDelay) delay(1);
}
// delay(1) would help sending UDP packets as documented on:
// https://github.com/esp8266/Arduino/issues/1009#issuecomment-189666095
//
// But calling delay()/yield() from interrupt context is not allowed.
// yield() will panic() on purpose, delay() just crash.
// Confere calls to cont_can_yield(&g_cont) in cores/esp8266/core_esp8266_main.cpp
// and implementation in cores/esp8266/cont_util.c
return;
}
#endif // PM25_NODEMCU_DEBUG_H