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 <unity.h>
14
15#include <stdint.h>
16
17#include "temp_schedule.h" // Real header - pure C++
18
21
22void setUp(void) {}
23void tearDown(void) {}
24
25namespace
26{
27constexpr uint16_t C50{ TempSchedule::conversionCycles(50) };
28constexpr uint16_t C60{ TempSchedule::conversionCycles(60) };
29
31Action readStep(uint16_t k)
32{
33 constexpr Step kinds[]{ Step::ADDRESS, Step::READ, Step::CLOSE };
34 return Action{ kinds[k % 3], static_cast< uint8_t >(k / 3) };
35}
36
37void assertAction(const Action expected, const Action actual, const char* msg)
38{
39 TEST_ASSERT_EQUAL_UINT8_MESSAGE(static_cast< uint8_t >(expected.step), static_cast< uint8_t >(actual.step), msg);
40 if (expected.step != Step::NONE)
41 {
42 TEST_ASSERT_EQUAL_UINT8_MESSAGE(expected.sensor, actual.sensor, msg);
43 }
44}
45} // namespace
46
47// ============================================================================
48// Conversion time
49// ============================================================================
50
52{
53 // 750 ms rounded up to whole cycles, plus one: the loop may start a transaction up to a little
54 // after its cycle began
55 TEST_ASSERT_EQUAL_UINT16(39, C50); // 780 ms
56 TEST_ASSERT_EQUAL_UINT16(46, C60); // 767 ms
57 TEST_ASSERT_TRUE(C50 * 1000UL / 50 >= 760);
58 TEST_ASSERT_TRUE(C60 * 1000UL / 60 >= 760);
59}
60
61// ============================================================================
62// The round
63// ============================================================================
64
66{
68
69 assertAction({ Step::CONVERT, 0 }, schedule.next(true), "first cycle: the conversion");
70 for (uint16_t i = 1; i != C50; ++i)
71 {
72 assertAction({ Step::NONE, 0 }, schedule.next(true), "during the conversion: nothing");
73 }
74 for (uint16_t k = 0; k != 6; ++k)
75 {
76 assertAction(readStep(k), schedule.next(true), "the reads, one transaction per cycle");
77 }
78 TEST_ASSERT_TRUE(schedule.done());
79 for (uint16_t i = 0; i != 300; ++i)
80 {
81 assertAction({ Step::NONE, 0 }, schedule.next(true), "round over: nothing until the next period");
82 }
83}
84
86{
88 uint16_t cycle{ 0 }, convertAt{ 0 }, addressAt{ 0 };
89 while (!schedule.done() && cycle < 1000)
90 {
91 const Action a{ schedule.next(true) };
92 if (a.step == Step::CONVERT)
93 {
94 convertAt = cycle;
95 }
96 if (a.step == Step::ADDRESS)
97 {
98 addressAt = cycle;
99 }
100 ++cycle;
101 }
102 TEST_ASSERT_EQUAL_UINT16(C60, addressAt - convertAt);
103}
104
106{
108
109 assertAction({ Step::NONE, 0 }, schedule.next(false), "late: no conversion");
110 assertAction({ Step::CONVERT, 0 }, schedule.next(true), "on time: the conversion");
111 for (uint16_t i = 1; i != C50; ++i)
112 {
113 schedule.next(true);
114 }
115 assertAction({ Step::NONE, 0 }, schedule.next(false), "late: the read waits");
116 assertAction({ Step::ADDRESS, 0 }, schedule.next(true), "");
117 assertAction({ Step::NONE, 0 }, schedule.next(false), "late: the read waits");
118 assertAction({ Step::READ, 0 }, schedule.next(true), "");
119 assertAction({ Step::CLOSE, 0 }, schedule.next(true), "");
120 TEST_ASSERT_TRUE(schedule.done());
121}
122
124{
125 // the conversion runs on the sensors' side whatever the loop does
127 schedule.next(true);
128 for (uint16_t i = 1; i != C50; ++i)
129 {
130 schedule.next(false);
131 }
132 assertAction({ Step::ADDRESS, 0 }, schedule.next(true), "conversion time over");
133}
134
136{
138 schedule.next(true);
139 for (uint16_t i = 1; i != C50; ++i)
140 {
141 schedule.next(true);
142 }
143 assertAction({ Step::ADDRESS, 0 }, schedule.next(true), "");
144 schedule.skipRestOf(0); // no presence pulse
145 assertAction({ Step::ADDRESS, 1 }, schedule.next(true), "the next sensor, on the next cycle");
146 assertAction({ Step::READ, 1 }, schedule.next(true), "");
147 schedule.skipRestOf(0); // another sensor's: ignored
148 assertAction({ Step::CLOSE, 1 }, schedule.next(true), "");
149 assertAction({ Step::ADDRESS, 2 }, schedule.next(true), "");
150 schedule.skipRestOf(2);
151 TEST_ASSERT_TRUE(schedule.done());
152 assertAction({ Step::NONE, 0 }, schedule.next(true), "the last sensor skipped: round over");
153}
154
156{
158 for (uint16_t i = 0; i != C50 + 3; ++i)
159 {
160 schedule.next(true);
161 }
162 TEST_ASSERT_TRUE(schedule.done());
163 TEST_ASSERT_TRUE(schedule.newPeriod()); // restarted: the loop may convert at once
164 TEST_ASSERT_FALSE(schedule.done());
165 assertAction({ Step::CONVERT, 0 }, schedule.next(true), "a new round");
166}
167
169{
170 // The loop starts the next round as soon as it handles the end of the period, in the same mains
171 // cycle, if it is on time: the round's cycle had no transaction (round over), so that cycle
172 // still has one at most, and the bus is quiet again before the datalog output starts.
174 for (uint16_t i = 0; i != C50 + 3; ++i)
175 {
176 schedule.next(true);
177 }
178 assertAction({ Step::NONE, 0 }, schedule.next(true), "the cycle that ends the period: nothing");
179 TEST_ASSERT_TRUE(schedule.newPeriod());
180 assertAction({ Step::CONVERT, 0 }, schedule.next(true), "the conversion, in that same cycle");
181 for (uint16_t i = 1; i != C50; ++i)
182 {
183 assertAction({ Step::NONE, 0 }, schedule.next(true), "during the conversion: nothing");
184 }
185 assertAction({ Step::ADDRESS, 0 }, schedule.next(true), "");
186}
187
189{
190 // a round longer than the period (only with many late cycles): the values of the sensors not
191 // read yet are those of the period before; the conversion is never cut short
193 for (uint16_t i = 0; i != C50 + 1; ++i)
194 {
195 schedule.next(true); // the conversion, the wait, ADDRESS of sensor 0
196 }
197 TEST_ASSERT_FALSE(schedule.newPeriod()); // not restarted: no extra transaction in this cycle
198 assertAction({ Step::READ, 0 }, schedule.next(true), "the round goes on");
199}
200
202{
203 // a pseudo-random pattern of late cycles, over many periods
205 uint32_t lfsr{ 0xACE1U };
206 int32_t lastConvert{ -100000 };
207 uint16_t rounds{ 0 };
208 for (int32_t cycle = 0; cycle != 20000; ++cycle)
209 {
210 lfsr = (lfsr >> 1) ^ (-(lfsr & 1U) & 0xB400U);
211 const bool onTime{ (lfsr & 7U) != 0 }; // 1 cycle in 8 late
212 if (cycle % 250 == 0)
213 {
214 schedule.newPeriod();
215 }
216 const Action a{ schedule.next(onTime) }; // one call per cycle: one transaction at most
217 if (a.step != Step::NONE)
218 {
219 TEST_ASSERT_TRUE_MESSAGE(onTime, "a transaction on a late cycle");
220 }
221 if (a.step == Step::CONVERT)
222 {
223 lastConvert = cycle;
224 ++rounds;
225 }
226 if (a.step == Step::ADDRESS || a.step == Step::READ)
227 {
228 TEST_ASSERT_TRUE_MESSAGE(cycle - lastConvert >= C50, "a read before the end of the conversion");
229 }
230 }
231 TEST_ASSERT_EQUAL_UINT16(80, rounds); // one round per period
232}
233
234// ============================================================================
235// Fitting the period
236// ============================================================================
237
245template< uint8_t N, uint8_t F >
246static bool roundEndsInPeriod(const uint16_t periodCycles)
247{
248 constexpr uint16_t C{ TempSchedule::conversionCycles(F) };
250 schedule.newPeriod();
251 for (uint16_t tick = 1; tick != periodCycles; ++tick) // tick periodCycles: the period-end cycle
252 {
253 const bool onTime{ (tick % F) != 1 };
254 schedule.next(onTime);
255 if (schedule.done())
256 {
257 return true;
258 }
259 }
260 return false;
261}
262
264{
265 // 5 s, the default
266 static_assert(TempSchedule::fitsInPeriod(2, 50, 250), "");
267 static_assert(TempSchedule::fitsInPeriod(8, 60, 300), "");
268 TEST_ASSERT_TRUE((roundEndsInPeriod< 2, 50 >(250)));
269 TEST_ASSERT_TRUE((roundEndsInPeriod< 8, 60 >(300)));
270 // 1 s: the conversion takes most of it
271 static_assert(TempSchedule::fitsInPeriod(3, 50, 50), "");
272 static_assert(!TempSchedule::fitsInPeriod(4, 50, 50), "");
273 static_assert(TempSchedule::fitsInPeriod(4, 60, 60), "");
274 static_assert(!TempSchedule::fitsInPeriod(5, 60, 60), "");
275 TEST_ASSERT_TRUE((roundEndsInPeriod< 3, 50 >(50)));
276 TEST_ASSERT_TRUE((roundEndsInPeriod< 4, 60 >(60)));
277 // what fitsInPeriod() refuses does not fit
278 TEST_ASSERT_FALSE((roundEndsInPeriod< 5, 50 >(50)));
279 // 2 s: the late cycle within the round counts
280 static_assert(TempSchedule::fitsInPeriod(19, 50, 100), "");
281 static_assert(!TempSchedule::fitsInPeriod(20, 50, 100), "");
282 TEST_ASSERT_TRUE((roundEndsInPeriod< 19, 50 >(100)));
283 TEST_ASSERT_FALSE((roundEndsInPeriod< 20, 50 >(100)));
284}
285
The round of the sensors, one transaction per mains cycle.
bool done() const
Whether the round is over.
bool newPeriod()
A datalog period begins: a new round, once this one is over.
Action next(const bool onTime)
The transaction of this mains cycle: called once per cycle, when the loop sees it.
void skipRestOf(const uint8_t idx)
The sensor did not answer its ADDRESS: its other transactions are skipped.
uint8_t i
constexpr uint16_t conversionCycles(const uint8_t supplyFrequency)
The mains cycles from the conversion to the first read.
Step
The 1-Wire transaction of a mains cycle.
constexpr bool fitsInPeriod(const uint8_t noOfSensors, const uint8_t supplyFrequency, const uint16_t periodCycles)
Whether a round ends within its datalog period.
int main(void)
static void cycle(RemoteLoadCore< NumUnits > &core, const LoadStates(&states)[NumLoads])
Run one mains cycle: feed the states to the core, for the given map.
Definition test_main.cpp:26
void test_new_period_continues_an_unfinished_round()
void test_never_two_transactions_in_one_cycle_and_never_before_the_conversion_time()
void test_first_read_conversion_cycles_after_the_conversion()
Definition test_main.cpp:85
void test_round_of_two_sensors()
Definition test_main.cpp:65
void test_conversion_in_the_cycle_that_ends_the_period()
void test_new_period_restarts_a_finished_round()
void test_conversion_cycles_cover_750_ms()
Definition test_main.cpp:51
void setUp(void)
Definition test_main.cpp:22
void tearDown(void)
Definition test_main.cpp:23
void test_late_cycles_still_count_for_the_conversion()
void test_rounds_fit_their_period()
void test_late_cycles_postpone_without_losing_a_step()
void test_a_sensor_that_does_not_answer_is_skipped()
static bool roundEndsInPeriod(const uint16_t periodCycles)
A datalog period of the main loop: the round must be over before the cycle that ends the period,...
The transaction to run, and the sensor it reads (ADDRESS, READ, CLOSE).
Which 1-Wire transaction of the temperature sensors runs on which mains cycle.