3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#include <Arduino.h>
3#include <ArduinoJson.h>
4#include <unity.h>
5
7
8#include "serial_output.h"
9#include "teleinfo.h"
10
14class CapturePrint : public Print
15{
16public:
17 char text[400]{};
18 size_t length{ 0 };
19 int room{ 63 };
20 bool overflow{ false };
21
22 size_t write(uint8_t c) override
23 {
24 if (room <= 0)
25 {
26 overflow = true;
27 }
28 --room;
29 if (length < sizeof text - 1)
30 {
31 text[length++] = static_cast< char >(c);
32 text[length] = '\0';
33 }
34 return 1;
35 }
36 int availableForWrite() override
37 {
38 return room;
39 }
40 void clear()
41 {
42 length = 0;
43 text[0] = '\0';
44 room = 63;
45 overflow = false;
46 }
47};
48
50
51/* an output of 10 fields and a line end */
52bool fieldsStep(Print& out, uint8_t step)
53{
54 if (step < 10)
55 {
56 out.print(F("field"));
57 out.print(step);
58 out.print(F(" = some value; "));
59 return true;
60 }
61 out.println(F("end"));
62 return false;
63}
64
65void setUp(void)
66{
67 capture.clear();
68 SerialOutput::current = nullptr;
69}
70
71void tearDown(void)
72{
73}
74
76{
77 TEST_ASSERT_TRUE(SerialOutput::start(fieldsStep));
78
81
82 TEST_ASSERT_EQUAL(0, capture.length);
83 TEST_ASSERT_TRUE(SerialOutput::busy());
84}
85
87{
88 TEST_ASSERT_TRUE(SerialOutput::start(fieldsStep));
89
90 uint8_t polls{ 0 };
91 while (SerialOutput::busy() && polls < 50)
92 {
94 capture.room = 63; // the UART sent everything meanwhile
95 ++polls;
96 }
97
98 TEST_ASSERT_FALSE(SerialOutput::busy());
99 TEST_ASSERT_FALSE(capture.overflow);
100 TEST_ASSERT_GREATER_THAN(1, polls); // it did take several passes
101 TEST_ASSERT_EQUAL_STRING("field0 = some value; field1 = some value; field2 = some value; field3 = some value; "
102 "field4 = some value; field5 = some value; field6 = some value; field7 = some value; "
103 "field8 = some value; field9 = some value; end\r\n",
104 capture.text);
105}
106
108{
109 TEST_ASSERT_TRUE(SerialOutput::start(fieldsStep));
110 TEST_ASSERT_FALSE(SerialOutput::start(fieldsStep));
111}
112
113/* the JSON format used to be written by ArduinoJson: the same numbers, in the shortest
114 * exact form (ArduinoJson showed float noise for about 2 % of them, -9.969999 for -9.97) */
116{
117 uint16_t mismatches{ 0 };
118 uint16_t noise{ 0 };
119 char message[80]{};
120
121 for (int32_t value = -5500; value <= 12500; ++value)
122 {
123 char json[24];
124 StaticJsonDocument< 16 > doc;
125 doc.set(static_cast< float >(value) * 0.01F);
126 serializeJson(doc, json, sizeof json);
127
128 capture.clear();
129 SerialOutput::printHundredths(capture, static_cast< int16_t >(value));
130
131 const char* point{ strchr(capture.text, '.') };
132 const size_t decimals{ point ? strlen(point + 1) : 0 };
133 const bool shortest{ decimals <= 2 && (!point || capture.text[capture.length - 1] != '0') };
134 const bool sameValue{ fabs(atof(json) - atof(capture.text)) <= 1e-5 * (1 + fabs(atof(json))) };
135
136 noise += strcmp(json, capture.text) != 0;
137 if (!(shortest && sameValue) && !mismatches++)
138 {
139 snprintf(message, sizeof message, "%ld: ArduinoJson '%s', printHundredths '%s'", static_cast< long >(value), json,
140 capture.text);
141 }
142 }
143 TEST_ASSERT_EQUAL_MESSAGE(0, mismatches, message);
144 TEST_ASSERT_LESS_THAN(1000, noise); // otherwise the two formats really differ
145}
146
148{
150 TEST_ASSERT_EQUAL_STRING("-327.68", capture.text);
151
152 capture.clear();
154 TEST_ASSERT_EQUAL_STRING("327.67", capture.text);
155
156 capture.clear();
158 TEST_ASSERT_EQUAL_STRING("-0.05", capture.text);
159}
160
161/* the datalog used to print voltages and temperatures with print(value_x100 * 0.01F): the same text */
163{
164 uint16_t mismatches{ 0 };
165 char message[80]{};
166 char reference[16]{}; // not a second CapturePrint: 2 kB of RAM
167
168 for (int32_t value = -5500; value <= 12500; ++value)
169 {
170 capture.clear();
171 capture.print(static_cast< float >(value) * 0.01F);
172 strlcpy(reference, capture.text, sizeof reference);
173
174 capture.clear();
176
177 if (strcmp(reference, capture.text) && !mismatches++)
178 {
179 snprintf_P(message, sizeof message, PSTR("%ld: print(float) '%s', printDecimals '%s'"), static_cast< long >(value),
180 reference, capture.text);
181 }
182 }
183 TEST_ASSERT_EQUAL_MESSAGE(0, mismatches, message);
184}
185
186/* the captured text, compared with a string kept in flash: the test is short of RAM */
187void assertCaptured(PGM_P expected)
188{
189 char text[16];
190 strlcpy_P(text, expected, sizeof text);
191 TEST_ASSERT_EQUAL_STRING(text, capture.text);
192}
193
195{
197 assertCaptured(PSTR("0.050000"));
198
199 capture.clear();
201 assertCaptured(PSTR("0.81510"));
202
203 capture.clear();
205 assertCaptured(PSTR("-0.10"));
206
207 capture.clear();
209 assertCaptured(PSTR("42"));
210
211 capture.clear();
213 assertCaptured(PSTR("-21474836.48"));
214}
215
216/* the calibration constants of the startup summary, converted at compile time */
218{
219 static_assert(SerialOutput::toDecimals(0.05F, 6) == 50000, "f_powerCal");
220 static_assert(SerialOutput::toDecimals(0.8151F, 5) == 81510, "f_voltageCal");
221 static_assert(SerialOutput::toDecimals(1.0F, 2) == 100, "f_phaseCal");
222 static_assert(SerialOutput::toDecimals(-0.1F, 2) == -10, "rounded away from zero");
223
224 constexpr float values[]{ 0.05F, 0.0612F };
225 constexpr auto table{ SerialOutput::toDecimals(values, 6) };
226 static_assert(table.value[0] == 50000 && table.value[1] == 61200, "array version");
227
228 TEST_PASS();
229}
230
233{
234 static constexpr uint8_t PHASE_COUNT{ 3 };
235 static constexpr bool RELAY_DIVERSION{ false };
236 static constexpr uint8_t RELAY_COUNT{ 0 };
237 static constexpr uint8_t LOAD_COUNT{ 1 };
238 static constexpr uint8_t TEMP_COUNT{ 1 };
239 static constexpr bool DUAL_TARIFF{ false };
240
241 static int16_t power()
242 {
243 return -1234;
244 }
245 static int16_t phasePower(uint8_t)
246 {
247 return 399;
248 }
249 static uint16_t phaseVoltage(uint8_t)
250 {
251 return 23012;
252 }
253 static int16_t relayAverage()
254 {
255 return 0;
256 }
257 static bool relayOn(uint8_t)
258 {
259 return false;
260 }
261 static int16_t loadOnPercent(uint8_t)
262 {
263 return 37;
264 }
265 static bool temperatureValid(uint8_t)
266 {
267 return true;
268 }
269 static int16_t temperature(uint8_t)
270 {
271 return 2137;
272 }
273 static uint16_t absenceCount()
274 {
275 return 7;
276 }
277 static bool offPeak()
278 {
279 return false;
280 }
281 static uint16_t sampleSets()
282 {
283 return 64000;
284 }
286 {
287 return 44;
288 }
289};
290
291/* one step of the IoT frame, as writeTeleInfoStep() in utils.h */
292bool frameStep(Print& out, uint8_t step)
293{
294 char text[TeleInfo::MAX_STEP_LENGTH];
295 uint8_t length;
296 const bool more{ TeleInfo::writeFrameStep< FrameSource >(step, text, length) };
297 out.write(text, length);
298 return more;
299}
300
301/* the IoT frame, a line per step through poll(), never overfills the transmit buffer and is the
302 * same as written at once */
304{
305 static_assert(TeleInfo::MAX_STEP_LENGTH <= SerialOutput::MAX_STEP_LENGTH, "a line fits a step");
306
307 char whole[300];
308 capture.room = 1000;
309 uint8_t step{ 0 };
310 while (frameStep(capture, step++))
311 {
312 }
313 TEST_ASSERT_LESS_THAN(sizeof whole, capture.length);
314 memcpy(whole, capture.text, capture.length + 1);
315 TEST_ASSERT_EQUAL_CHAR(0x02, whole[0]);
316 TEST_ASSERT_EQUAL_CHAR(0x03, whole[capture.length - 1]);
317
318 capture.clear();
319 TEST_ASSERT_TRUE(SerialOutput::start(frameStep));
320 uint8_t polls{ 0 };
321 while (SerialOutput::busy() && polls < 50)
322 {
324 capture.room = 63; // the UART sent everything meanwhile
325 ++polls;
326 }
327
328 TEST_ASSERT_FALSE(SerialOutput::busy());
329 TEST_ASSERT_FALSE(capture.overflow);
330 TEST_ASSERT_GREATER_THAN(1, polls); // it did take several passes
331 TEST_ASSERT_EQUAL_STRING(whole, capture.text);
332}
333
338{
340 const uint8_t startup{ UCSR0C };
342 const uint8_t iot{ UCSR0C };
343
346 const uint8_t other{ UCSR0C };
347
348 Serial.begin(9600); // 8N1 again, for the test report (already, unless a check fails)
349
350 TEST_ASSERT_EQUAL_HEX8(SERIAL_8N1, startup);
351 TEST_ASSERT_EQUAL_HEX8(SERIAL_7E1, iot);
352 TEST_ASSERT_EQUAL_HEX8(SERIAL_8N1, other);
353}
354
355void setup()
356{
357 delay(1000);
358
359 UNITY_BEGIN();
360
361 RUN_TEST(test_poll_waits_for_room);
371
372 UNITY_END();
374}
375
376void loop()
377{
378}
A Print that captures the text, with a transmit buffer that fills up as it is written.
Definition test_main.cpp:15
int availableForWrite() override
Definition test_main.cpp:36
char text[400]
Definition test_main.cpp:17
size_t length
Definition test_main.cpp:18
size_t write(uint8_t c) override
Definition test_main.cpp:22
void setup()
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
void loop()
void test_start_while_busy_is_refused(void)
void test_printHundredths_extremes(void)
void test_poll_never_overfills_and_completes(void)
Definition test_main.cpp:86
void test_teleinfo_written_in_parts(void)
void test_printHundredths_matches_arduinojson(void)
void test_startup_text_serial_settings()
The startup text at 8N1, then 7E1 for the IoT output only (UART0 frame format in UCSR0C)
void test_toDecimals_at_compile_time(void)
void test_poll_waits_for_room(void)
Definition test_main.cpp:75
CapturePrint capture
Definition test_main.cpp:49
void test_printDecimals_matches_print_float(void)
void assertCaptured(PGM_P expected)
bool fieldsStep(Print &out, uint8_t step)
Definition test_main.cpp:52
void test_printDecimals_other_decimals_and_extremes(void)
bool frameStep(Print &out, uint8_t step)
Stops the MCU once an embedded test suite has run.
void haltAfterTests()
void printHundredths(Print &out, int16_t value_x100)
Print a value given in hundredths, in its shortest decimal form.
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
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...
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())
bool writeFrameStep(const uint8_t step, char *const text, uint8_t &length)
Writes a step of the frame: one line, STX before the first one, ETX after the last.
Definition teleinfo.h:304
constexpr uint8_t MAX_STEP_LENGTH
The most a step writes: STX or ETX, and the longest line, a 4-character tag ("S_MC") with a signed 16...
Definition teleinfo.h:60
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
Definition test_main.cpp:28
static Values values
Definition test_main.cpp:41
Non-blocking serial output of the datalog.
The data of a small IoT frame: P, P1, V1, P2, V2, P3, V3, D1, T1, N, S, S_MC (3 phases,...
static bool temperatureValid(uint8_t)
static int16_t temperature(uint8_t)
static constexpr uint8_t LOAD_COUNT
static uint16_t phaseVoltage(uint8_t)
static bool offPeak()
static uint8_t lowestSampleSetsPerCycle()
static constexpr uint8_t PHASE_COUNT
static int16_t phasePower(uint8_t)
static constexpr uint8_t TEMP_COUNT
static uint16_t sampleSets()
static uint16_t absenceCount()
static constexpr bool RELAY_DIVERSION
static int16_t relayAverage()
static int16_t loadOnPercent(uint8_t)
static bool relayOn(uint8_t)
static int16_t power()
static constexpr uint8_t RELAY_COUNT
static constexpr bool DUAL_TARIFF
The telemetry frame of the IoT output, written a line at a time.