Skip to content

Commit e4f67c7

Browse files
committed
Adding enableDebugging plus helper functions for print, println and printf
1 parent 5fda6e4 commit e4f67c7

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/ICM_20948.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,61 @@ ICM_20948::ICM_20948()
1414
{
1515
}
1616

17+
void ICM_20948::enableDebugging(Stream &debugPort)
18+
{
19+
_debugSerial = &debugPort; //Grab which port the user wants us to use for debugging
20+
_printDebug = true; //Should we print the commands we send? Good for debugging
21+
}
22+
void ICM_20948::disableDebugging(void)
23+
{
24+
_printDebug = false; //Turn off extra print statements
25+
}
26+
27+
// Debug Printing: based on gfvalvo's flash string helper code:
28+
// https://forum.arduino.cc/index.php?topic=533118.msg3634809#msg3634809
29+
30+
void ICM_20948::debugPrint(const char *line)
31+
{
32+
doDebugPrint([](const char *ptr) {return *ptr;}, line);
33+
}
34+
35+
void ICM_20948::debugPrint(const __FlashStringHelper *line)
36+
{
37+
doDebugPrint([](const char *ptr) {return (char) pgm_read_byte_near(ptr);},
38+
(const char*) line);
39+
}
40+
41+
void ICM_20948::debugPrintln(const char *line)
42+
{
43+
doDebugPrint([](const char *ptr) {return *ptr;}, line, true);
44+
}
45+
46+
void ICM_20948::debugPrintln(const __FlashStringHelper *line)
47+
{
48+
doDebugPrint([](const char *ptr) {return (char) pgm_read_byte_near(ptr);},
49+
(const char*) line, true);
50+
}
51+
52+
void ICM_20948::doDebugPrint(char (*funct)(const char *), const char *string, bool newLine)
53+
{
54+
if (_printDebug == false)
55+
return; // Bail if debugging is not enabled
56+
57+
char ch;
58+
59+
while ((ch = funct(string++)))
60+
{
61+
_debugSerial->print(ch);
62+
}
63+
64+
if (newLine)
65+
{
66+
_debugSerial->println();
67+
}
68+
}
69+
70+
71+
1772
ICM_20948_AGMT_t ICM_20948::getAGMT(void)
1873
{
1974
status = ICM_20948_get_agmt(&_device, &agmt);

src/ICM_20948.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ A C++ interface to the ICM-20948
2020
class ICM_20948
2121
{
2222
private:
23+
Stream *_debugSerial; //The stream to send debug messages to if enabled
24+
boolean _printDebug = false; //Flag to print the serial commands we are sending to the Serial port for debug
25+
2326
protected:
2427
ICM_20948_Device_t _device;
2528

@@ -31,6 +34,43 @@ class ICM_20948
3134
public:
3235
ICM_20948(); // Constructor
3336

37+
// Enable debug messages using the chosen Serial port (Stream)
38+
// Boards like the RedBoard Turbo use SerialUSB (not Serial).
39+
// But other boards like the SAMD51 Thing Plus use Serial (not SerialUSB).
40+
// These lines let the code compile cleanly on as many SAMD boards as possible.
41+
#if defined(ARDUINO_ARCH_SAMD) // Is this a SAMD board?
42+
#if defined(USB_VID) // Is the USB Vendor ID defined?
43+
#if (USB_VID == 0x1B4F) // Is this a SparkFun board?
44+
#if !defined(ARDUINO_SAMD51_THING_PLUS) & !defined(ARDUINO_SAMD51_MICROMOD) // If it is not a SAMD51 Thing Plus or SAMD51 MicroMod
45+
void enableDebugging(Stream &debugPort = SerialUSB); //Given a port to print to, enable debug messages.
46+
#else
47+
void enableDebugging(Stream &debugPort = Serial); //Given a port to print to, enable debug messages.
48+
#endif
49+
#else
50+
void enableDebugging(Stream &debugPort = Serial); //Given a port to print to, enable debug messages.
51+
#endif
52+
#else
53+
void enableDebugging(Stream &debugPort = Serial); //Given a port to print to, enable debug messages.
54+
#endif
55+
#else
56+
void enableDebugging(Stream &debugPort = Serial); //Given a port to print to, enable debug messages.
57+
#endif
58+
59+
void disableDebugging(void); //Turn off debug statements
60+
61+
// gfvalvo's flash string helper code: https://forum.arduino.cc/index.php?topic=533118.msg3634809#msg3634809
62+
void debugPrint(const char *);
63+
void debugPrint(const __FlashStringHelper *);
64+
void debugPrintln(const char *);
65+
void debugPrintln(const __FlashStringHelper *);
66+
void doDebugPrint(char (*)(const char *), const char *, bool newLine = false);
67+
68+
// Debug printf - not glamorous but it works...
69+
#define debugPrintf1( var ) {if (_printDebug == true) _debugSerial->printf( var );}
70+
#define debugPrintf2( var1, var2 ) {if (_printDebug == true) _debugSerial->printf( var1, var2 );}
71+
#define debugPrintf3( var1, var2, var3 ) {if (_printDebug == true) _debugSerial->printf( var1, var2, var3 );}
72+
#define debugPrintf4( var1, var2, var3, var4 ) {if (_printDebug == true) _debugSerial->printf( var1, var2, var3, var4 );}
73+
3474
ICM_20948_AGMT_t agmt; // Acceleometer, Gyroscope, Magenetometer, and Temperature data
3575
ICM_20948_AGMT_t getAGMT(void); // Updates the agmt field in the object and also returns a copy directly
3676

0 commit comments

Comments
 (0)