25template< const auto &Map, u
int8_t NumUnits, u
int8_t NumLoads >
28 core.template updateLoads< Map >(states);
32template< u
int8_t NumUnits >
36 return core.
takePending(unitIdx, payload) ?
static_cast< int >(payload) : -1;
40template< u
int8_t NumLoads >
43 for (uint8_t
i = 0;
i < NumLoads; ++
i)
125 for (uint8_t
i = 0;
i <
sizeof(map); ++
i)
129 TEST_ASSERT_EQUAL_HEX16(1U <<
Load::pinOf(map[
i]), masks.local[
i]);
130 TEST_ASSERT_EQUAL_HEX8(0, masks.remote[
i]);
134 TEST_ASSERT_EQUAL_HEX16(0, masks.local[
i]);
138 TEST_ASSERT_EQUAL_HEX16(0x2000, masks.local[2]);
139 TEST_ASSERT_EQUAL_HEX8(0x04, masks.remote[5]);
149 TEST_ASSERT_EQUAL_HEX16(0, masks.local[0]);
150 TEST_ASSERT_EQUAL_HEX8(0x80, masks.remote[8]);
151 TEST_ASSERT_EQUAL_HEX8(0, masks.remote[9]);
178 TEST_ASSERT_EQUAL_HEX8(0b101, core.
payloadOf(0));
189 TEST_ASSERT_EQUAL_HEX8(0, core.
payloadOf(0));
202 TEST_ASSERT_EQUAL_HEX8(0b01, core.
payloadOf(0));
203 TEST_ASSERT_EQUAL_HEX8(0b10, core.
payloadOf(1));
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));
231 TEST_ASSERT_EQUAL_HEX8(0b11, core.
payloadOf(0));
232 TEST_ASSERT_EQUAL_HEX8(0b01, core.
payloadOf(1));
247 TEST_ASSERT_EQUAL_HEX8(0b001, core.
payloadOf(0));
251 TEST_ASSERT_EQUAL_HEX8(0b100, core.
payloadOf(0));
263 TEST_ASSERT_EQUAL_HEX8(0x80, core.
payloadOf(0));
267 TEST_ASSERT_EQUAL_HEX8(0xFF, core.
payloadOf(0));
280 TEST_ASSERT_EQUAL_HEX8(0b01, core.
payloadOf(0));
296 TEST_ASSERT_EQUAL(1,
drain(core, 0));
307 TEST_ASSERT_EQUAL(1,
drain(core, 0));
309 TEST_ASSERT_EQUAL(-1,
drain(core, 0));
319 TEST_ASSERT_EQUAL(1,
drain(core, 0));
338 TEST_ASSERT_EQUAL_HEX8(0b10, core.
payloadOf(0));
339 TEST_ASSERT_EQUAL(0b10,
drain(core, 0));
371 TEST_ASSERT_EQUAL(1,
drain(core, 0));
381 TEST_ASSERT_EQUAL(1,
drain(core, 0));
393 uint8_t payload{ 0 };
409 TEST_ASSERT_EQUAL(1,
drain(core, 0));
412 for (uint8_t
i = 1;
i < 4; ++
i)
420 TEST_ASSERT_EQUAL(0,
drain(core, 0));
443 TEST_ASSERT_EQUAL(0,
drain(core, 0));
456 TEST_ASSERT_EQUAL(-1,
drain(core, 0));
472 TEST_ASSERT_EQUAL_HEX8(0, core.
payloadOf(0));
473 TEST_ASSERT_EQUAL_HEX8(0, core.
payloadOf(1));
485 TEST_ASSERT_EQUAL_HEX8(1, core.
payloadOf(0));
486 TEST_ASSERT_EQUAL_HEX8(1, core.
payloadOf(1));
493 TEST_ASSERT_EQUAL_HEX8(0, core.
payloadOf(7));
495 TEST_ASSERT_EQUAL(-1,
drain(core, 7));
506 static_assert(
overridePinOf(map, 0) == 5,
"a local load keeps its pin");
527 TEST_ASSERT_EQUAL_HEX32((1UL << 5) | (1UL << 7),
localLoadsMask(map));
528 TEST_ASSERT_EQUAL_HEX32((1UL << 16) | (1UL << 17),
remoteLoadsMask(map));
535 TEST_ASSERT_EQUAL_HEX32((1UL << 5) | (1UL << 6),
localLoadsMask(map));
547 { 3, {
LOAD(2) } } } };
550 TEST_ASSERT_EQUAL_HEX16((1U << 5) | (1U << 6), pins.getLocalBitmask(0));
551 TEST_ASSERT_EQUAL_HEX8(0b11, pins.getRemoteBitmask(0));
554 TEST_ASSERT_EQUAL_HEX16(0, pins.getLocalBitmask(1));
555 TEST_ASSERT_EQUAL_HEX8(0b10, pins.getRemoteBitmask(1));
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) };
584 static constexpr uint8_t map[]{
Load::local(5), 0xFF };
586 static_assert(!
Load::isValidMap(map),
"the unused_pin sentinel must be caught");
593 static constexpr uint8_t ledOnSerial[]{
Load::remote(1, 1) };
606 static constexpr uint8_t unit4NoLed[]{
Load::remote(4) };
627 static constexpr uint8_t ids[]{ 15, 16, 17 };
629 static_assert(
areValidNodeIds(ids, 3, 10),
"distinct in-range IDs must pass");
636 static constexpr uint8_t ids[]{ 15, 15, 17 };
644 static constexpr uint8_t ids[]{ 15, 10 };
651 static constexpr uint8_t zero[]{ 0 };
652 static constexpr uint8_t tooHigh[]{ 31 };
653 static constexpr uint8_t bounds[]{ 1, 30 };
662 static constexpr uint8_t ids[]{ 15 };
690static constexpr uint8_t
sendIds[]{ 15, 16, 17 };
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]);
720 TEST_ASSERT_EQUAL_UINT8(2, radio.count);
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]);
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]);
770 TEST_ASSERT_EQUAL_UINT8(0, radio.count);
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
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).
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.
constexpr bool isLocal(uint8_t entry)
True if the entry describes a locally driven load.
constexpr uint8_t maxLoadsPerUnit(const uint8_t(&map)[N])
Largest number of loads assigned to any single remote unit.
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.
constexpr uint8_t local(uint8_t pin)
Declares a load driven locally by the router.
constexpr uint8_t countUnits(const uint8_t(&map)[N])
Highest remote unit number appearing in the map (0 if fully local).
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.
constexpr uint8_t pinOf(uint8_t entry)
The pin of the entry: TRIAC pin if local, status LED (0 = none) if remote.
constexpr bool isValidMap(const uint8_t(&map)[N])
Check that every entry of a load map designates a usable pin.
constexpr uint8_t countRemoteLoads(const uint8_t(&map)[N])
Number of entries of the map that are driven remotely.
constexpr OverrideMasks< N > overrideMasks(const uint8_t(&map)[N])
Build the override bits of a load map.
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.
constexpr uint8_t unitOf(uint8_t entry)
The owning unit of the entry: 0 for a local load, 1..3 for a remote one.
constexpr uint8_t remote(uint8_t unit, uint8_t ledPin=0)
Declares a load driven by a remote unit over RF.
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)
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)
void test_census_is_constexpr(void)
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.
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)
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.
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)
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)
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.
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.
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).