Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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 not defined(ARDUINO_API_VERSION) && not defined(ArduinoFake) && \
9 not defined(ARDUINO_ARCH_STM32)
10#define FLUSH
11#endif
12
14
15/* #define OCT 8 */
16
22enum : char { LOWERCASE = 0x7F, UPPERCASE = 0x5F } casePrintStream = LOWERCASE;
24
25template <class T>
26Print &printIntegral(Print &printer, T i);
27
28Print &endl(Print &printer) {
29 printer.println();
30#ifdef FLUSH
31 printer.flush();
32#endif
33 return printer;
34}
35
36Print &uppercase(Print &printer) {
38 return printer;
39}
40
41Print &nouppercase(Print &printer) {
43 return printer;
44}
45
46Print &showbase(Print &printer) {
48 return printer;
49}
50
51Print &noshowbase(Print &printer) {
52 showbasePrintStream = false;
53 return printer;
54}
55
56Print &flush(Print &printer) {
57#ifdef FLUSH
58 printer.flush();
59#endif
60 return printer;
61}
62
63Print &hex(Print &printer) {
65 return printer;
66}
67
68/* Print &oct(Print &printer) {
69 formatPrintStream = OCT;
70 return printer;
71} */
72
73Print &bin(Print &printer) {
75 return printer;
76}
77
78Print &dec(Print &printer) {
80 return printer;
81}
82
83Print &boolalpha(Print &printer) {
85 return printer;
86}
87Print &noboolalpha(Print &printer) {
89 return printer;
90}
91
92Print &leadingzeros(Print &printer) {
94 return printer;
95}
96Print &noleadingzeros(Print &printer) {
98 return printer;
99}
100Print &operator<<(Print &printer, const __FlashStringHelper *s) {
101 printer.print(s);
102 return printer;
103}
104#ifdef ARDUINO
105Print &operator<<(Print &printer, const String &s) {
106 printer.print(s);
107 return printer;
108}
109#endif
110Print &operator<<(Print &printer, const char s[]) {
111 printer.print(s);
112 return printer;
113}
114Print &operator<<(Print &printer, char c) {
115 printer.print(c);
116 return printer;
117}
118Print &operator<<(Print &printer, unsigned char i) {
119 return printIntegral(printer, i);
120}
121Print &operator<<(Print &printer, int i) { return printIntegral(printer, i); }
122Print &operator<<(Print &printer, unsigned int i) {
123 return printIntegral(printer, i);
124}
125Print &operator<<(Print &printer, int8_t i) {
126 return printIntegral(printer, i);
127}
128Print &operator<<(Print &printer, long i) { return printIntegral(printer, i); }
129Print &operator<<(Print &printer, unsigned long i) {
130 return printIntegral(printer, i);
131}
132Print &operator<<(Print &printer, double d) {
134 return printer;
135}
136Print &operator<<(Print &printer, float f) {
138}
139Print &operator<<(Print &printer, const Printable &p) {
140 printer.print(p);
141 return printer;
142}
143Print &operator<<(Print &printer, bool b) {
145 printer.print(b ? F("true") : F("false"));
146 else
147 printer.print(b);
148 return printer;
149}
150
151Print &operator<<(Print &printer, manipulator pf) { return pf(printer); }
152
153Setbase setbase(uint8_t base) { return {base}; }
154Print &operator<<(Print &printer, Setbase f) {
155 formatPrintStream = f.M_base;
156 return printer;
157}
158
159Setbytesep setbytesep(char bytesep) { return {bytesep}; }
161 byteSeparatorPrintStream = f.M_bytesep;
162 return printer;
163}
164
165Setprecision setprecision(int n) { return {n}; }
168 return printer;
169}
170
171static char nibble_to_hex(
172 uint8_t nibble) { // convert a 4-bit nibble to a hexadecimal character
173 nibble &= 0xF;
174 return nibble > 9 ? nibble - 10 + ('a' & casePrintStream) : nibble + '0';
175}
176
177#if __BYTE_ORDER != __LITTLE_ENDIAN
178#error "Byte order not supported"
179#endif
180
181template <class T>
182void printHex(Print &printer, T val) {
184 printer.print("0x");
185 bool nonZero = false;
186 for (int i = sizeof(val) - 1; i >= 0; i--) {
187 uint8_t currByte = ((uint8_t *)&val)[i];
188 if (currByte != 0 || i == 0)
189 nonZero = true;
191 printer.print(nibble_to_hex(currByte >> 4));
195 }
196 }
197}
198
199template <class T>
200void printBin(Print &printer, T val) {
202 printer.print("0b");
203 bool nonZero = false;
204 for (int i = sizeof(val) - 1; i >= 0; i--) {
205 uint8_t currByte = ((uint8_t *)&val)[i];
206 for (int j = 7; j >= 0; j--) {
207 uint8_t currBit = currByte & 0x80;
208 if (currBit != 0 || (i == 0 && j == 0))
209 nonZero = true;
211 printer.print(currBit ? '1' : '0');
212 currByte <<= 1;
213 }
217 }
218}
219
220/* template <class T>
221void printOct(Print &printer, T val)
222{
223 ; // TODO
224} */
225
226template <class T>
227Print &printIntegral(Print &printer, T i) {
228 switch (formatPrintStream) {
229 case DEC: printer.print(i); break;
230 case HEX: printHex(printer, i); break;
231 case BIN: printBin(printer, i); break;
232 /* case OCT:
233 printOct(printer, i);
234 break; */
235 default: break;
236 }
237 return printer;
238}
239
240Print &operator<<(Print &p, HexDump h) {
241 if (h.length == 0)
242 return p;
243
246 while (h.length-- > 1) {
247 printHex(p, *h.data++);
248 p.print(' ');
249 }
250 printHex(p, *h.data++);
252 return p;
253}
254
255#ifndef ARDUINO
256
257std::ostream &operator<<(std::ostream &p, HexDump h) {
258 if (h.length == 0)
259 return p;
260
261 auto hex_nibble_to_char = [](uint8_t nibble) -> char {
262 nibble &= 0xF;
263 return nibble > 9 ? nibble - 10 + 'A' : nibble + '0';
264 };
265 auto printHex = [&](std::ostream &p, uint8_t b) {
266 p << hex_nibble_to_char(b >> 4) << hex_nibble_to_char(b);
267 };
268
269 while (h.length-- > 1) {
270 printHex(p, *h.data++);
271 p << ' ';
272 }
273 printHex(p, *h.data++);
274 return p;
275}
276
277#endif
278
280
281// LCOV_EXCL_STOP
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Print & operator<<(Print &os, Cable c)
Definition Cable.cpp:6
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
Print & printIntegral(Print &printer, T i)
char byteSeparatorPrintStream
@ UPPERCASE
@ LOWERCASE
static char nibble_to_hex(uint8_t nibble)
void printBin(Print &printer, T val)
uint8_t formatPrintStream
bool boolalphaPrintStream
bool leadingZerosPrintStream
uint8_t precisionPrintStream
void printHex(Print &printer, T val)
bool showbasePrintStream
enum AH::@2 casePrintStream