-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd891ae
Showing
7 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "Dbg.h" | ||
|
||
#ifdef DEBUG_ENABLED | ||
|
||
#define DBG_MAX_LENGTH 80 | ||
|
||
Dbg::Dbg() { | ||
} | ||
|
||
void Dbg::begin(int serial, int rate ) | ||
{ | ||
switch(serial) { | ||
case 1: | ||
m_serial = &Serial; | ||
break; | ||
#if defined(__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) | ||
case 2: | ||
m_serial = &Serial1; | ||
break; | ||
case 3: | ||
m_serial = &Serial2; | ||
break; | ||
case 4: | ||
m_serial = &Serial3; | ||
break; | ||
#endif | ||
default: | ||
m_serial = &Serial; | ||
break; | ||
} | ||
|
||
m_serial->begin(rate); | ||
|
||
} | ||
|
||
void Dbg::log(const char *fmt, ...) | ||
{ | ||
char buf[DBG_MAX_LENGTH]; | ||
|
||
va_list ap; | ||
va_start(ap, fmt); | ||
|
||
vsnprintf(buf, (DBG_MAX_LENGTH - 1), fmt, ap); | ||
va_end(ap); | ||
m_serial->println(buf); | ||
} | ||
|
||
#endif | ||
|
||
Dbg Debug; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef _DBG_H_ | ||
#define _DBG_H | ||
#include <WProgram.h> | ||
|
||
#define DEBUG_ENABLED 1 | ||
|
||
#ifdef DEBUG_ENABLED | ||
class Dbg { | ||
public: | ||
Dbg(); | ||
|
||
void begin(int serial = 1, int rate = 9600); | ||
|
||
void log(const char *fmt, ...) | ||
__attribute__((format(printf, 2, 3))); | ||
|
||
private: | ||
HardwareSerial* m_serial; | ||
|
||
}; | ||
|
||
#define dbg(fmt, ...) (Debug.log(fmt, ## __VA_ARGS__)) | ||
|
||
#else | ||
class Dbg { | ||
public: | ||
Dbg(); | ||
|
||
void begin(int serial = 1, int rate = 9600) {}; | ||
void log(const char *fmt, ...) | ||
__attribute__((format(printf, 2, 3))) {}; | ||
|
||
}; | ||
|
||
#define dbg(fmt, ...) ((void)0) | ||
|
||
#endif | ||
|
||
extern Dbg Debug; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Dbg library for Arduino | ||
|
||
Version 1.0 | ||
|
||
by Byron Guernsey | ||
|
||
contact: [email protected] | ||
|
||
See the online documentation here: http://code.google.com/p/arduino-library-dbg/ | ||
See examples/ directory for simple examples | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <Dbg.h> | ||
|
||
char prg_name[] = "Debug test"; | ||
unsigned int counter = 0; | ||
|
||
void setup() { | ||
// The following requires an Arduino Mega, or similar board | ||
// with additional serial ports (2-4) | ||
|
||
// If compiled for an Uno, this will quietly default back to "Serial" | ||
|
||
// The valid id's for serial port numbers on Arduino Mega are: | ||
// 1 = Serial | ||
// 2 = Serial1 | ||
// 3 = Serial2 | ||
// 4 = Serial3 | ||
|
||
// Direct serial logging to Serial1 at 9600 baud | ||
Debug.begin(2,9600); | ||
|
||
Debug.log("Dbg Library initialized: %lu", millis()); | ||
|
||
Debug.log("This is the normal syntax"); | ||
dbg("This is the preferred shorthand syntax"); | ||
} | ||
|
||
void loop() { | ||
|
||
delay(1000); | ||
counter++; | ||
|
||
// Log prg_name string and counter value | ||
// using printf formatting syntax and shorthand macro | ||
dbg("[%s]: Time = %lu Counter = %d", prg_name, millis(), counter); | ||
|
||
// Outputs: | ||
// [Debug test]: Time = <time in millis> Counter = 1 | ||
// | ||
// ...on first pass... | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <Dbg.h> | ||
|
||
char prg_name[] = "Debug test"; | ||
unsigned int counter = 0; | ||
|
||
void setup() { | ||
// Direct serial logging to Serial (standard serial port) at default 9600 baud | ||
Debug.begin(); | ||
Debug.log("Dbg Library initialized: %lu", millis()); | ||
|
||
Debug.log("This is the normal syntax"); | ||
dbg("This is the preferred shorthand syntax that compiles out of existence when DEBUG_ENABLED is undefined"); | ||
|
||
// Notice that the above dbg() call will be truncated to 79 characters! | ||
// This is defined as the max length and can be increased in Dbg.cpp | ||
|
||
} | ||
|
||
void loop() { | ||
|
||
delay(1000); | ||
counter++; | ||
|
||
// Log prg_name string and counter value | ||
// using printf formatting syntax and shorthand macro | ||
dbg("[%s]: Time = %lu Counter = %d", prg_name, millis(), counter); | ||
|
||
// Outputs: | ||
// [Debug test]: Time = <time in millis> Counter = 1 | ||
// | ||
// ...on first pass... | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
####################################### | ||
# Syntax Coloring Map For Dbg library | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
Dbg KEYWORD1 | ||
dbg KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
begin KEYWORD2 | ||
log KEYWORD2 | ||
|
||
####################################### | ||
# Instances (KEYWORD2) | ||
####################################### | ||
Debug KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Dbg library for Arduino | ||
|
||
Version 1.0 | ||
|
||
by Byron Guernsey | ||
|
||
contact: [email protected] | ||
|
||
See the online documentation here: http://code.google.com/p/arduino-library-dbg/ | ||
See Dbg/examples/ directory for examples | ||
|
||
Installation: | ||
|
||
- Copy the Dbg/ directory and all contents to your Arduino/libraries/ directory | ||
- On Mac OSX this is ~/Documents/Arduino/libraries | ||
- On other systems its the directory containing your sketches | ||
- You may need to create the libraries/ subdirectory if it does not exist | ||
- Restart Arduino IDE to pick up new libraries | ||
|
||
Simple usage: | ||
|
||
#include <Dbg.h> | ||
|
||
int port = 1; // 1 = Serial, [Mega: 2 = Serial1, 3 = Serial2, 4 = Serial3 ] | ||
int speed = 9600; | ||
|
||
void setup() { | ||
|
||
Debug.begin(port, speed); | ||
Debug.log("Long form logging"); | ||
dbg("Short form logging"); | ||
dbg("Port: %d Speed: %d", port, speed); | ||
dbg("Inserting a %s is %s", "string", "easy"); | ||
|
||
} | ||
|
||
... | ||
|
||
Future changes may include: | ||
|
||
- Log levels | ||
- Fix for eliminating all dbg code with a macro | ||
|