28bool isOn(
const uint8_t entry)
30 return (entry & ON) != 0;
57bool noOverride(uint8_t )
72 uint8_t allOff[3]{ 0, 1, 2 };
73 uint8_t firstOn[3]{ 0 | ON, 1, 2 };
74 uint8_t gap[3]{ 0 | ON, 1, 2 | ON };
83 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
90 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
91 uint8_t twoOn[3]{ 0 | ON, 1 | ON, 2 };
92 uint8_t gap[3]{ 0 | ON, 1, 2 | ON };
101 uint8_t allOff[3]{ 0, 1, 2 };
109 uint8_t prio[3]{ 2 | ON, 0, 1 };
122 uint8_t prio[3]{ 0, 1, 2 };
126 TEST_ASSERT_TRUE(isOn(prio[0]));
127 TEST_ASSERT_FALSE(isOn(prio[1]));
128 TEST_ASSERT_FALSE(isOn(prio[2]));
136 uint8_t prio[3]{ 2, 0, 1 };
140 TEST_ASSERT_EQUAL_HEX8(2 | ON, prio[0]);
141 TEST_ASSERT_EQUAL_HEX8(0, prio[1]);
142 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
148 uint8_t prio[3]{ 0, 1, 2 };
151 int32_t energy{ 800 };
156 TEST_ASSERT_FALSE_MESSAGE(isOn(prio[1]),
"second load switched during the post-transition period");
161 TEST_ASSERT_TRUE(isOn(prio[1]));
162 TEST_ASSERT_FALSE(isOn(prio[2]));
169 uint8_t prio[3]{ 0, 1, 2 };
177 cycles(s, prio, 850, 5);
179 TEST_ASSERT_FALSE(isOn(prio[1]));
185 uint8_t prio[3]{ 0, 1, 2 };
198 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
202 TEST_ASSERT_TRUE(isOn(prio[0]));
203 TEST_ASSERT_TRUE(isOn(prio[1]));
204 TEST_ASSERT_FALSE(isOn(prio[2]));
211 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
214 int32_t energy{ 200 };
219 TEST_ASSERT_TRUE_MESSAGE(isOn(prio[1]),
"second load switched off during the post-transition period");
224 TEST_ASSERT_FALSE(isOn(prio[1]));
225 TEST_ASSERT_TRUE(isOn(prio[0]));
231 uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
244 uint8_t prio[3]{ 0 | ON, 1, 2 };
247 TEST_ASSERT_TRUE(isOn(prio[1]));
251 TEST_ASSERT_FALSE(isOn(prio[1]));
252 TEST_ASSERT_TRUE(isOn(prio[0]));
258 uint8_t prio[3]{ 0, 1, 2 };
262 TEST_ASSERT_FALSE(isOn(prio[0]));
269 uint8_t prio[3]{ 0 | ON, 1, 2 };
287 uint8_t prio[3]{ 0 | ON, 1, 2 };
290 cycles(s, prio, 450, 5, bucketAF);
291 cycles(s, prio, 550, 5, bucketAF);
292 TEST_ASSERT_EQUAL_HEX8(0 | ON, prio[0]);
293 TEST_ASSERT_EQUAL_HEX8(1, prio[1]);
296 TEST_ASSERT_TRUE(isOn(prio[1]));
302 uint8_t allOn[3]{ 0 | ON, 1 | ON, 2 | ON };
303 uint8_t allOff[3]{ 0, 1, 2 };
305 cycles(s, allOn, 900, MAX_COUNT);
309 cycles(s, allOff, 100, MAX_COUNT);
311 TEST_ASSERT_EQUAL_HEX8(0, allOff[0]);
317 uint8_t prio[3]{ 0, 1, 2 };
319 int32_t energy{ 600 };
340 uint8_t prio[3]{ 0, 1, 2 };
344 TEST_ASSERT_EQUAL_HEX8(2, prio[0]);
345 TEST_ASSERT_EQUAL_HEX8(0, prio[1]);
346 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
351 uint8_t prio[3]{ 0 | ON, 1, 2 | ON };
355 TEST_ASSERT_EQUAL_HEX8(2 | ON, prio[0]);
356 TEST_ASSERT_EQUAL_HEX8(0 | ON, prio[1]);
357 TEST_ASSERT_EQUAL_HEX8(1, prio[2]);
362 uint8_t prio[4]{ 3, 1, 0, 2 };
364 for (uint8_t
i = 0;
i != 4; ++
i)
369 TEST_ASSERT_EQUAL_HEX8(3, prio[0]);
370 TEST_ASSERT_EQUAL_HEX8(1, prio[1]);
371 TEST_ASSERT_EQUAL_HEX8(0, prio[2]);
372 TEST_ASSERT_EQUAL_HEX8(2, prio[3]);
380 uint8_t before{ 0xAA };
381 uint8_t prio[1]{ 0 | ON };
382 uint8_t after[4]{ 0x55, 0x55, 0x55, 0x55 };
387 TEST_ASSERT_EQUAL_HEX8(0 | ON, mem.prio[0]);
388 TEST_ASSERT_EQUAL_HEX8(0xAA, mem.before);
389 TEST_ASSERT_EQUAL_HEX8(0x55, mem.after[0]);
390 TEST_ASSERT_EQUAL_HEX8(0x55, mem.after[3]);
399 const uint8_t prio[3]{ 2 | ON, 0, 1 | ON };
411 const uint8_t prio[3]{ 0 | ON, 1 | ON, 2 | ON };
423 const uint8_t prio[3]{ 0, 1, 2 };
425 const auto overrideLoad1 = [](
const uint8_t iLoad)
442 const uint8_t prio[3]{ 2, 0, 1 };
445 const auto overrideLoad2 = [](
const uint8_t iLoad)
459 const uint8_t prio[3]{ 0 | ON, 1 | ON, 2 };
461 const auto overrideAll = [](uint8_t )
The load priority state machine: which load to switch at each mains cycle.
constexpr uint8_t loadStateOnBit
uint8_t nextToRemove(const uint8_t(&priorities)[N])
The next logical load to switch OFF: the last one ON, in priority order.
uint8_t nextToAdd(const uint8_t(&priorities)[N])
The next logical load to switch ON: the first one OFF, in priority order.
void toPhysical(const uint8_t(&priorities)[N], LoadStates(&physical)[N], const bool diversionEnabled, IsOverridden isOverridden)
Maps the logical load states to the physical ones.
void rotate(uint8_t(&priorities)[N])
Rotates the priorities by one: the last load becomes the first, the others move down one place....
void test_the_lower_threshold_stays_within_the_bucket(void)
void test_rotate_n_times_is_back_to_the_start(void)
void test_nothing_to_switch_starts_no_post_transition_period(void)
void test_rotate_a_single_load_changes_nothing(void)
void test_next_to_remove_none_when_all_off(void)
void test_the_override_is_asked_for_the_physical_load(void)
void test_the_upper_threshold_stays_within_the_bucket(void)
void test_rotate_moves_the_last_load_first(void)
void test_next_to_add_none_when_all_on(void)
void test_a_surplus_brings_every_load_on_then_its_end_every_load_off(void)
void test_the_upper_threshold_follows_the_bucket_during_the_post_transition_period(void)
void test_rotate_keeps_each_load_state(void)
void test_the_previous_load_waits_for_the_end_of_the_post_transition_period(void)
void test_an_override_forces_its_load_on(void)
void test_switching_on_follows_the_priorities_not_the_load_numbers(void)
void test_calibration_mode_keeps_every_load_off(void)
void test_next_to_remove_is_the_last_load_on(void)
void test_diversion_off_switches_every_load_off(void)
void test_the_middle_of_the_bucket_counts_as_the_lower_half(void)
void test_each_half_resets_the_opposite_threshold(void)
void test_next_ignores_the_load_numbers(void)
void test_next_to_add_is_the_first_load_off(void)
void test_high_energy_switches_the_first_load_on(void)
void test_low_energy_switches_the_last_load_off(void)
void test_the_next_load_waits_for_the_end_of_the_post_transition_period(void)
void test_physical_states_follow_the_load_numbers(void)
void test_anti_flicker_thresholds_leave_a_dead_band(void)
void test_the_load_switched_last_may_switch_back_at_once(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.
The fixed levels of the energy bucket, in bucket units.
The switching decision, once per mains cycle, and its state.
static constexpr uint8_t POST_TRANSITION_MAX_COUNT
void onNewCycle(uint8_t(&priorities)[N], const int32_t &energy, const Bucket &bucket)
The decision of one mains cycle: may switch one load ON or OFF.