Display_Lib_RPI 2.3.0
A C++ Library to connect electronic displays to Linux single board computers.
Loading...
Searching...
No Matches
print_data_RDL.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <cinttypes>
13#include <cstdio> // for size_t
14#include <cstring>
15#include <cmath>
16#include <string>
17#include <vector>
18#include <array>
19#include <bitset> // required for Binary conversion
20#include <iomanip> // required for std::setprecision
21#include <sstream> // Required for std::ostringstream
22
28class Print
29{
30public:
31 Print() : _ErrorFlag(0) {}
32
34 enum BaseNum : uint8_t{
35 RDL_DEC = 10,
36 RDL_HEX = 16,
37 RDL_OCT = 8,
38 RDL_BIN = 2
39 };
40
41 int getWriteError();
42 void clearWriteError();
44 virtual size_t write(uint8_t) = 0;
46 virtual size_t write(const uint8_t *buffer, size_t size);
47 size_t write(const char *str);
48 size_t write(const char *buffer, size_t size);
50 virtual int availableForWrite();
51
52 size_t print(const char[]);
53 size_t print(char);
54 size_t print(int, int = RDL_DEC);
55 size_t print(unsigned int, int = RDL_DEC);
56 size_t print(long, int = RDL_DEC);
57 size_t print(unsigned long, int = RDL_DEC);
58 size_t print(double, int = 2);
59 size_t print(const std::string &);
60
61 size_t println(const char[]);
62 size_t println(char);
63 size_t println(int, int = RDL_DEC);
64 size_t println(unsigned int, int = RDL_DEC);
65 size_t println(long, int = RDL_DEC);
66 size_t println(unsigned long, int = RDL_DEC);
67 size_t println(double, int = 2);
68 size_t println(void);
69 size_t println(const std::string &s);
70
80 template <typename T>
81 size_t print(const std::vector<T> &v, int format = defaultFormat<T>()) {
82 size_t totalWritten = 0;
83 for (const auto &element : v) {
84 std::string elementStr = formatElement(element, format);
85 totalWritten += write(elementStr.c_str(), elementStr.length());
86
87 // Add a separator space if not the last element
88 if (&element != &v.back()) {
89 totalWritten += write(" ", 1);
90 }
91 }
92 return totalWritten;
93 }
94
102 template <typename T>
103 size_t println(const std::vector<T> &vec, int format = defaultFormat<T>())
104 {
105 size_t chars = print(vec, format); // Use default format for the type
106 chars += print("\r\n"); // Add a newline
107 return chars;
108 }
109
120 template <typename T, size_t N>
121 size_t print(const std::array<T, N> &arr, int format = defaultFormat<T>())
122 {
123 return print(std::vector<T>(arr.begin(), arr.end()), format);
124 }
125
136 template <typename T, size_t N>
137 size_t println(const std::array<T, N> &arr, int format = defaultFormat<T>()) {
138 // Call print() to print the array elements
139 size_t chars = print(arr, format);
140 // Print a newline sequence
141 chars += print("\r\n");
142 return chars;
143 }
144
145 protected:
146
147 void setWriteError(int err = 0);
148
149private:
150
152 size_t printNumber(unsigned long, uint8_t);
153 size_t printFloat(double, uint8_t);
154
155
163template <typename T>
164std::string formatElement(const T &element, int format) {
165 std::ostringstream oss;
166
167 if constexpr (std::is_integral_v<T>) {
168 // Handle integral types with base conversion
169 switch (format) {
170 case RDL_DEC:
171 oss << element; // Decimal
172 break;
173 case RDL_HEX:
174 oss << std::hex << std::uppercase << element; // Hexadecimal
175 break;
176 case RDL_OCT:
177 oss << std::oct << element; // Octal
178 break;
179 case RDL_BIN: {
180 // Binary conversion using std::bitset without leading zeros
181 std::string binary = std::bitset<sizeof(T) * 8>(element).to_string();
182 binary.erase(0, binary.find_first_not_of('0')); // Strip leading zeros
183 oss << (binary.empty() ? "0" : binary); // Handle the case for 0
184 break;
185 }
186 default:
187 oss << element; // Default to decimal
188 }
189 } else if constexpr (std::is_floating_point_v<T>) {
190 // Handle floating-point types with fixed precision
191 oss << std::fixed << std::setprecision(format) << element;
192 } else {
193 // Handle other types (fallback)
194 oss << element;
195 }
196
197 return oss.str();
198}
199
205 template <typename T>
206 static constexpr int defaultFormat() {
207 if constexpr (std::is_floating_point_v<T>) {
208 return 2; // Default decimal places for floats/doubles
209 } else if constexpr (std::is_integral_v<T>) {
210 return RDL_DEC; // Default base for integers
211 }
212 return 0; // Fallback for unsupported types
213 }
214
215
216};
Polymorphic print class to print many data types by wrapping write function in the graphics class's.
Definition print_data_RDL.hpp:29
size_t print(const std::vector< T > &v, int format=defaultFormat< T >())
Print a vector of any type.
Definition print_data_RDL.hpp:81
size_t println(void)
Goto to new line.
Definition print_data_RDL.cpp:185
void setWriteError(int err=0)
Definition print_data_RDL.cpp:57
size_t print(const std::array< T, N > &arr, int format=defaultFormat< T >())
Print a std::array of any type.
Definition print_data_RDL.hpp:121
size_t printFloat(double, uint8_t)
Used internally to parse float.
Definition print_data_RDL.cpp:327
virtual int availableForWrite()
define in the sub class
Definition print_data_RDL.cpp:44
virtual size_t write(uint8_t)=0
define in the sub class
size_t println(const std::array< T, N > &arr, int format=defaultFormat< T >())
Print a std::array of elements followed by a newline.
Definition print_data_RDL.hpp:137
BaseNum
Definition print_data_RDL.hpp:34
@ RDL_DEC
Definition print_data_RDL.hpp:35
@ RDL_BIN
Definition print_data_RDL.hpp:38
@ RDL_OCT
Definition print_data_RDL.hpp:37
@ RDL_HEX
Definition print_data_RDL.hpp:36
int getWriteError()
gets the error flag status, zero no error
Definition print_data_RDL.cpp:50
size_t println(const std::vector< T > &vec, int format=defaultFormat< T >())
Print a vector of elements followed by a newline.
Definition print_data_RDL.hpp:103
std::string formatElement(const T &element, int format)
Called from vector template Format an element based on its type and the format parameter.
Definition print_data_RDL.hpp:164
static constexpr int defaultFormat()
Called from vector & string template , Get the default format for a given type.
Definition print_data_RDL.hpp:206
size_t printNumber(unsigned long, uint8_t)
Used internally to parse and print number.
Definition print_data_RDL.cpp:301
size_t print(const char[])
print an array
Definition print_data_RDL.cpp:82
void clearWriteError()
clears the error flag by setting it to zero
Definition print_data_RDL.cpp:53
int _ErrorFlag
Definition print_data_RDL.hpp:151