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
12
13#include <Arduino.h>
14#include <unity.h>
15
16#include "../halt_after_tests.h"
17
18#include "serial_output.h"
19#include "teleinfo.h"
20
21void setUp(void)
22{
23 // Setup for each test
24}
25
26void tearDown(void)
27{
28 // Clean up after each test
29}
30
31/* one line as the router writes it: LF tag TAB value TAB checksum CR */
32static void appendLine(char* expected, const char* tagAndValue)
33{
34 uint8_t sum{ 0 };
35 for (const char* p = tagAndValue; *p; ++p)
36 {
37 sum += *p;
38 }
39 sum += '\t';
40 const char line[]{ '\n', '\0' };
41 strcat(expected, line);
42 strcat(expected, tagAndValue);
43 const char end[]{ '\t', static_cast< char >((sum & 0x3F) + 0x20), '\r', '\0' };
44 strcat(expected, end);
45}
46
47/* the expected text of a single line */
48static const char* expectedLine(const char* tagAndValue)
49{
50 static char expected[TeleInfo::MAX_STEP_LENGTH + 1];
51 expected[0] = '\0';
52 appendLine(expected, tagAndValue);
53 return expected;
54}
55
57{
58 // LF + tag + TAB + value + TAB + checksum + CR = 5 + tagLength + valueLength
59 TEST_ASSERT_EQUAL(5 + 1 + 3, TeleInfo::lineSize(1, 3));
60 TEST_ASSERT_EQUAL(5 + 2 + 5, TeleInfo::lineSize(2, 5));
61 TEST_ASSERT_EQUAL(5 + 4 + 2, TeleInfo::lineSize(4, 2));
62
63 // a step fits the serial output's (SerialOutput::MAX_STEP_LENGTH)
65}
66
67/* the values that used to be cut or wrong: a 5-character temperature, a voltage and a sample
68 * count above 32767, an indexed relay state, the extremes of 16 bits */
70{
71 char text[TeleInfo::MAX_STEP_LENGTH + 1];
72
73 *TeleInfo::line(text, "T", -5500, 1) = '\0';
74 TEST_ASSERT_EQUAL_STRING(expectedLine("T1\t-5500"), text);
75
76 *TeleInfo::lineUnsigned(text, "S", 64000) = '\0';
77 TEST_ASSERT_EQUAL_STRING(expectedLine("S\t64000"), text);
78
79 *TeleInfo::lineUnsigned(text, "V", 46000, 2) = '\0';
80 TEST_ASSERT_EQUAL_STRING(expectedLine("V2\t46000"), text);
81
82 *TeleInfo::line(text, "R", 1, 2) = '\0';
83 TEST_ASSERT_EQUAL_STRING(expectedLine("R2\t1"), text);
84
85 *TeleInfo::line(text, "P", -32768) = '\0';
86 TEST_ASSERT_EQUAL_STRING(expectedLine("P\t-32768"), text);
87
88 *TeleInfo::line(text, "S_MC", 0) = '\0';
89 TEST_ASSERT_EQUAL_STRING(expectedLine("S_MC\t0"), text);
90}
91
93template< uint8_t PHASES >
95{
96 static constexpr uint8_t PHASE_COUNT{ PHASES };
97 static constexpr bool RELAY_DIVERSION{ true };
98 static constexpr uint8_t RELAY_COUNT{ 1 };
99 static constexpr uint8_t LOAD_COUNT{ 2 };
100 static constexpr uint8_t TEMP_COUNT{ 2 };
101 static constexpr bool DUAL_TARIFF{ true };
102
103 static int16_t power()
104 {
105 return -1234;
106 }
107 static int16_t phasePower(uint8_t idx)
108 {
109 return static_cast< int16_t >(400 + idx);
110 }
111 static uint16_t phaseVoltage(uint8_t idx)
112 {
113 return static_cast< uint16_t >(46000 + idx); // above 32767: unsigned
114 }
115 static int16_t relayAverage()
116 {
117 return -567;
118 }
119 static bool relayOn(uint8_t)
120 {
121 return true;
122 }
123 static int16_t loadOnPercent(uint8_t idx)
124 {
125 return idx == 0 ? 100 : 37;
126 }
127 static bool temperatureValid(uint8_t idx)
128 {
129 return idx == 0;
130 }
131 static int16_t temperature(uint8_t)
132 {
133 return 2137;
134 }
135 static uint16_t absenceCount()
136 {
137 return 40000;
138 }
139 static bool offPeak()
140 {
141 return true;
142 }
143 static uint16_t sampleSets()
144 {
145 return 64000;
146 }
148 {
149 return 44;
150 }
151};
152
153/* captures a frame, written step by step as the serial output does */
154class FramePrint : public Print
155{
156public:
157 char text[240]{};
158 size_t length{ 0 };
159
160 using Print::write;
161
162 size_t write(uint8_t c) override
163 {
164 if (length < sizeof text - 1)
165 {
166 text[length++] = static_cast< char >(c);
167 }
168 return 1;
169 }
170};
171
172template< typename Source >
173static void streamFrame(FramePrint& out)
174{
175 uint8_t step{ 0 };
176 bool more{ true };
177 while (more)
178 {
179 char text[TeleInfo::MAX_STEP_LENGTH];
180 uint8_t length{ 0 };
181 more = TeleInfo::writeFrameStep< Source >(step++, text, length);
182 TEST_ASSERT_LESS_OR_EQUAL(TeleInfo::MAX_STEP_LENGTH, length);
183 out.write(text, length);
184 }
185}
186
187/* the frame of a 3-phase router: P, then P and V of each phase, the last phase first */
189{
190 FramePrint out;
192
193 char expected[240]{ '\x02' };
194 appendLine(expected, "P\t-1234");
195 appendLine(expected, "P3\t402");
196 appendLine(expected, "V3\t46002");
197 appendLine(expected, "P2\t401");
198 appendLine(expected, "V2\t46001");
199 appendLine(expected, "P1\t400");
200 appendLine(expected, "V1\t46000");
201 appendLine(expected, "R\t-567");
202 appendLine(expected, "R1\t1");
203 appendLine(expected, "D2\t37");
204 appendLine(expected, "D1\t100");
205 appendLine(expected, "T1\t2137");
206 appendLine(expected, "N\t40000");
207 appendLine(expected, "TA\t1");
208 appendLine(expected, "S\t64000");
209 appendLine(expected, "S_MC\t44");
210 const char etx[]{ '\x03', '\0' };
211 strcat(expected, etx);
212
213 TEST_ASSERT_EQUAL_STRING(expected, out.text);
214}
215
216/* the frame of a single-phase setup: no per-phase lines */
218{
219 FramePrint out;
221
222 char expected[240]{ '\x02' };
223 appendLine(expected, "P\t-1234");
224 appendLine(expected, "R\t-567");
225 appendLine(expected, "R1\t1");
226 appendLine(expected, "D2\t37");
227 appendLine(expected, "D1\t100");
228 appendLine(expected, "T1\t2137");
229 appendLine(expected, "N\t40000");
230 appendLine(expected, "TA\t1");
231 appendLine(expected, "S\t64000");
232 appendLine(expected, "S_MC\t44");
233 const char etx[]{ '\x03', '\0' };
234 strcat(expected, etx);
235
236 TEST_ASSERT_EQUAL_STRING(expected, out.text);
237}
238
239void setup()
240{
241 delay(2000); // Give time for Serial to initialize
242
243 Serial.begin(9600);
244 Serial.println("Starting TeleInfo embedded tests...");
245
246 UNITY_BEGIN();
247}
248
249void loop()
250{
253 RUN_TEST(test_frame_three_phases);
254 RUN_TEST(test_frame_single_phase);
255
256 Serial.println("All TeleInfo tests completed!");
257
258 UNITY_END();
260}
size_t length
char text[240]
size_t write(uint8_t c) override
void setup()
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
void loop()
void test_lineSize_calculation(void)
Definition test_main.cpp:56
void test_frame_three_phases(void)
static void appendLine(char *expected, const char *tagAndValue)
Definition test_main.cpp:32
static void streamFrame(FramePrint &out)
static const char * expectedLine(const char *tagAndValue)
Definition test_main.cpp:48
void test_frame_single_phase(void)
void test_teleinfo_line_contents(void)
Definition test_main.cpp:69
Stops the MCU once an embedded test suite has run.
void haltAfterTests()
constexpr uint8_t MAX_STEP_LENGTH
char * lineUnsigned(char *dst, const char *tag, uint16_t value, uint8_t index=0)
Writes the line of an unsigned value (above 32767, line() would wrap).
Definition teleinfo.h:157
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
constexpr uint8_t lineSize(uint8_t tagLength, uint8_t valueLength)
The size of a line: LF, tag, TAB, value, TAB, checksum and CR.
Definition teleinfo.h:51
char * line(char *dst, const char *tag, int16_t value, uint8_t index=0)
Writes the line of a signed value.
Definition teleinfo.h:142
static Streamed streamFrame()
Non-blocking serial output of the datalog.
A data source of writeFrameStep(): 2 loads, 1 relay, 2 temperature sensors (the 2nd invalid).
Definition test_main.cpp:95
static uint8_t lowestSampleSetsPerCycle()
static constexpr uint8_t TEMP_COUNT
static int16_t phasePower(uint8_t idx)
static int16_t temperature(uint8_t)
static bool offPeak()
static constexpr uint8_t PHASE_COUNT
Definition test_main.cpp:96
static uint16_t sampleSets()
static int16_t relayAverage()
static bool temperatureValid(uint8_t idx)
static bool relayOn(uint8_t)
static constexpr bool RELAY_DIVERSION
Definition test_main.cpp:97
static uint16_t absenceCount()
static int16_t loadOnPercent(uint8_t idx)
static int16_t power()
static constexpr uint8_t LOAD_COUNT
Definition test_main.cpp:99
static constexpr bool DUAL_TARIFF
static constexpr uint8_t RELAY_COUNT
Definition test_main.cpp:98
static uint16_t phaseVoltage(uint8_t idx)
The telemetry frame of the IoT output, written a line at a time.