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
10
11#include <unity.h>
12
13#include "load_map.h" // Real header - pure C++
14#include "remote_loads_core.h" // Real header - pure C++
15#include "utils_override.h" // Real header - pure C++
16
17void setUp(void) {}
18void tearDown(void) {}
19
20// ============================================================================
21// Helpers
22// ============================================================================
23
25template< const auto &Map, uint8_t NumUnits, uint8_t NumLoads >
26static void cycle(RemoteLoadCore< NumUnits > &core, const LoadStates (&states)[NumLoads])
27{
28 core.template updateLoads< Map >(states);
29}
30
32template< uint8_t NumUnits >
33static int drain(RemoteLoadCore< NumUnits > &core, uint8_t unitIdx)
34{
35 uint8_t payload{ 0 };
36 return core.takePending(unitIdx, payload) ? static_cast< int >(payload) : -1;
37}
38
40template< uint8_t NumLoads >
41static void setStates(LoadStates (&states)[NumLoads], uint8_t pattern)
42{
43 for (uint8_t i = 0; i < NumLoads; ++i)
44 {
45 states[i] = (pattern & (1U << i)) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF;
46 }
47}
48
49// ============================================================================
50// Tests for the packed encoding
51// ============================================================================
52
54{
55 TEST_ASSERT_TRUE(Load::isLocal(Load::local(5)));
56 TEST_ASSERT_EQUAL(0, Load::unitOf(Load::local(5)));
57 TEST_ASSERT_EQUAL(5, Load::pinOf(Load::local(5)));
58 TEST_ASSERT_EQUAL(13, Load::pinOf(Load::local(13)));
59}
60
62{
63 TEST_ASSERT_EQUAL_HEX8(0x40, Load::remote(1));
64 TEST_ASSERT_EQUAL_HEX8(0x80, Load::remote(2));
65 TEST_ASSERT_EQUAL_HEX8(0xC0, Load::remote(3));
66
67 TEST_ASSERT_FALSE(Load::isLocal(Load::remote(1)));
68 TEST_ASSERT_EQUAL(1, Load::unitOf(Load::remote(1)));
69 TEST_ASSERT_EQUAL(2, Load::unitOf(Load::remote(2)));
70 TEST_ASSERT_EQUAL(3, Load::unitOf(Load::remote(3)));
71}
72
74{
75 TEST_ASSERT_EQUAL(0, Load::pinOf(Load::remote(1)));
76 TEST_ASSERT_EQUAL(0, Load::pinOf(Load::remote(3)));
77}
78
80{
81 TEST_ASSERT_EQUAL(6, Load::pinOf(Load::remote(1, 6)));
82 TEST_ASSERT_EQUAL(2, Load::unitOf(Load::remote(2, 13)));
83 TEST_ASSERT_EQUAL(13, Load::pinOf(Load::remote(2, 13)));
84}
85
87{
88 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1, 6), Load::remote(2, 7), Load::remote(1) };
89
90 static_assert(Load::countUnits(map) == 2, "highest unit is 2");
91 static_assert(Load::countRemoteLoads(map) == 3, "three remote loads");
92 static_assert(Load::maxLoadsPerUnit(map) == 2, "unit 1 carries two loads");
93
94 TEST_ASSERT_EQUAL(2, Load::countUnits(map));
95 TEST_ASSERT_EQUAL(3, Load::countRemoteLoads(map));
96 TEST_ASSERT_EQUAL(2, Load::maxLoadsPerUnit(map));
97}
98
100{
101 static constexpr uint8_t map[]{ Load::local(5), Load::local(6), Load::local(7) };
102
103 static_assert(Load::countUnits(map) == 0, "no remote unit");
104 static_assert(Load::countRemoteLoads(map) == 0, "no remote load");
105 static_assert(Load::maxLoadsPerUnit(map) == 0, "no remote load");
106
107 TEST_ASSERT_EQUAL(0, Load::countUnits(map));
108}
109
111{
112 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::local(6), Load::remote(2), Load::remote(1) };
113
114 TEST_ASSERT_EQUAL(0, Load::remoteOrdinal(map, 1));
115 TEST_ASSERT_EQUAL(1, Load::remoteOrdinal(map, 3));
116 TEST_ASSERT_EQUAL(2, Load::remoteOrdinal(map, 4));
117}
118
120{
121 // the ISR tests the override of load i with these masks instead of shifting by the pin
122 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1, 6), Load::local(13), Load::remote(2), Load::local(2), Load::remote(1) };
123 static constexpr auto masks{ Load::overrideMasks(map) };
124
125 for (uint8_t i = 0; i < sizeof(map); ++i)
126 {
127 if (Load::isLocal(map[i]))
128 {
129 TEST_ASSERT_EQUAL_HEX16(1U << Load::pinOf(map[i]), masks.local[i]);
130 TEST_ASSERT_EQUAL_HEX8(0, masks.remote[i]);
131 }
132 else
133 {
134 TEST_ASSERT_EQUAL_HEX16(0, masks.local[i]);
135 TEST_ASSERT_EQUAL_HEX8(1U << Load::remoteOrdinal(map, i), masks.remote[i]);
136 }
137 }
138 TEST_ASSERT_EQUAL_HEX16(0x2000, masks.local[2]); // pin 13: the high byte, PORTB
139 TEST_ASSERT_EQUAL_HEX8(0x04, masks.remote[5]); // third remote load, though on unit 1
140}
141
143{
144 // rejected by validation.h; the mask builder itself must stay a valid constant expression
145 static constexpr uint8_t map[]{ Load::local(20), Load::remote(1), Load::remote(1), Load::remote(1), Load::remote(1),
147 static constexpr auto masks{ Load::overrideMasks(map) };
148
149 TEST_ASSERT_EQUAL_HEX16(0, masks.local[0]); // pin 20: beyond the 16-bit override bitmask
150 TEST_ASSERT_EQUAL_HEX8(0x80, masks.remote[8]); // eighth remote load: the last bit
151 TEST_ASSERT_EQUAL_HEX8(0, masks.remote[9]); // ninth: beyond the 8-bit remote bitmask
152}
153
155{
156 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(2), Load::remote(1), Load::remote(2), Load::remote(1) };
157
158 TEST_ASSERT_EQUAL(0, Load::bitOf(map, 0));
159 TEST_ASSERT_EQUAL(0, Load::bitOf(map, 1));
160 TEST_ASSERT_EQUAL(1, Load::bitOf(map, 2));
161 TEST_ASSERT_EQUAL(1, Load::bitOf(map, 3));
162 TEST_ASSERT_EQUAL(2, Load::bitOf(map, 4));
163}
164
165// ============================================================================
166// Tests for unit grouping
167// ============================================================================
168
170{
171 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(1), Load::remote(1) };
172 LoadStates states[3]{};
174
175 setStates(states, 0b101);
176 cycle< map >(core, states);
177
178 TEST_ASSERT_EQUAL_HEX8(0b101, core.payloadOf(0));
179}
180
182{
183 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::local(7) };
186
187 cycle< map >(core, states);
188
189 TEST_ASSERT_EQUAL_HEX8(0, core.payloadOf(0));
190}
191
193{
194 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(2), Load::remote(1), Load::remote(2) };
195 LoadStates states[4]{};
197
198 // load 0 (unit 1, bit 0) and load 3 (unit 2, bit 1) are ON
199 setStates(states, 0b1001);
200 cycle< map >(core, states);
201
202 TEST_ASSERT_EQUAL_HEX8(0b01, core.payloadOf(0));
203 TEST_ASSERT_EQUAL_HEX8(0b10, core.payloadOf(1));
204}
205
207{
208 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(2), Load::remote(3) };
209 LoadStates states[3]{};
211
212 setStates(states, 0b101);
213 cycle< map >(core, states);
214
215 TEST_ASSERT_EQUAL_HEX8(1, core.payloadOf(0));
216 TEST_ASSERT_EQUAL_HEX8(0, core.payloadOf(1));
217 TEST_ASSERT_EQUAL_HEX8(1, core.payloadOf(2));
218}
219
221{
222 // the map used as an example in the documentation
223 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1, 6), Load::remote(2, 7), Load::remote(1) };
224 LoadStates states[4]{};
226
227 // everything ON: unit 1 holds loads 1 and 3, unit 2 holds load 2
228 setStates(states, 0b1111);
229 cycle< map >(core, states);
230
231 TEST_ASSERT_EQUAL_HEX8(0b11, core.payloadOf(0));
232 TEST_ASSERT_EQUAL_HEX8(0b01, core.payloadOf(1));
233}
234
235// ============================================================================
236// Bit ordering - regression pin for the descending/ascending mismatch
237// ============================================================================
238
240{
241 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(1), Load::remote(1) };
242 LoadStates states[3]{};
244
245 setStates(states, 0b001);
246 cycle< map >(core, states);
247 TEST_ASSERT_EQUAL_HEX8(0b001, core.payloadOf(0));
248
249 setStates(states, 0b100);
250 cycle< map >(core, states);
251 TEST_ASSERT_EQUAL_HEX8(0b100, core.payloadOf(0));
252}
253
255{
256 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(1), Load::remote(1), Load::remote(1),
258 LoadStates states[8]{};
260
261 setStates(states, 0x80);
262 cycle< map >(core, states);
263 TEST_ASSERT_EQUAL_HEX8(0x80, core.payloadOf(0));
264
265 setStates(states, 0xFF);
266 cycle< map >(core, states);
267 TEST_ASSERT_EQUAL_HEX8(0xFF, core.payloadOf(0));
268}
269
271{
272 // Regression pin: the previous implementation filled a separate remote-state array
273 // ascending and consumed it descending, so this map produced 0b10 instead of 0b01.
274 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::remote(1) };
277
278 cycle< map >(core, states);
279
280 TEST_ASSERT_EQUAL_HEX8(0b01, core.payloadOf(0));
281}
282
283// ============================================================================
284// Change detection
285// ============================================================================
286
288{
289 static constexpr uint8_t map[]{ Load::remote(1) };
290 LoadStates states[1]{ LoadStates::LOAD_ON };
292
293 cycle< map >(core, states);
294
295 TEST_ASSERT_TRUE(core.isPending(0));
296 TEST_ASSERT_EQUAL(1, drain(core, 0));
297}
298
300{
301 static constexpr uint8_t map[]{ Load::remote(1) };
302 LoadStates states[1]{ LoadStates::LOAD_ON };
304
305 cycle< map >(core, states);
306
307 TEST_ASSERT_EQUAL(1, drain(core, 0));
308 TEST_ASSERT_FALSE(core.isPending(0));
309 TEST_ASSERT_EQUAL(-1, drain(core, 0));
310}
311
313{
314 static constexpr uint8_t map[]{ Load::remote(1) };
315 LoadStates states[1]{ LoadStates::LOAD_ON };
317
318 cycle< map >(core, states);
319 TEST_ASSERT_EQUAL(1, drain(core, 0));
320
321 cycle< map >(core, states);
322 TEST_ASSERT_FALSE(core.isPending(0));
323}
324
326{
327 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(1) };
328 LoadStates states[2]{};
330
331 setStates(states, 0b01);
332 cycle< map >(core, states);
333
334 // never drained
335 setStates(states, 0b10);
336 cycle< map >(core, states);
337
338 TEST_ASSERT_EQUAL_HEX8(0b10, core.payloadOf(0));
339 TEST_ASSERT_EQUAL(0b10, drain(core, 0));
340}
341
343{
344 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(2) };
345 LoadStates states[2]{};
347
348 cycle< map >(core, states);
349 // both start at 0, which matches the initial previousBitmask: nothing to send
350 TEST_ASSERT_FALSE(core.isPending(0));
351 TEST_ASSERT_FALSE(core.isPending(1));
352
353 setStates(states, 0b01);
354 cycle< map >(core, states);
355
356 TEST_ASSERT_TRUE(core.isPending(0));
357 TEST_ASSERT_FALSE(core.isPending(1));
358}
359
360// ============================================================================
361// Keep-alive refresh
362// ============================================================================
363
365{
366 static constexpr uint8_t map[]{ Load::remote(1) };
367 LoadStates states[1]{ LoadStates::LOAD_ON };
369
370 cycle< map >(core, states);
371 TEST_ASSERT_EQUAL(1, drain(core, 0));
372
373 for (uint8_t i = 1; i < REMOTE_REFRESH_CYCLES; ++i)
374 {
375 cycle< map >(core, states);
376 TEST_ASSERT_FALSE(core.isPending(0));
377 }
378
379 cycle< map >(core, states);
380 TEST_ASSERT_TRUE(core.isPending(0));
381 TEST_ASSERT_EQUAL(1, drain(core, 0));
382}
383
385{
386 static constexpr uint8_t map[]{ Load::remote(1) };
387 LoadStates states[1]{ LoadStates::LOAD_ON };
389
390 for (uint8_t i = 0; i < 2 * REMOTE_REFRESH_CYCLES + 1; ++i)
391 {
392 cycle< map >(core, states);
393 uint8_t payload{ 0 };
394 core.takePending(0, payload);
395 }
396
397 // two full refresh periods elapsed without the counter running away
398 cycle< map >(core, states);
399 TEST_ASSERT_FALSE(core.isPending(0));
400}
401
403{
404 static constexpr uint8_t map[]{ Load::remote(1) };
405 LoadStates states[1]{ LoadStates::LOAD_ON };
407
408 cycle< map >(core, states);
409 TEST_ASSERT_EQUAL(1, drain(core, 0));
410
411 // three quiet cycles
412 for (uint8_t i = 1; i < 4; ++i)
413 {
414 cycle< map >(core, states);
415 }
416
417 // a change, which resets the counter
418 states[0] = LoadStates::LOAD_OFF;
419 cycle< map >(core, states);
420 TEST_ASSERT_EQUAL(0, drain(core, 0));
421
422 // the refresh must now be a full period away, not one cycle away
423 cycle< map >(core, states);
424 TEST_ASSERT_FALSE(core.isPending(0));
425}
426
427// ============================================================================
428// Edge cases
429// ============================================================================
430
432{
433 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(1) };
436
437 for (uint8_t i = 0; i < REMOTE_REFRESH_CYCLES; ++i)
438 {
439 cycle< map >(core, states);
440 }
441
442 TEST_ASSERT_TRUE(core.isPending(0));
443 TEST_ASSERT_EQUAL(0, drain(core, 0));
444}
445
447{
448 static constexpr uint8_t map[]{ Load::local(5), Load::local(6) };
451
452 cycle< map >(core, states);
453
454 TEST_ASSERT_EQUAL(0, RemoteLoadCore< 0 >::size());
455 TEST_ASSERT_FALSE(core.isPending(0));
456 TEST_ASSERT_EQUAL(-1, drain(core, 0));
457}
458
460{
461 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(2) };
464
465 cycle< map >(core, states);
466 TEST_ASSERT_TRUE(core.isPending(0));
467
468 core.reset();
469
470 TEST_ASSERT_FALSE(core.isPending(0));
471 TEST_ASSERT_FALSE(core.isPending(1));
472 TEST_ASSERT_EQUAL_HEX8(0, core.payloadOf(0));
473 TEST_ASSERT_EQUAL_HEX8(0, core.payloadOf(1));
474}
475
477{
478 // unit 3 has no slot in a 2-unit core: it must be skipped, not written past the array
479 static constexpr uint8_t map[]{ Load::remote(1), Load::remote(3), Load::remote(2) };
482
483 cycle< map >(core, states);
484
485 TEST_ASSERT_EQUAL_HEX8(1, core.payloadOf(0));
486 TEST_ASSERT_EQUAL_HEX8(1, core.payloadOf(1));
487}
488
490{
492
493 TEST_ASSERT_EQUAL_HEX8(0, core.payloadOf(7));
494 TEST_ASSERT_FALSE(core.isPending(7));
495 TEST_ASSERT_EQUAL(-1, drain(core, 7));
496}
497
498// ============================================================================
499// Override pins - the map seen through the override subsystem
500// ============================================================================
501
503{
504 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::local(7) };
505
506 static_assert(overridePinOf(map, 0) == 5, "a local load keeps its pin");
507
508 TEST_ASSERT_EQUAL_UINT8(5, overridePinOf(map, 0));
509 TEST_ASSERT_EQUAL_UINT8(7, overridePinOf(map, 2));
510}
511
513{
514 // the virtual pin counts remote loads across every unit, in map order
515 static constexpr uint8_t map[]{ Load::remote(2), Load::local(5), Load::remote(1), Load::remote(2) };
516
517 TEST_ASSERT_EQUAL_UINT8(REMOTE_PIN_BASE + 0, overridePinOf(map, 0));
518 TEST_ASSERT_EQUAL_UINT8(5, overridePinOf(map, 1));
519 TEST_ASSERT_EQUAL_UINT8(REMOTE_PIN_BASE + 1, overridePinOf(map, 2));
520 TEST_ASSERT_EQUAL_UINT8(REMOTE_PIN_BASE + 2, overridePinOf(map, 3));
521}
522
524{
525 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::local(7), Load::remote(2) };
526
527 TEST_ASSERT_EQUAL_HEX32((1UL << 5) | (1UL << 7), localLoadsMask(map));
528 TEST_ASSERT_EQUAL_HEX32((1UL << 16) | (1UL << 17), remoteLoadsMask(map));
529}
530
532{
533 static constexpr uint8_t map[]{ Load::local(5), Load::local(6) };
534
535 TEST_ASSERT_EQUAL_HEX32((1UL << 5) | (1UL << 6), localLoadsMask(map));
536 TEST_ASSERT_EQUAL_HEX32(0, remoteLoadsMask(map));
537}
538
540{
541 // ALL_LOADS() and LOAD(n) in config.h, turned into pins against the map, as config_derived.h does
542 static constexpr uint8_t map[]{ Load::local(5), Load::remote(1), Load::remote(2), Load::local(6) };
543 static constexpr RelaySetting relaySettings[]{ { 9 } };
544 static constexpr RelayEngine relays{ relaySettings };
545 constexpr TargetMap targets{ map, relays, false };
546 constexpr OverridePins< 2, 8 > settings{ { { 4, { ALL_LOADS() } },
547 { 3, { LOAD(2) } } } };
548 constexpr OverrideTable pins{ settings, targets };
549
550 TEST_ASSERT_EQUAL_HEX16((1U << 5) | (1U << 6), pins.getLocalBitmask(0));
551 TEST_ASSERT_EQUAL_HEX8(0b11, pins.getRemoteBitmask(0));
552
553 // load 2 is the second remote load, whichever unit it sits on
554 TEST_ASSERT_EQUAL_HEX16(0, pins.getLocalBitmask(1));
555 TEST_ASSERT_EQUAL_HEX8(0b10, pins.getRemoteBitmask(1));
556}
557
558// ============================================================================
559// Load map validation (validation.h asserts on these)
560// ============================================================================
561
563{
564 static constexpr uint8_t map[]{ Load::local(2), Load::local(13), Load::remote(1), Load::remote(3, 9) };
565
566 static_assert(Load::isValidMap(map), "a well-formed map must pass");
567 TEST_ASSERT_TRUE(Load::isValidMap(map));
568}
569
571{
572 static constexpr uint8_t serialRx[]{ Load::local(0) };
573 static constexpr uint8_t serialTx[]{ Load::local(1) };
574 static constexpr uint8_t missing[]{ Load::local(14) };
575
576 TEST_ASSERT_FALSE(Load::isValidMap(serialRx));
577 TEST_ASSERT_FALSE(Load::isValidMap(serialTx));
578 TEST_ASSERT_FALSE(Load::isValidMap(missing));
579}
580
582{
583 // unused_pin is 0xFF, which decodes as unit 3 with a status LED on pin 63
584 static constexpr uint8_t map[]{ Load::local(5), 0xFF };
585
586 static_assert(!Load::isValidMap(map), "the unused_pin sentinel must be caught");
587 TEST_ASSERT_FALSE(Load::isValidMap(map));
588}
589
591{
592 static constexpr uint8_t noLed[]{ Load::remote(1) };
593 static constexpr uint8_t ledOnSerial[]{ Load::remote(1, 1) };
594 static constexpr uint8_t ledOk[]{ Load::remote(1, 8) };
595
596 TEST_ASSERT_TRUE(Load::isValidMap(noLed));
597 TEST_ASSERT_FALSE(Load::isValidMap(ledOnSerial));
598 TEST_ASSERT_TRUE(Load::isValidMap(ledOk));
599}
600
602{
603 // packed as is, unit 4 wraps to 0: remote(4, 8) would be local(8), a TRIAC on the LED pin
604 static constexpr uint8_t unit4[]{ Load::local(5), Load::remote(4, 8) };
605 static constexpr uint8_t unit0[]{ Load::remote(0, 8) };
606 static constexpr uint8_t unit4NoLed[]{ Load::remote(4) };
607 static constexpr uint8_t units1to3[]{ Load::remote(1), Load::remote(2, 8), Load::remote(3) };
608
609 static_assert(!Load::hasValidRemoteUnits(unit4), "unit 4 must be caught");
610 TEST_ASSERT_EQUAL_UINT8(invalidRemoteUnitEntry, Load::remote(4, 8));
611 TEST_ASSERT_FALSE(Load::isLocal(Load::remote(4, 8)));
612 TEST_ASSERT_FALSE(Load::hasValidRemoteUnits(unit4));
613 TEST_ASSERT_FALSE(Load::hasValidRemoteUnits(unit0));
614 TEST_ASSERT_FALSE(Load::hasValidRemoteUnits(unit4NoLed));
615 TEST_ASSERT_TRUE(Load::hasValidRemoteUnits(units1to3));
616
617 // one error only: the map check leaves the marker to hasValidRemoteUnits()
618 TEST_ASSERT_TRUE(Load::isValidMap(unit4));
619}
620
621// ============================================================================
622// Node ID validation (validation.h asserts on this)
623// ============================================================================
624
626{
627 static constexpr uint8_t ids[]{ 15, 16, 17 };
628
629 static_assert(areValidNodeIds(ids, 3, 10), "distinct in-range IDs must pass");
630 TEST_ASSERT_TRUE(areValidNodeIds(ids, 3, 10));
631 TEST_ASSERT_TRUE(areValidNodeIds(ids, 0, 10)); // no remote unit: nothing to check
632}
633
635{
636 static constexpr uint8_t ids[]{ 15, 15, 17 };
637
638 TEST_ASSERT_FALSE(areValidNodeIds(ids, 2, 10));
639 TEST_ASSERT_TRUE(areValidNodeIds(ids, 1, 10)); // entries past the last unit are ignored
640}
641
643{
644 static constexpr uint8_t ids[]{ 15, 10 };
645
646 TEST_ASSERT_FALSE(areValidNodeIds(ids, 2, 10));
647}
648
650{
651 static constexpr uint8_t zero[]{ 0 };
652 static constexpr uint8_t tooHigh[]{ 31 };
653 static constexpr uint8_t bounds[]{ 1, 30 };
654
655 TEST_ASSERT_FALSE(areValidNodeIds(zero, 1, 10));
656 TEST_ASSERT_FALSE(areValidNodeIds(tooHigh, 1, 10));
657 TEST_ASSERT_TRUE(areValidNodeIds(bounds, 2, 10));
658}
659
661{
662 static constexpr uint8_t ids[]{ 15 };
663
664 TEST_ASSERT_FALSE(areValidNodeIds(ids, 2, 10));
665}
666
667// ============================================================================
668// Sending - the radio replaced by a recorder
669// ============================================================================
670
673{
674 uint8_t nodeId[8]{};
675 uint8_t payload[8]{};
676 uint8_t count{ 0 };
677
678 void operator()(uint8_t id, uint8_t data)
679 {
680 if (count < 8)
681 {
682 nodeId[count] = id;
683 payload[count] = data;
684 }
685 ++count;
686 }
687};
688
689static constexpr uint8_t sendMap[]{ Load::local(5), Load::remote(1), Load::remote(2), Load::remote(1) };
690static constexpr uint8_t sendIds[]{ 15, 16, 17 };
691
693{
695 RecordingRadio radio;
696 LoadStates states[4];
697
698 setStates(states, 0b1100); // unit 1: loads 1 (off) and 3 (on) -> 0b10 ; unit 2: load 2 (on) -> 0b1
699 cycle< sendMap >(core, states);
700 core.sendPending(sendIds, radio);
701
702 TEST_ASSERT_EQUAL_UINT8(2, radio.count);
703 TEST_ASSERT_EQUAL_UINT8(15, radio.nodeId[0]);
704 TEST_ASSERT_EQUAL_HEX8(0b10, radio.payload[0]);
705 TEST_ASSERT_EQUAL_UINT8(16, radio.nodeId[1]);
706 TEST_ASSERT_EQUAL_HEX8(0b1, radio.payload[1]);
707}
708
710{
712 RecordingRadio radio;
713 LoadStates states[4];
714
715 setStates(states, 0b1100);
716 cycle< sendMap >(core, states);
717 core.sendPending(sendIds, radio);
718 core.sendPending(sendIds, radio); // nothing new since
719
720 TEST_ASSERT_EQUAL_UINT8(2, radio.count);
721}
722
724{
726 RecordingRadio radio;
727 LoadStates states[4];
728
729 setStates(states, 0b1100);
730 cycle< sendMap >(core, states);
731 core.sendPending(sendIds, RecordingRadio{}); // flush the first cycle
732
733 setStates(states, 0b1000); // only load 2 (unit 2) switches off
734 cycle< sendMap >(core, states);
735 core.sendPending(sendIds, radio);
736
737 TEST_ASSERT_EQUAL_UINT8(1, radio.count);
738 TEST_ASSERT_EQUAL_UINT8(16, radio.nodeId[0]);
739 TEST_ASSERT_EQUAL_HEX8(0, radio.payload[0]);
740}
741
743{
745 RecordingRadio radio;
746 LoadStates states[4];
747
748 setStates(states, 0b1100);
749 cycle< sendMap >(core, states);
751
752 for (uint8_t i = 0; i < REMOTE_REFRESH_CYCLES; ++i)
753 {
754 cycle< sendMap >(core, states);
755 }
756 core.sendPending(sendIds, radio);
757
758 TEST_ASSERT_EQUAL_UINT8(2, radio.count);
759 TEST_ASSERT_EQUAL_UINT8(15, radio.nodeId[0]);
760 TEST_ASSERT_EQUAL_UINT8(16, radio.nodeId[1]);
761}
762
764{
766 RecordingRadio radio;
767
768 core.sendPending(sendIds, radio);
769
770 TEST_ASSERT_EQUAL_UINT8(0, radio.count);
771}
772
773// ============================================================================
774
775int main(int, char **)
776{
777 UNITY_BEGIN();
778
779 // Packed encoding
784 RUN_TEST(test_census_is_constexpr);
790
791 // Unit grouping
792 RUN_TEST(test_single_unit_payload);
796 RUN_TEST(test_interleaved_map);
797
798 // Bit ordering
802
803 // Change detection
809
810 // Keep-alive refresh
814
815 // Edge cases
821
827
833
839
845
846 return UNITY_END();
847}
The override pins, as in config.h: each pin with the targets it forces.
Manages override pins and their associated bitmasks for forced operation.
The relays, as in config.h: the sliding average and the settings of each relay.
Builds and schedules the RF payloads of every remote unit.
bool takePending(uint8_t unitIdx, uint8_t &payload)
Claim a pending transmission for one unit.
void reset()
Return every unit to its power-on state.
constexpr uint8_t payloadOf(uint8_t unitIdx) const
Current payload of a unit, whether or not it has been sent.
constexpr bool isPending(uint8_t unitIdx) const
True if a transmission is due for that unit.
void sendPending(const uint8_t(&nodeIds)[NumIds], Send &&send)
Hand every pending payload to send, addressed to its unit's node ID.
static constexpr uint8_t size()
Number of remote units handled.
What the target codes of config.h stand for, once the load map and the relays are known.
constexpr RelayEngine relays
Definition config.h:130
constexpr uint8_t REMOTE_PIN_BASE
Base value for virtual pins representing remote loads.
constexpr uint8_t ALL_LOADS()
Every load, local and remote.
constexpr uint8_t LOAD(const uint8_t loadNum)
Load loadNum of physicalLoadPin[], local or remote (0, no load, if out of range).
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
Encoding of the load map: one packed byte per dump load.
constexpr uint8_t invalidRemoteUnitEntry
The entry Load::remote() returns for a unit outside 1..MAX_REMOTE_UNITS.
Definition load_map.h:57
LoadStates
Definition load_map.h:32
constexpr bool isLocal(uint8_t entry)
True if the entry describes a locally driven load.
Definition load_map.h:89
constexpr uint8_t maxLoadsPerUnit(const uint8_t(&map)[N])
Largest number of loads assigned to any single remote unit.
Definition load_map.h:144
constexpr uint8_t bitOf(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a load among the loads of its own unit, in ascending map order.
Definition load_map.h:195
constexpr uint8_t local(uint8_t pin)
Declares a load driven locally by the router.
Definition load_map.h:68
constexpr uint8_t countUnits(const uint8_t(&map)[N])
Highest remote unit number appearing in the map (0 if fully local).
Definition load_map.h:110
constexpr uint8_t remoteOrdinal(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a remote load among the remote loads, in ascending map order.
Definition load_map.h:176
constexpr uint8_t pinOf(uint8_t entry)
The pin of the entry: TRIAC pin if local, status LED (0 = none) if remote.
Definition load_map.h:101
constexpr bool isValidMap(const uint8_t(&map)[N])
Check that every entry of a load map designates a usable pin.
Definition load_map.h:258
constexpr uint8_t countRemoteLoads(const uint8_t(&map)[N])
Number of entries of the map that are driven remotely.
Definition load_map.h:127
constexpr OverrideMasks< N > overrideMasks(const uint8_t(&map)[N])
Build the override bits of a load map.
Definition load_map.h:230
constexpr bool hasValidRemoteUnits(const uint8_t(&map)[N])
Check that every remote load of a map was declared with a unit from 1 to 3.
Definition load_map.h:288
constexpr uint8_t unitOf(uint8_t entry)
The owning unit of the entry: 0 for a local load, 1..3 for a remote one.
Definition load_map.h:95
constexpr uint8_t remote(uint8_t unit, uint8_t ledPin=0)
Declares a load driven by a remote unit over RF.
Definition load_map.h:79
int main(void)
void test_sendPending_refreshes_every_unit(void)
void test_a_change_on_one_unit_does_not_flag_the_other(void)
void test_local_loads_are_ignored(void)
void test_interleaved_map(void)
void test_sendPending_only_addresses_the_unit_that_changed(void)
void test_first_load_of_a_unit_is_bit_zero(void)
void test_remote_without_led_has_pin_zero(void)
Definition test_main.cpp:73
void test_draining_clears_the_flag(void)
void test_load_masks_of_an_all_local_map(void)
void test_all_off_still_refreshes(void)
void test_local_keeps_unit_zero(void)
Definition test_main.cpp:53
void test_census_is_constexpr(void)
Definition test_main.cpp:86
void test_remoteOrdinal_counts_remote_entries_only(void)
void test_a_unit_beyond_the_core_is_ignored(void)
void test_isValidMap_rejects_the_unused_pin_sentinel(void)
void test_isValidMap_rejects_unusable_local_pins(void)
void test_out_of_range_accessors_are_safe(void)
void test_unchanged_state_does_not_flag(void)
void test_node_ids_rejects_the_router_id(void)
static void setStates(LoadStates(&states)[NumLoads], uint8_t pattern)
Build a LoadStates array from a bit pattern, bit i = load i.
Definition test_main.cpp:41
void test_refresh_fires_on_the_fifth_unchanged_cycle(void)
void test_masks_feed_the_override_config(void)
void test_node_ids_accepts_distinct_ids(void)
void test_isValidMap_checks_a_remote_status_led(void)
void test_sendPending_drains_what_it_sends(void)
void test_three_units_one_load_each(void)
void test_reset_clears_every_unit(void)
void test_remote_led_pin_round_trips(void)
Definition test_main.cpp:79
static constexpr uint8_t sendIds[]
void test_isValidMap_accepts_a_mixed_map(void)
void test_eight_loads_on_one_unit(void)
void test_bitOf_counts_within_the_same_unit(void)
void test_refresh_counter_restarts_after_a_refresh(void)
void test_overrideMasks_give_no_bit_to_an_out_of_range_entry(void)
void test_node_ids_rejects_a_duplicate(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_sendPending_without_units_never_sends(void)
void test_node_ids_rejects_a_short_table(void)
void test_node_ids_rejects_out_of_range(void)
void test_two_units_are_independent(void)
void test_no_unit_configured_is_a_no_op(void)
void test_single_unit_payload(void)
void test_overridePinOf_local_is_the_physical_pin(void)
void test_overrideMasks_match_the_shift_they_replace(void)
void test_remote_encodes_the_unit_number(void)
Definition test_main.cpp:61
void test_sendPending_addresses_each_unit_by_its_node_id(void)
static constexpr uint8_t sendMap[]
void test_census_of_an_all_local_map(void)
Definition test_main.cpp:99
void test_load_masks_of_a_mixed_map(void)
static int drain(RemoteLoadCore< NumUnits > &core, uint8_t unitIdx)
Claim a unit's payload if a transmission is due, else return -1.
Definition test_main.cpp:33
void test_remote_unit_out_of_range_is_flagged(void)
void test_payload_tracks_the_latest_state_even_if_unsent(void)
void test_index_alignment_with_a_leading_local_load(void)
void test_a_change_restarts_the_refresh_counter(void)
void test_change_flags_a_transmission(void)
void test_overridePinOf_remote_is_a_virtual_pin(void)
Unit grouping and payload bookkeeping for remotely controlled loads.
constexpr bool areValidNodeIds(const uint8_t(&nodeIds)[NumIds], uint8_t numUnits, uint8_t routerId)
Check the node IDs of the first numUnits remote units.
constexpr uint8_t REMOTE_REFRESH_CYCLES
Stands in for RFM69::send(): records who would have been sent what.
uint8_t payload[8]
uint8_t nodeId[8]
void operator()(uint8_t id, uint8_t data)
The settings of one relay, as in config.h: { pin, surplus, import, minON, minOFF }...
Compile-time utilities for managing override pins and index-to-bitmask mapping.
constexpr uint32_t remoteLoadsMask(const uint8_t(&loadMap)[N])
Bitmask of every remote load (bit 16 + remote rank).
constexpr uint8_t overridePinOf(const uint8_t(&loadMap)[N], uint8_t loadNum)
Override pin of a load: its physical pin if local, a virtual pin if remote.
constexpr uint32_t localLoadsMask(const uint8_t(&loadMap)[N])
Bitmask of the pins of every local load (lower 16 bits).