-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDebugUtil.cpp
112 lines (93 loc) · 2.83 KB
/
DebugUtil.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "DebugUtil.h"
#include "wiring_private.h"
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32)
extern "C" char* sbrk(int incr);
extern char *__brkval;
#endif
// DebugUtil unique instance creation
DebugUtil DebugUtil::Debug;
DebugUtil& Debug = DebugUtil::Debug;
/*
* Format Help:
* http://www.cplusplus.com/reference/cstdio/printf/
*
* %i = signed decimal integer
* %u = unsigned decimal integer
* %x = hex
* %X = upper case hex
* %s = string
* %c = character
* 0x%02x = hex representation like 0xff
* %% = % symbol
*/
DebugUtil::DebugUtil() {
}
void DebugUtil::setPrintStream(Stream* printstream) {
_printstream = printstream;
print(F("DEBUG! free ram: %d bytes \n"), freeRam());
}
int DebugUtil::freeRam() {
#if defined(ESP8266) || defined(ESP32)
return ESP.getFreeHeap();
#elif defined(ARDUINO_ARCH_AVR)
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32)
char top;
return &top - reinterpret_cast<char*>(sbrk(0));
#else
return -1;
#endif
}
void DebugUtil::print(const char *format, ...) {
if (_printstream) {
char buf[128]; // limit to 128chars
va_list args;
va_start(args, format);
vsnprintf(buf, 128, format, args);
va_end(args);
_printstream->print(buf);
}
}
void DebugUtil::print(const __FlashStringHelper *format, ...) {
if (_printstream) {
char buf[128]; // limit to 128chars
va_list args;
va_start(args, format);
#if defined(__AVR__) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32)
vsnprintf_P(buf, sizeof (buf), (const char *) format, args); // progmem for AVR and ESP8266
#else
vsnprintf(buf, sizeof (buf), (const char *) format, args); // for rest of the world
#endif
va_end(args);
//Serial.print(buf);)
_printstream->print(buf);
}
}
void DebugUtil::println(const char *format, ...) {
if (_printstream) {
char buf[128]; // limit to 128chars
va_list args;
va_start(args, format);
vsnprintf(buf, 128, format, args);
va_end(args);
//Serial.println(buf);)
_printstream->println(buf);
}
}
void DebugUtil::println(const __FlashStringHelper *format, ...) {
if (_printstream) {
char buf[128]; // limit to 128chars
va_list args;
va_start(args, format);
#if defined(__AVR__) || defined(ESP8266) || defined(ARDUINO_ARCH_STM32)
vsnprintf_P(buf, sizeof (buf), (const char *) format, args); // progmem for AVR and ESP8266
#else
vsnprintf(buf, sizeof (buf), (const char *) format, args); // for rest of the world
#endif
va_end(args);
//Serial.println(buf);)
_printstream->println(buf);
}
}