SerialDebug_options.ino
3.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
////////
// Libraries Arduino
//
// Library: SerialDebug - Improved serial debugging to Arduino, with simple software debugger
// GitHub: https://github.com/JoaoLopesF/SerialDebug
// Author: Joao Lopes
//
// Example to show how to enabling or disabling features
//
///////
////// Includes
#include "Arduino.h"
// SerialDebug Library
// SerialDebug disables examples
// Please edit SerialDebug.h, if local defines not working (for Arduino IDE it not works)
// Note: for the Arduino recompile SerialDebugLibrary, please open and save SerialDebug.h
// Please try all and see memory afects
// Disable all debug ? Good to release builds (production)
// as nothing of SerialDebug is compiled, zero overhead :-)
// For it just uncomment the DEBUG_DISABLED
#define DEBUG_DISABLED true
// Minimum mode - only to show messages of debug
// No commands from serial, no debugger, no printf
// Minumum usage of memory RAM and program
// Uncomment this, to use this mode
//#define DEBUG_MINIMUM true
// Disable SerialDebug debugger ? No more commands and features as functions and globals
// Uncomment this to disable it
//#define DEBUG_DISABLE_DEBUGGER true
// Disable Flash variables support - F()
// Used internally in SerialDebug and in public API
// If is a low memory board, like AVR, all strings in SerialDebug is using flash memory
// If have RAM memory, this is more fast than flash
//#define DEBUG_NOT_USE_FLASH_F true
// For Espressif boards, not flash support, due it have a lot of memory
// If you need more memory, can force it:
//#define DEBUG_USE_FLASH_F true
// Disable auto function name (good if your debug yet contains it)
//#define DEBUG_AUTO_FUNC_DISABLED true
// Include SerialDebug
#include "SerialDebug.h" //https://github.com/JoaoLopesF/SerialDebug
////// Variables
////// Setup
void setup() {
// Initialize the Serial
Serial.begin(115200);
#ifdef __AVR_ATmega32U4__ // Arduino AVR Leonardo
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
#else
delay(500); // Wait a time
#endif
}
////// Loop
void loop()
{
// SerialDebug handle
// Notes: if in inactive mode (until receive anything from serial),
// it show only messages of always or errors level type
// And the overhead during inactive mode is very low
// Only if not DEBUG_DISABLED
debugHandle();
// Example to show that nothing is compiled if Serial.Debug is disabled
// Good for production releases
#ifdef DEBUG_DISABLED
debugV("this is not compiled. if compiled, cause a error, due variable not exist %d", not_existing_var);
// Show memory (not using SerialDebug)
Serial.print("* (by Serial.print) Free Heap RAM: ");
Serial.println(freeMemory());
#endif
// Show the memory - using SerialDebug
// Note if set DEBUG_DISABLED, this not compile and not generate any output
debugA("* (by SerialDebug) Free Heap RAM: %d", freeMemory());
// Delay of 5 second
delay(5000);
}
// Free memory
#if defined ARDUINO_ARCH_AVR || defined __arm__
// Based in https://forum.pjrc.com/threads/23256-Get-Free-Memory-for-Teensy-3-0
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else
extern char *__brkval;
extern char __bss_end;
#endif
#endif
int freeMemory() {
#if defined ESP8266 || defined ESP32
return ESP.getFreeHeap();
#elif defined ARDUINO_ARCH_AVR || defined __arm__
// function from the sdFat library (SdFatUtil.cpp)
// licensed under GPL v3
// Full credit goes to William Greiman.
char top;
#ifdef __arm__
return &top - reinterpret_cast<char*>(sbrk(0));
#else
return __brkval ? &top - __brkval : &top - &__bss_end;
#endif
#else // Not known
return -1;
#endif
}
/////////// End