aboutsummaryrefslogtreecommitdiff
path: root/Debug.h
blob: 9bf82bfe888f81da45c7ca7c157979e1af33facd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef DEBUG_H
#define DEBUG_H

#include "config-check.h"

#include <AltSoftSerial.h>

#ifdef ARDUINO_attiny
class Serial {
public:
  void begin(int);
  void write(uint8_t);
  int read();
  void print(...);
  void println(...);
  operator bool();
  bool available();
  void flush();
};

extern Serial Serial;
#endif

enum DebugSink {
  DEBUG_SINK_VOID,
  DEBUG_SINK_SERIAL,
  DEBUG_SINK_SOFTWARE_SERIAL,
};

template <enum DebugSink sink_, int rx_pin = 1, int tx_pin = 0>
class Debug
{
  AltSoftSerial ss;
public:
  const enum DebugSink sink;

  Debug() : ss(rx_pin, tx_pin), sink(sink_) {
  }

  inline void begin(const long baud_rate) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.begin(baud_rate);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.begin(baud_rate);
    }
  }

  inline operator bool() {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.operator bool();
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.operator bool();
    } else {
      return false;
    }
  };

  inline bool available() {
    if (sink_ == DEBUG_SINK_SERIAL) {
      return Serial.available();
    } if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      return this->ss.available();
    }

    return false;
  };

  inline int read() {
    if (sink_ == DEBUG_SINK_SERIAL) {
      return Serial.read();
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      return this->ss.read();
    }

    return '\0';
  };

  size_t write(uint8_t value) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.write(value);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.write(value);
    }

    return value;
  }

  template<typename T>
  inline
  void print(T t, int format = DEC) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.print(t, format);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.print(t, format);
    }
  };

  template<typename T>
  inline
  void print(const T *t) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.print(t);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.print(t);
    }
  };

  template<typename T>
  inline
  void print(T *t) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.print(t);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.print(t);
    }
  };

  template<typename T>
  inline
  void println(T t, int format = DEC) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.println(t, format);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.println(t, format);
    }
  };

  template<typename T>
  inline
  void println(const T *t) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.println(t);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.println(t);
    }
  };

  template<typename T>
  inline
  void println(T *t) {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.println(t);
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.println(t);
    }
  };

  inline
  void println() {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.println();
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.println();
    }
  };

  inline
  void flush() {
    if (sink_ == DEBUG_SINK_SERIAL) {
      Serial.flush();
    } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) {
      this->ss.flush();
    }
  };
};

#if DEBUG_SINK == 0
static Debug<DEBUG_SINK_VOID> debug;
#elif DEBUG_SINK == 1
static Debug<DEBUG_SINK_SERIAL> debug;
#elif DEBUG_SINK == 2
static Debug<DEBUG_SINK_SOFTWARE_SERIAL> debug;
#else
#error Invalid value for DEBUG_SINK, must be one of 1 (for no output), 2 for serial port, or 3 for software serial.
#endif

#endif