This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
PrintStream.cpp
Go to the documentation of this file.
1 // https://github.com/tttapa/Arduino-PrintStream/blob/6a9e0d365be0b3d84187daa2a8a7bda8d541472e/src/PrintStream.cpp
2 
3 #include "PrintStream.hpp"
4 
5 // LCOV_EXCL_START
6 
7 #if not defined(ARDUINO_ARCH_ESP32) && not defined(ARDUINO_ARCH_SAM)
8 #define FLUSH
9 #endif
10 
11  /* #define OCT 8 */
12 
13 uint8_t formatPrintStream = DEC;
14 bool boolalphaPrintStream = false;
18 enum : char{
19  LOWERCASE = 0x7F,
20  UPPERCASE = 0x5F
22 bool showbasePrintStream = false;
23 
24 template <class T>
25 Print &printIntegral(Print &printer, T i);
26 
27 Print &endl(Print &printer) {
28  printer.println();
29 #ifdef FLUSH
30  printer.flush();
31 #endif
32  return printer;
33 }
34 
35 Print &uppercase(Print &printer) {
37  return printer;
38 }
39 
40 Print &nouppercase(Print &printer) {
42  return printer;
43 }
44 
45 Print &showbase(Print &printer) {
46  showbasePrintStream = true;
47  return printer;
48 }
49 
50 Print &noshowbase(Print &printer) {
51  showbasePrintStream = false;
52  return printer;
53 }
54 
55 Print &flush(Print &printer) {
56 #ifdef FLUSH
57  printer.flush();
58 #endif
59  return printer;
60 }
61 
62 Print &hex(Print &printer) {
63  formatPrintStream = HEX;
64  return printer;
65 }
66 
67 /* Print &oct(Print &printer) {
68  formatPrintStream = OCT;
69  return printer;
70 } */
71 
72 Print &bin(Print &printer) {
73  formatPrintStream = BIN;
74  return printer;
75 }
76 
77 Print &dec(Print &printer) {
78  formatPrintStream = DEC;
79  return printer;
80 }
81 
82 Print &boolalpha(Print &printer) {
83  boolalphaPrintStream = true;
84  return printer;
85 }
86 Print &noboolalpha(Print &printer) {
87  boolalphaPrintStream = false;
88  return printer;
89 }
90 
91 Print &leadingzeros(Print &printer) {
93  return printer;
94 }
95 Print &noleadingzeros(Print &printer) {
97  return printer;
98 }
99 Print &operator<<(Print &printer, const __FlashStringHelper *s) {
100  printer.print(s);
101  return printer;
102 }
103 #ifdef ARDUINO
104 Print &operator<<(Print &printer, const String &s) {
105  printer.print(s);
106  return printer;
107 }
108 #endif
109 Print &operator<<(Print &printer, const char s[]) {
110  printer.print(s);
111  return printer;
112 }
113 Print &operator<<(Print &printer, char c) {
114  printer.print(c);
115  return printer;
116 }
117 Print &operator<<(Print &printer, unsigned char i) {
118  return printIntegral(printer, i);
119 }
120 Print &operator<<(Print &printer, int i) {
121  return printIntegral(printer, i);
122 }
123 Print &operator<<(Print &printer, unsigned int i) {
124  return printIntegral(printer, i);
125 }
126 Print &operator<<(Print &printer, int8_t i) {
127  return printIntegral(printer, i);
128 }
129 Print &operator<<(Print &printer, long i) {
130  return printIntegral(printer, i);
131 }
132 Print &operator<<(Print &printer, unsigned long i) {
133  return printIntegral(printer, i);
134 }
135 Print &operator<<(Print &printer, double d) {
136  printer.print(d, precisionPrintStream);
137  return printer;
138 }
139 Print &operator<<(Print &printer, const Printable& p) {
140  printer.print(p);
141  return printer;
142 }
143 Print &operator<<(Print &printer, bool b) {
145  printer.print(b ? F("true") : F("false"));
146  else
147  printer.print(b);
148  return printer;
149 }
150 
151 template <class T>
152 Print &printIntegral(Print &printer, T i) {
153  switch (formatPrintStream)
154  {
155  case DEC:
156  printer.print(i);
157  break;
158  case HEX:
159  printHex(printer, i);
160  break;
161  case BIN:
162  printBin(printer, i);
163  break;
164  /* case OCT:
165  printOct(printer, i);
166  break; */
167  default:
168  break;
169  }
170  return printer;
171 }
172 
173 Print &operator<<(Print &printer, manipulator pf) {
174  return pf(printer);
175 }
176 
177 Setbase setbase(uint8_t base) {
178  return { base };
179 }
180 Print &operator<<(Print &printer, Setbase f) {
182  return printer;
183 }
184 
185 Setbytesep setbytesep(char bytesep) {
186  return { bytesep };
187 }
188 Print &operator<<(Print &printer, Setbytesep f) {
190  return printer;
191 }
192 
194  return { n };
195 }
196 Print &operator<<(Print &printer, Setprecision f) {
198  return printer;
199 }
200 
201 static char nibble_to_hex(uint8_t nibble) { // convert a 4-bit nibble to a hexadecimal character
202  nibble &= 0xF;
203  return nibble > 9 ? nibble - 10 + ('a' & casePrintStream) : nibble + '0';
204 }
205 
206 #if __BYTE_ORDER != __LITTLE_ENDIAN
207 #error "Byte order not supported"
208 #endif
209 
210 template <class T>
211 void printHex(Print &printer, T val)
212 {
214  printer.print("0x");
215  bool nonZero = false;
216  for (int i = sizeof(val) - 1; i >= 0; i--)
217  {
218  uint8_t currByte = ((uint8_t *)&val)[i];
219  if (currByte != 0 || i == 0)
220  nonZero = true;
221  if (leadingZerosPrintStream || nonZero) {
222  printer.print(nibble_to_hex(currByte >> 4));
223  printer.print(nibble_to_hex(currByte));
224  if (byteSeparatorPrintStream && i)
225  printer.print(byteSeparatorPrintStream);
226  }
227  }
228 }
229 
230 template <class T>
231 void printBin(Print &printer, T val)
232 {
234  printer.print("0b");
235  bool nonZero = false;
236  for (int i = sizeof(val) - 1; i >= 0; i--)
237  {
238  uint8_t currByte = ((uint8_t *)&val)[i];
239  for (int j = 7; j >= 0; j--)
240  {
241  uint8_t currBit = currByte & 0x80;
242  if (currBit != 0 || (i == 0 && j == 0))
243  nonZero = true;
244  if (leadingZerosPrintStream || nonZero)
245  printer.print(currBit ? '1' : '0');
246  currByte <<= 1;
247  }
248  if (byteSeparatorPrintStream && i && (leadingZerosPrintStream || nonZero))
249  printer.print(byteSeparatorPrintStream);
250  }
251 }
252 
253 
254 /* template <class T>
255 void printOct(Print &printer, T val)
256 {
257  ; // TODO
258 } */
259 
260 // LCOV_EXCL_STOP
LOWERCASE
Definition: PrintStream.cpp:19
printHex
void printHex(Print &printer, T val)
Definition: PrintStream.cpp:211
bin
Print & bin(Print &printer)
Definition: PrintStream.cpp:72
showbasePrintStream
bool showbasePrintStream
Definition: PrintStream.cpp:22
nibble_to_hex
static char nibble_to_hex(uint8_t nibble)
Definition: PrintStream.cpp:201
boolalpha
Print & boolalpha(Print &printer)
Definition: PrintStream.cpp:82
UPPERCASE
Definition: PrintStream.cpp:20
manipulator
Print & manipulator(Print &)
PrintStream library
Definition: PrintStream.hpp:9
noshowbase
Print & noshowbase(Print &printer)
Definition: PrintStream.cpp:50
Setbytesep::M_bytesep
char M_bytesep
Definition: PrintStream.hpp:52
flush
Print & flush(Print &printer)
Definition: PrintStream.cpp:55
noboolalpha
Print & noboolalpha(Print &printer)
Definition: PrintStream.cpp:86
leadingzeros
Print & leadingzeros(Print &printer)
Definition: PrintStream.cpp:91
boolalphaPrintStream
bool boolalphaPrintStream
Definition: PrintStream.cpp:14
setbase
Setbase setbase(uint8_t base)
Definition: PrintStream.cpp:177
formatPrintStream
uint8_t formatPrintStream
Definition: PrintStream.cpp:13
byteSeparatorPrintStream
char byteSeparatorPrintStream
Definition: PrintStream.cpp:17
printBin
void printBin(Print &printer, T val)
Definition: PrintStream.cpp:231
Setbytesep
Definition: PrintStream.hpp:52
uppercase
Print & uppercase(Print &printer)
Definition: PrintStream.cpp:35
Setbase
Definition: PrintStream.hpp:44
nouppercase
Print & nouppercase(Print &printer)
Definition: PrintStream.cpp:40
printIntegral
Print & printIntegral(Print &printer, T i)
Definition: PrintStream.cpp:152
operator<<
Print & operator<<(Print &printer, const __FlashStringHelper *s)
Definition: PrintStream.cpp:99
setprecision
Setprecision setprecision(int n)
Definition: PrintStream.cpp:193
noleadingzeros
Print & noleadingzeros(Print &printer)
Definition: PrintStream.cpp:95
dec
Print & dec(Print &printer)
Definition: PrintStream.cpp:77
endl
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
hex
Print & hex(Print &printer)
Definition: PrintStream.cpp:62
precisionPrintStream
uint8_t precisionPrintStream
Definition: PrintStream.cpp:16
casePrintStream
enum @3 casePrintStream
PrintStream.hpp
Setbase::M_base
uint8_t M_base
Definition: PrintStream.hpp:44
leadingZerosPrintStream
bool leadingZerosPrintStream
Definition: PrintStream.cpp:15
setbytesep
Setbytesep setbytesep(char bytesep)
Definition: PrintStream.cpp:185
showbase
Print & showbase(Print &printer)
Definition: PrintStream.cpp:45
Setprecision
Definition: PrintStream.hpp:48
Setprecision::M_n
int M_n
Definition: PrintStream.hpp:48
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23