AskSin++
Debug.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __DEBUG_H__
7 #define __DEBUG_H__
8 
9 #ifdef ARDUINO
10  #include "Arduino.h"
11 #endif
12 
13 // #define NDEBUG
14 
15 #ifdef NDEBUG
16 
17  #include <stdint.h>
18 
19  #define DPRINT(str)
20  #define DPRINTLN(str)
21  #define DHEX(b...)
22  #define DHEXLN(b)
23  #define DDEC(b)
24  #define DDECLN(b)
25 
26  #define DINIT(baudrate,msg)
27  #define DDEVINFO(dev)
28 
29 #else
30 
31 #ifdef ARDUINO
32 
33 // can be used to move the debug output to Serial1 (useful on STM32 BluePill)
34 // must be defined in main .ino before any eader inclusion!
35 #ifndef DSERIAL
36  #define DSERIAL Serial
37 #endif
38 
39  template <class T>
40  inline void DPRINT(T str) { DSERIAL.print(str); }
41  template <class T>
42  inline void DPRINTLN(T str) { DPRINT(str); DPRINT(F("\r\n")); }
43  inline void DHEX(uint8_t b) {
44  if( b<0x10 ) DSERIAL.print('0');
45  DSERIAL.print(b,HEX);
46  }
47  inline void DHEX(uint16_t b) {
48  if( b<0x10 ) DSERIAL.print(F("000"));
49  else if( b<0x100 ) DSERIAL.print(F("00"));
50  else if( b<0x1000 ) DSERIAL.print(F("0"));
51  DSERIAL.print(b,HEX);
52  }
53  inline void DHEX(uint32_t b) {
54  if( b<0x10 ) DSERIAL.print(F("0000000"));
55  else if( b<0x100 ) DSERIAL.print(F("000000"));
56  else if( b<0x1000 ) DSERIAL.print(F("00000"));
57  else if( b<0x10000 ) DSERIAL.print(F("0000"));
58  else if( b<0x100000 ) DSERIAL.print(F("000"));
59  else if( b<0x1000000 ) DSERIAL.print(F("00"));
60  else if( b<0x10000000 ) DSERIAL.print(F("0"));
61  DSERIAL.print(b,HEX);
62  }
63 
64  template<typename TYPE>
65  inline void DDEC(TYPE b) {
66  DSERIAL.print(b,DEC);
67  }
68 
69  #define DINIT(baudrate,msg) \
70  DSERIAL.begin(baudrate); \
71  DPRINTLN(msg);
72 
73  #define DDEVINFO(dev) \
74  HMID devid; \
75  dev.getDeviceID(devid); \
76  DPRINT(F("ID: "));devid.dump(); \
77  uint8_t serial[11]; \
78  dev.getDeviceSerial(serial); \
79  serial[10]=0; \
80  DPRINT(F(" Serial: "));DPRINTLN((char*)serial);
81 
82 #else
83 
84  #include <iostream>
85  #include <iomanip>
86 
87  #ifndef F
88  #define F(str) str
89  #endif
90 
91  template <class T>
92  inline void DPRINT(T str) { std::cout << str << std::flush; }
93  template <class T>
94  inline void DPRINTLN(T str) { std::cout << str << std::endl; }
95  inline void DHEX(uint8_t b) { std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)b; }
96  inline void DHEX(uint16_t b) { std::cout << std::setw(4) << std::setfill('0') << std::hex << (int)b; }
97  inline void DHEX(uint32_t b) { std::cout << std::setw(8) << std::setfill('0') << std::hex << (int)b; }
98 
99  template<typename TYPE>
100  inline void DDEC(TYPE b) { std::cout << std::dec << (int)b; }
101 
102 #endif // ARDUINO
103 
104  inline void DHEX(const uint8_t* b,uint8_t l) {
105  for( int i=0; i<l; i++, b++) {
106  DHEX(*b); DPRINT(F(" "));
107  }
108  }
109  inline void DHEXLN(uint8_t b) { DHEX(b); DPRINT(F("\r\n")); }
110  inline void DHEXLN(uint16_t b) { DHEX(b); DPRINT(F("\r\n")); }
111  inline void DHEXLN(uint32_t b) { DHEX(b); DPRINT(F("\r\n")); }
112  template<typename TYPE>
113  inline void DDECLN(TYPE b) { DDEC(b); DPRINT(F("\r\n")); }
114  inline void DHEXLN(const uint8_t* b,uint8_t l) { DHEX(b,l); DPRINT(F("\r\n")); }
115 
116 #endif
117 
118 #endif