[Update] dbg.h : debug library
history:
version 1.1
- fix: reduced debug array 1000 10. highly recommend download , use version instead... or nothing works. oops.
- fix: renamed couple of global variables remove sketch clashes
version 1.0
- example sketch, library, keywords , empty library released
- version, addvar, addpin, dump , debug commands implemented
example:
the current example program:
the output example program, displayed in tellurium:
raw output, showing debug library version:

parsed variable output, showing variables , pins grouped, , effect of sending "hello":

files:
there 4 files associated library:
installation:
the dbg , nodbg header files should placed corresponding dbg , nodbg directories in arduino libraries folder. place keywords.txt file in dbg , nodbg directories also. have colour coded dbg commands keyword1 stand out bit.
commands:
all commands have been created #define macros in dbg.h. these same macros have been defined "empty" macros in nodbg.h, allowing switch between 2 include files , remove debugging actions , easily, whilst leaving debugging commands in code.
commands have been (fairly) reliably implemented are:
nb: if go poking around, notice there couple of other commands present in dbg.h, correct operation not guaranteed.
roadmap: (in random order)
- fix: range checking on debug array
- fix: store debug variables in linked list instead of array
- fix: handle [local function] variable debugging gracefully
- add: sketch flow - sketch actions, function entry & exit, function return values
background
in development, have found debugging incredibly useful in working out going on. true when goes wrong.
with arduino, serial port obvious communication conduit, allowing send data serial monitor viewing / debugging purposes.
it wasn't long before got sick of doing things like
serial.print("some variable name: ");
serial.println(somevariable);
and sought put library make debugging process little easier.
i had found arduino serial monitor less optimal debugging purposes, clearing sent text, resetting board @ start , reluctance appear when pressed ctrl+shift+m...
accumulating gross amounts of text in serial monitor not helpful expected either.
so goal develop serial monitor cooperate debugging sent arduino debugging library. serial monitor has been released. more information here: http://forum.arduino.cc/index.php?topic=169518.0
the other thing challenge way arduino sketches work, looping around, careful placement of debug statements required prevent torrent form serial port flooding serial monitor. end, debug library tracks variable values, , sends them serial port when change. still need mindful of debug placement, assistance provided via mechanism.
version 1.1
- fix: reduced debug array 1000 10. highly recommend download , use version instead... or nothing works. oops.
- fix: renamed couple of global variables remove sketch clashes
version 1.0
- example sketch, library, keywords , empty library released
- version, addvar, addpin, dump , debug commands implemented
example:
the current example program:
code: [select]
#include "arduino.h"
#include "dbg.h"
//#include "nodbg.h"
#define pin13 13
int charcount = 0;
string inputbuffer;
byte dpin13; // digital pin 13 (the onboard led)
void setup() {
serial.begin(9600);
serial.println("ready");
pinmode(pin13, input);
dbg_version; // output version
dbg_addvar("number of characters received", charcount) // add variable
dbg_addvar(f("serial input buffer"), inputbuffer) // add variable
dbg_addpin(f("onboard led"), dpin13) // add pin
dbg_dump; // debug added variables // output debug variables
}
void loop() {
dpin13 = digitalread(pin13);
dbg_debug(dpin13); // debug variable
}
void serialevent() {
while (serial.available()) {
charcount++;
char inchar = serial.read();
inputbuffer += inchar;
switch (inchar) {
case 'z' : // let's clear buffer
serial.println("\nclearing input buffer");
inputbuffer = "";
break;
default : serial.print(inchar);
}
}
dbg_debug(charcount)
dbg_debug(inputbuffer)
}
the output example program, displayed in tellurium:
raw output, showing debug library version:

parsed variable output, showing variables , pins grouped, , effect of sending "hello":

files:
there 4 files associated library:
- dbg.h - debug library (no .cpp file required) download: https://dl.dropboxusercontent.com/u/85621331/dbg.h
- nodbg.h - debug library include use when want remove debugging not debugging statements download: https://dl.dropboxusercontent.com/u/85621331/nodbg.h
- keywords.txt - use arduino ide download: https://dl.dropboxusercontent.com/u/85621331/keywords.txt
- debug.ino - example sketch download: https://dl.dropboxusercontent.com/u/85621331/debug.ino
installation:
the dbg , nodbg header files should placed corresponding dbg , nodbg directories in arduino libraries folder. place keywords.txt file in dbg , nodbg directories also. have colour coded dbg commands keyword1 stand out bit.
commands:
all commands have been created #define macros in dbg.h. these same macros have been defined "empty" macros in nodbg.h, allowing switch between 2 include files , remove debugging actions , easily, whilst leaving debugging commands in code.
commands have been (fairly) reliably implemented are:
- dbg_version: print version of dbg library serial port
- dbg_addvar(descriptive name, variable): associate descriptive name variable , add debug array
- dbg_addpin(descriptive name, variable): associate descriptive name variable, flag pin , add debug array
- dbg_dump: send debug variables serial port
- dbg_debug(variable): send variable serial port
nb: if go poking around, notice there couple of other commands present in dbg.h, correct operation not guaranteed.
roadmap: (in random order)
- fix: range checking on debug array
- fix: store debug variables in linked list instead of array
- fix: handle [local function] variable debugging gracefully
- add: sketch flow - sketch actions, function entry & exit, function return values
background
in development, have found debugging incredibly useful in working out going on. true when goes wrong.
with arduino, serial port obvious communication conduit, allowing send data serial monitor viewing / debugging purposes.
it wasn't long before got sick of doing things like
serial.print("some variable name: ");
serial.println(somevariable);
and sought put library make debugging process little easier.
i had found arduino serial monitor less optimal debugging purposes, clearing sent text, resetting board @ start , reluctance appear when pressed ctrl+shift+m...
accumulating gross amounts of text in serial monitor not helpful expected either.
so goal develop serial monitor cooperate debugging sent arduino debugging library. serial monitor has been released. more information here: http://forum.arduino.cc/index.php?topic=169518.0
the other thing challenge way arduino sketches work, looping around, careful placement of debug statements required prevent torrent form serial port flooding serial monitor. end, debug library tracks variable values, , sends them serial port when change. still need mindful of debug placement, assistance provided via mechanism.
here's library, don't have download see it:
code: [select]
#include "arduino.h"
#ifndef dbg_h
#define dbg_h
#define _dbg_version_ 1.1
#define dbg_addarray(s1, s2) dbgarray[dbgcounter] = newarraychild(s1, *s2, sizeof(s2)/sizeof(s2[0])); dbgcounter++;
#define dbg_addpin(s1, s2) dbgarray[dbgcounter] = newchild(s1, s2, true); dbgcounter++;
#define dbg_addpinarray(s1, s2) dbgarray[dbgcounter] = newarraychild(s1, *s2, sizeof(s2)/sizeof(s2[0]), true); dbgcounter++;
#define dbg_addvar(s1, s2) dbgarray[dbgcounter] = newchild(s1, s2); dbgcounter++;
#define dbg_debug(s1) dbg_debug(s1);
#define dbg_dump (int = 0; < dbgcounter; i++) dbgarray[i] -> debug();
#define dbg_version serial.print("\ndbg version: "); serial.println(_dbg_version_);
class dbg_baseclass {
protected:
bool isdone;
bool ispin;
public:
dbg_baseclass() {isdone = false;};
virtual void debug() {};
virtual bool isequal(void *thisvarptr) {};
};
template <typename t1, typename t2>
class dbg_childclass : public dbg_baseclass {
private:
t1 variablename;
t2 lastvalue;
t2 *variableptr;
int arraysize;
bool valuechanged() {
if (arraysize ==1) return (*variableptr != lastvalue);
else return true; // (memcmp(variableptr, &lastvalue, arraysize) == 0);
};
void savevalue() {
if (arraysize ==1) lastvalue = *variableptr;
else {
// memcpy(*variableptr, lastvalue, arraysize);
lastvalue = *variableptr;
}
}
/*
control codes used in sending info out serial port:
sstx = #02; // start text - start of set of data
setx = #03; // end of text - plain variable
seot = #04; // end of transmission - complete set of data
senq = #05; // enquiry - pins
sack = #06; // acknowledge - actions
sso = #14; // shift out - return value function
*/
void divider() {
if (!ispin) serial.print((char)0x03);
else serial.print((char)0x05);
};
void debugstart() {
serial.print((char)0x02);
serial.print(variablename);
divider();
};
void debugend() { serial.print((char)0x04); };
void debugvalue() {
debugstart();
serial.print(*variableptr);
debugend();
};
public:
dbg_childclass(t1 thisvariablename, t2 &thisvariable, int thissize = 1, bool thisispin = false) {
arraysize = thissize;
ispin = thisispin;
variablename = thisvariablename;
variableptr = &thisvariable;
lastvalue = thisvariable;
};
virtual void debug() {
if ((!isdone) || (valuechanged())) {
isdone = true;
savevalue();
if (arraysize == 1) debugvalue();
else {
debugstart();
(int = 0; < arraysize; i++) {
if (variableptr[i] == null) break;
serial.print(variableptr[i]); // output next value array
serial.print(" "); // separate array values spaces
}
debugend();
}
}
};
// addresses same
virtual bool isequal(void *thisvarptr) { return (thisvarptr == variableptr); };
};
template <typename t1, typename t2>
dbg_baseclass *newchild(t1 thisvariablename, t2 &thisvariable, bool thisispin = false) {
return new dbg_childclass<t1, t2>(thisvariablename, thisvariable, 1, thisispin);
};
template <typename t1, typename t2>
dbg_baseclass *newarraychild(t1 thisvariablename, t2 &thisvariable, int thissize, bool thisispin = false) {
return new dbg_childclass<t1, t2>(thisvariablename, thisvariable, thissize, thisispin);
};
int dbgcounter = 0;
dbg_baseclass *dbgarray[10];
template <typename t>
void dbg_debug(t &thisvariable) {
(int = 0; < dbgcounter; i++) {
if (dbgarray[i] -> isequal(&thisvariable)) {
dbgarray[i] -> debug();
break;
}
}
};
#endif
Arduino Forum > Development > Other Software Development > [Update] dbg.h : debug library
arduino
Comments
Post a Comment