3-phase PV router
Loading...
Searching...
No Matches
serial_output.h
Go to the documentation of this file.
1
24
25#ifndef SERIAL_OUTPUT_H
26#define SERIAL_OUTPUT_H
27
28#include <Arduino.h>
29
30namespace SerialOutput
31{
32inline constexpr uint8_t MAX_STEP_LENGTH{ 32 };
33
41using Step = bool (*)(Print& out, uint8_t step);
42
43inline Step current{ nullptr };
44inline uint8_t nextStep{ 0 };
45
49[[nodiscard]] inline bool busy()
50{
51 return current != nullptr;
52}
53
60inline bool start(Step step)
61{
62 if (busy())
63 {
64 return false;
65 }
66 current = step;
67 nextStep = 0;
68 return true;
69}
70
74inline void runStep(Print& out)
75{
76 if (!current(out, nextStep++))
77 {
78 current = nullptr;
79 }
80}
81
85inline void poll(Print& out = Serial)
86{
87 while (current && out.availableForWrite() >= MAX_STEP_LENGTH)
88 {
89 runStep(out);
90 }
91}
92
99__attribute__((noinline)) inline void complete()
100{
101 while (current)
102 {
103 runStep(Serial);
104 }
105}
106
117inline void printHundredths(Print& out, int16_t value_x100)
118{
119 const uint16_t magnitude{ value_x100 < 0 ? static_cast< uint16_t >(0U - static_cast< uint16_t >(value_x100))
120 : static_cast< uint16_t >(value_x100) };
121 if (value_x100 < 0)
122 {
123 out.print('-');
124 }
125 out.print(magnitude / 100);
126
127 const uint8_t hundredths{ static_cast< uint8_t >(magnitude % 100) };
128 if (!hundredths)
129 {
130 return;
131 }
132 out.print('.');
133 out.print(hundredths / 10);
134 if (hundredths % 10)
135 {
136 out.print(hundredths % 10);
137 }
138}
139
151constexpr int32_t toDecimals(float value, uint8_t decimals)
152{
153 for (uint8_t i = 0; i != decimals; ++i)
154 {
155 value *= 10;
156 }
157 return static_cast< int32_t >(value < 0 ? value - 0.5F : value + 0.5F);
158}
159
163template< uint8_t N >
165{
166 int32_t value[N];
167};
168
176template< uint8_t N >
177constexpr DecimalsTable< N > toDecimals(const float (&values)[N], uint8_t decimals)
178{
179 DecimalsTable< N > table{};
180 for (uint8_t i = 0; i != N; ++i)
181 {
182 table.value[i] = toDecimals(values[i], decimals);
183 }
184 return table;
185}
186
197__attribute__((noinline)) inline void printDecimals(Print& out, int32_t value, uint8_t decimals)
198{
199 const uint32_t magnitude{ value < 0 ? 0U - static_cast< uint32_t >(value) : static_cast< uint32_t >(value) };
200 if (value < 0)
201 {
202 out.print('-');
203 }
204
205 uint32_t divisor{ 1 };
206 for (uint8_t i = 0; i != decimals; ++i)
207 {
208 divisor *= 10;
209 }
210 out.print(magnitude / divisor);
211
212 if (!decimals)
213 {
214 return;
215 }
216 out.print('.');
217 while (divisor /= 10)
218 {
219 out.print(static_cast< char >('0' + (magnitude / divisor) % 10));
220 }
221}
222
224inline constexpr uint32_t BAUD_RATE{ 9600 };
225
229inline void beginStartupText()
230{
231 Serial.begin(BAUD_RATE, SERIAL_8N1);
232}
233
240template< bool IOT >
241inline void endStartupText()
242{
243 if constexpr (IOT)
244 {
245 Serial.flush(); // the startup text has left, at 8N1
246 Serial.begin(BAUD_RATE, SERIAL_7E1);
247 }
248}
249} // namespace SerialOutput
250
251#endif // SERIAL_OUTPUT_H
uint8_t i
bool(*)(Print &out, uint8_t step) Step
One step of an output.
void printHundredths(Print &out, int16_t value_x100)
Print a value given in hundredths, in its shortest decimal form.
void runStep(Print &out)
Run the next step of the current output.
void complete()
Write whatever is left of the current output to Serial, waiting like a plain print.
constexpr int32_t toDecimals(float value, uint8_t decimals)
A float constant as an integer with the given number of decimals, rounded.
void beginStartupText()
Serial settings for the startup text: 9600 baud 8N1, the default of serial monitors.
constexpr uint8_t MAX_STEP_LENGTH
constexpr uint32_t BAUD_RATE
bool start(Step step)
Start an output, written by later calls to poll()
void endStartupText()
Serial settings after the startup text: 7E1 for the IoT telemetry frames, as the mk2Wifi module expec...
uint8_t nextStep
bool busy()
Whether an output is still in progress.
void poll(Print &out=Serial)
Write as many steps as the transmit buffer can take, without waiting.
void printDecimals(Print &out, int32_t value, uint8_t decimals)
Print a value given as an integer with a fixed number of decimals (see toDecimals())
static Values values
Definition test_main.cpp:41
The values of a float array, as integers with the given number of decimals.