Arduino-Debug
On-target sketch debugger for Arduino
DebugDemo.ino
Go to the documentation of this file.
1 
43 // Define to remove debug statements
44 // #define NDEBUG
45 
46 #include <Debug.h>
47 
48 // Define to allow debug shell on Serial1 (Arduino Mega2560)
49 // #define USE_SERIAL1
50 
51 namespace A {
52 
53  // Single breakpoint
54  void a()
55  {
56  BREAKPOINT();
57  }
58 
59  // Multiple breakpoints with call between
60  void b()
61  {
62  BREAKPOINT();
63  a();
64  BREAKPOINT();
65  }
66 
67  // Register variable, conditional observation and breakpoint
68  void c()
69  {
70  static int i = 0;
71  REGISTER(i);
72  OBSERVE_IF(i < 1, i);
73  OBSERVE_IF(i > 5, i);
74  BREAK_IF(i == 10);
75  i += 1;
76  ASSERT(i < 15);
77  }
78 
79  long e(int i)
80  {
81  REGISTER(i);
82  CHECK_STACK();
83  OBSERVE(i);
84  if (i > 0) return (e(i - 1) * i);
85  return (1);
86  }
87 };
88 
89 const size_t BUF_MAX = 128;
90 char* buf = NULL;
91 
92 void setup()
93 {
94  // Set the debug stream
95  Serial.begin(57600);
96  while (!Serial);
97  Serial.println(F("DebugDemo::started"));
98 
99 #if defined(ARDUINO_AVR_MEGA2560) && defined(USE_SERIAL1)
100  Serial1.begin(57600);
101  DEBUG_STREAM(Serial1);
102 #else
103  DEBUG_STREAM(Serial);
104 #endif
105 
106  // Register global data (for this scope)
107  Serial.println(F("setup running"));
108  REGISTER(buf);
109 
110  // Contains a breakpoint. Check the memory. No heap used
111  A::a();
112 
113  // Allocate from heap
114  buf = (char*) malloc(BUF_MAX);
115  memset(buf, 0xa5, BUF_MAX);
116 
117  // Contains several breakpoints. Check the memory again. Heap is now used
118  A::b();
119 
120  // Free allocated buffer. Check memory and heap again
121  free(buf);
122  A::a();
123 }
124 
125 void loop()
126 {
127  // Local variable
128  static uint8_t i = 0;
129 
130  // Register all the variables the debug handler should know about
131  REGISTER(buf);
132  REGISTER(BUF_MAX);
133  REGISTER(Serial);
134  REGISTER(i);
135 
136  Serial.println(F("loop running"));
137 
138  // Contains both breakpoints and observations. Check variables
139  A::c();
140 
141  // Leak memory and run function with memory check
142  buf = (char*) malloc(BUF_MAX);
143  ASSERT(buf != NULL);
144  memset(buf, 0xa5, BUF_MAX);
145  Serial.print(i++);
146  Serial.print(':');
147  Serial.println(A::e(5));
148 
149  // Keep up with the user
150  delay(500);
151 }
void c()
Definition: DebugDemo.ino:68
#define REGISTER(var)
Definition: Debug.h:381
void a()
Definition: DebugDemo.ino:54
void b()
Definition: DebugDemo.ino:60
char * buf
Definition: DebugDemo.ino:90
Definition: DebugDemo.ino:51
#define BREAK_IF(cond)
Definition: Debug.h:336
long e(int i)
Definition: DebugDemo.ino:79
#define ASSERT(cond)
Definition: Debug.h:313
void setup()
Definition: DebugDemo.ino:92
const size_t BUF_MAX
Definition: DebugDemo.ino:89
#define OBSERVE(expr)
Definition: Debug.h:373
void loop()
Definition: DebugDemo.ino:125
#define BREAKPOINT()
Definition: Debug.h:325
#define OBSERVE_IF(cond, expr)
Definition: Debug.h:360
#define CHECK_STACK(room)
Definition: Debug.h:347
#define DEBUG_STREAM(dev)
Definition: Debug.h:299