19constexpr uint32_t MINUTE{ 60UL * 1000UL };
20constexpr uint32_t HOUR{ 60UL * MINUTE };
22constexpr uint8_t OFF_PEAK_HOURS{ 8 };
23constexpr int16_t THRESHOLD_x100{ 6000 };
29constexpr uint8_t
remote(
const uint8_t n)
47constexpr uint16_t bit(
const uint8_t n)
49 return static_cast< uint16_t
>(1U << n);
53auto activeIn(
const uint16_t activePins)
55 return [activePins](
const uint8_t pin)
57 return (activePins & bit(pin)) != 0;
72OverrideMasks boostsAt(
const uint32_t elapsed,
const int16_t temperature_x100 = 2000,
const bool bOffPeak =
true)
77void assertMasks(
const uint16_t local,
const uint8_t remoteMask,
const OverrideMasks& masks)
79 TEST_ASSERT_EQUAL_HEX16(local, masks.
local);
80 TEST_ASSERT_EQUAL_HEX8(remoteMask, masks.
remote);
93 assertMasks(0, 0, pinsOnly(0));
98 assertMasks(bit(5) | bit(6), 0, pinsOnly(bit(4)));
103 assertMasks(0, 0x01, pinsOnly(bit(12)));
104 assertMasks(bit(5), 0x02, pinsOnly(bit(3)));
110 assertMasks(0, 0, pinsOnly(bit(2) | bit(5) | bit(13)));
116 assertMasks(bit(5) | bit(6), 0x02, pinsOnly(bit(3) | bit(4)));
117 assertMasks(bit(5) | bit(6), 0x03, pinsOnly(bit(3) | bit(4) | bit(12)));
122 uint16_t readPins{ 0 };
126 readPins |= bit(pin);
130 TEST_ASSERT_EQUAL_UINT8(3, reads);
131 TEST_ASSERT_EQUAL_HEX16(bit(3) | bit(4) | bit(12), readPins);
141 assertMasks(bit(5), 0, boostsAt(HOUR - 1));
142 assertMasks(bit(5) | bit(6), 0x04, boostsAt(HOUR));
148 assertMasks(bit(5) | bit(6), 0x04, boostsAt(2 * HOUR - 1));
149 assertMasks(bit(6), 0x04, boostsAt(2 * HOUR));
150 assertMasks(bit(6), 0x04, boostsAt(3 * HOUR - 1));
151 assertMasks(0, 0, boostsAt(3 * HOUR));
156 assertMasks(bit(5), 0, boostsAt(0));
161 assertMasks(0, 0, boostsAt(4 * HOUR));
162 assertMasks(0, 0, boostsAt(5 * HOUR - 1));
167 assertMasks(bit(7), 0, boostsAt(5 * HOUR));
168 assertMasks(bit(7), 0, boostsAt(OFF_PEAK_HOURS * HOUR));
170 assertMasks(bit(7), 0, boostsAt(OFF_PEAK_HOURS * HOUR + 30 * MINUTE));
176 const uint32_t start{ UINT32_MAX - 90 * MINUTE + 1 };
177 const uint32_t now{ 30 * MINUTE };
178 assertMasks(bit(6), 0x04, boostsAt(now - start));
187 assertMasks(0, 0, boostsAt(0, 2000,
false));
188 assertMasks(0, 0, boostsAt(90 * MINUTE, 2000,
false));
189 assertMasks(0, 0, boostsAt(6 * HOUR, 2000,
false));
194 assertMasks(bit(5), 0, boostsAt(0, THRESHOLD_x100));
199 assertMasks(0, 0, boostsAt(0, THRESHOLD_x100 + 1));
200 assertMasks(0, 0, boostsAt(90 * MINUTE, THRESHOLD_x100 + 1));
205 assertMasks(bit(5), 0, boostsAt(0, THRESHOLD_x100 - 1));
206 assertMasks(bit(5), 0, boostsAt(0, -500));
229 assertMasks(bit(5) | bit(6), 0x04, boostsAt(90 * MINUTE));
235 uint8_t highest{ 0 };
242 TEST_ASSERT_EQUAL_UINT8(2, reads);
243 TEST_ASSERT_EQUAL_UINT8(1, highest);
244 assertMasks(bit(5), 0, masks);
251 TEST_FAIL_MESSAGE(
"no boost to read");
252 return DualTariff::Boost{ 0, UINT32_MAX, 0xFFFF, 0xFF };
254 assertMasks(0, 0, masks);
259 const auto fail{ [](
const uint8_t)
261 TEST_FAIL_MESSAGE(
"no boost to read");
262 return DualTariff::Boost{ 0, UINT32_MAX, 0xFFFF, 0xFF };
281 assertMasks(bit(5) | bit(6), 0x05, pinsOnly(bit(12)) | boostsAt(90 * MINUTE));
283 assertMasks(bit(5) | bit(6) | bit(7), 0, pinsOnly(bit(4)) | boostsAt(6 * HOUR));
288 assertMasks(bit(5), 0x02, pinsOnly(bit(3)) | boostsAt(90 * MINUTE, 2000,
false));
289 assertMasks(bit(5), 0x02, pinsOnly(bit(3)) | boostsAt(90 * MINUTE, THRESHOLD_x100 + 1));
The scheduled boosts: a window and the bitmasks of its targets, for each entry.
Manages override pins and their associated bitmasks for forced operation.
constexpr uint8_t REMOTE_PIN_BASE
Base value for virtual pins representing remote loads.
constexpr BoostLength FOR(const Minutes m)
A boost lasting m.
constexpr BoostWindow FIRST(const Minutes d)
The first d of the off-peak period.
constexpr BoostWindow LAST(const Minutes d)
The last d of the off-peak period, until the end.
constexpr BoostStart FROM_START(const Minutes m)
A start m after the beginning of the off-peak period.
constexpr int16_t DEVICE_DISCONNECTED_RAW
constexpr int16_t OUTOFRANGE_TEMPERATURE
DualTariff::Boost readBoost(const uint8_t i)
Reads scheduled boost i from flash.
constexpr Boost resolve(const BoostSchedule< N, MaxPins > &boosts, const uint8_t i, const uint8_t offPeakHours)
Boost i of boosts, for an off-peak period of offPeakHours.
OverrideMasks boostForcing(const bool bOffPeak, const uint32_t elapsed, const int16_t temperature_x100, const int16_t threshold_x100, const uint8_t count, ReadBoost readBoost)
The targets of the scheduled boosts whose window is open.
OverrideMasks pinForcing(const Pins &pins, IsActive isActive)
The targets of the override pins that are active (held LOW).
constexpr uint8_t remote(uint8_t unit, uint8_t ledPin=0)
Declares a load driven by a remote unit over RF.
void test_overlapping_boosts_add_up(void)
void test_first_boost_is_open_from_the_start(void)
void test_no_boost_between_the_windows(void)
void test_no_boost_at_all(void)
void test_an_out_of_range_reading_stops_the_boost(void)
void test_only_the_override_pins_count(void)
void test_no_boost_above_the_threshold(void)
void test_boost_window_closes_at_its_end(void)
void test_each_pin_is_read_once(void)
void test_pins_and_boosts_add_up(void)
void test_a_dead_probe_lets_the_boost_run(void)
void test_an_active_pin_forces_its_targets(void)
void test_boost_window_opens_at_its_start(void)
void test_no_active_pin_forces_nothing(void)
void test_several_active_pins_add_up(void)
void test_only_the_boosts_of_the_count_are_read(void)
void test_boost_at_the_threshold(void)
void test_operator_or_joins_both_masks(void)
void test_last_boost_lasts_until_the_meter_ends_off_peak(void)
void test_boost_across_the_millis_wraparound(void)
void test_the_table_is_not_read_outside_off_peak_or_when_too_hot(void)
void test_pins_force_outside_off_peak_and_when_too_hot(void)
void test_boost_below_the_threshold(void)
void test_no_boost_outside_off_peak(void)
void test_a_pin_forces_its_remote_targets(void)
The forced outputs: the override pins, and the scheduled boosts of the dual tariff.
Holds both local and remote override bitmasks.
#define HOURS(value)
A compile-time number of hours, as MINUTES().
Compile-time utilities for managing override pins and index-to-bitmask mapping.