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
11
12#include <unity.h>
13
14#include "override_masks.h" // Real header - pure C++
15#include "utils_override.h" // OverrideTable
16
17namespace
18{
19constexpr uint32_t MINUTE{ 60UL * 1000UL };
20constexpr uint32_t HOUR{ 60UL * MINUTE };
21
22constexpr uint8_t OFF_PEAK_HOURS{ 8 };
23constexpr int16_t THRESHOLD_x100{ 6000 }; // 60 °C
24
25// The same values as constants.h, which needs Arduino.h
26constexpr int16_t DEVICE_DISCONNECTED_RAW{ -7040 };
27constexpr int16_t OUTOFRANGE_TEMPERATURE{ 30200 };
28
29constexpr uint8_t remote(const uint8_t n)
30{
31 return REMOTE_PIN_BASE + n;
32}
33
34// Override pins: D3 forces D5 and remote load 1, D4 forces D5 and D6, D12 forces remote load 0
35constexpr OverrideTable pins{ { { 3, { 5, remote(1) } },
36 { 4, { 5, 6 } },
37 { 12, { remote(0) } } } };
38
39// Boosts, over 8 hours of off-peak:
40// #0 the first 2 h: D5
41// #1 from 1 h, for 2 h (1 h to 3 h): D6 and remote load 2
42// #2 the last 3 h (5 h to the end): D7
43constexpr BoostSchedule boosts{ { { FIRST(HOURS(2)), { 5 } },
44 { FROM_START(HOURS(1)), FOR(HOURS(2)), { 6, remote(2) } },
45 { LAST(HOURS(3)), { 7 } } } };
46
47constexpr uint16_t bit(const uint8_t n)
48{
49 return static_cast< uint16_t >(1U << n);
50}
51
53auto activeIn(const uint16_t activePins)
54{
55 return [activePins](const uint8_t pin)
56 {
57 return (activePins & bit(pin)) != 0;
58 };
59}
60
62DualTariff::Boost readBoost(const uint8_t i)
63{
64 return DualTariff::resolve(boosts, i, OFF_PEAK_HOURS);
65}
66
67OverrideMasks pinsOnly(const uint16_t activePins)
68{
69 return Forcing::pinForcing(pins, activeIn(activePins));
70}
71
72OverrideMasks boostsAt(const uint32_t elapsed, const int16_t temperature_x100 = 2000, const bool bOffPeak = true)
73{
74 return Forcing::boostForcing(bOffPeak, elapsed, temperature_x100, THRESHOLD_x100, boosts.size(), readBoost);
75}
76
77void assertMasks(const uint16_t local, const uint8_t remoteMask, const OverrideMasks& masks)
78{
79 TEST_ASSERT_EQUAL_HEX16(local, masks.local);
80 TEST_ASSERT_EQUAL_HEX8(remoteMask, masks.remote);
81}
82} // namespace
83
84void setUp(void) {}
85void tearDown(void) {}
86
87// ============================================================================
88// Override pins
89// ============================================================================
90
92{
93 assertMasks(0, 0, pinsOnly(0));
94}
95
97{
98 assertMasks(bit(5) | bit(6), 0, pinsOnly(bit(4)));
99}
100
102{
103 assertMasks(0, 0x01, pinsOnly(bit(12)));
104 assertMasks(bit(5), 0x02, pinsOnly(bit(3)));
105}
106
108{
109 // D2, D5, D13 active, but none is an override pin
110 assertMasks(0, 0, pinsOnly(bit(2) | bit(5) | bit(13)));
111}
112
114{
115 // D3 and D4 share D5
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)));
118}
119
121{
122 uint16_t readPins{ 0 };
123 uint8_t reads{ 0 };
124 Forcing::pinForcing(pins, [&](const uint8_t pin)
125 {
126 readPins |= bit(pin);
127 ++reads;
128 return false;
129 });
130 TEST_ASSERT_EQUAL_UINT8(3, reads);
131 TEST_ASSERT_EQUAL_HEX16(bit(3) | bit(4) | bit(12), readPins);
132}
133
134// ============================================================================
135// Scheduled boosts: the windows
136// ============================================================================
137
139{
140 // #1 opens at 1 h: just before, only #0
141 assertMasks(bit(5), 0, boostsAt(HOUR - 1));
142 assertMasks(bit(5) | bit(6), 0x04, boostsAt(HOUR));
143}
144
146{
147 // #0 closes at 2 h, #1 at 3 h
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));
152}
153
155{
156 assertMasks(bit(5), 0, boostsAt(0));
157}
158
160{
161 assertMasks(0, 0, boostsAt(4 * HOUR));
162 assertMasks(0, 0, boostsAt(5 * HOUR - 1));
163}
164
166{
167 assertMasks(bit(7), 0, boostsAt(5 * HOUR));
168 assertMasks(bit(7), 0, boostsAt(OFF_PEAK_HOURS * HOUR));
169 // the meter is late: the boost goes on until it switches to peak
170 assertMasks(bit(7), 0, boostsAt(OFF_PEAK_HOURS * HOUR + 30 * MINUTE));
171}
172
174{
175 // off-peak started 90 min before millis() wrapped around: elapsed = now - start, unsigned
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));
179}
180
181// ============================================================================
182// Scheduled boosts: off-peak and temperature
183// ============================================================================
184
186{
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));
190}
191
193{
194 assertMasks(bit(5), 0, boostsAt(0, THRESHOLD_x100));
195}
196
198{
199 assertMasks(0, 0, boostsAt(0, THRESHOLD_x100 + 1));
200 assertMasks(0, 0, boostsAt(90 * MINUTE, THRESHOLD_x100 + 1));
201}
202
204{
205 assertMasks(bit(5), 0, boostsAt(0, THRESHOLD_x100 - 1));
206 assertMasks(bit(5), 0, boostsAt(0, -500)); // frost: still a valid reading
207}
208
210{
211 // DEVICE_DISCONNECTED_RAW (no answer, or a CRC error) counts as cold: the heater's thermostat
212 // stays the safety limit
213 assertMasks(bit(5) | bit(6), 0x04, boostsAt(90 * MINUTE, DEVICE_DISCONNECTED_RAW));
214}
215
217{
218 // OUTOFRANGE_TEMPERATURE (302 °C) is above any threshold
219 assertMasks(0, 0, boostsAt(90 * MINUTE, OUTOFRANGE_TEMPERATURE));
220}
221
222// ============================================================================
223// Scheduled boosts: the table
224// ============================================================================
225
227{
228 // 1 h 30: #0 and #1, #1 forces a local and a remote target
229 assertMasks(bit(5) | bit(6), 0x04, boostsAt(90 * MINUTE));
230}
231
233{
234 uint8_t reads{ 0 };
235 uint8_t highest{ 0 };
236 const auto masks{ Forcing::boostForcing(true, 0, 0, THRESHOLD_x100, 2, [&](const uint8_t i)
237 {
238 ++reads;
239 highest = i;
240 return readBoost(i);
241 }) };
242 TEST_ASSERT_EQUAL_UINT8(2, reads);
243 TEST_ASSERT_EQUAL_UINT8(1, highest);
244 assertMasks(bit(5), 0, masks);
245}
246
248{
249 const auto masks{ Forcing::boostForcing(true, 0, 0, THRESHOLD_x100, 0, [](const uint8_t)
250 {
251 TEST_FAIL_MESSAGE("no boost to read");
252 return DualTariff::Boost{ 0, UINT32_MAX, 0xFFFF, 0xFF };
253 }) };
254 assertMasks(0, 0, masks);
255}
256
258{
259 const auto fail{ [](const uint8_t)
260 {
261 TEST_FAIL_MESSAGE("no boost to read");
262 return DualTariff::Boost{ 0, UINT32_MAX, 0xFFFF, 0xFF };
263 } };
264 assertMasks(0, 0, Forcing::boostForcing(false, 0, 0, THRESHOLD_x100, 3, fail));
265 assertMasks(0, 0, Forcing::boostForcing(true, 0, THRESHOLD_x100 + 1, THRESHOLD_x100, 3, fail));
266}
267
268// ============================================================================
269// Pins and boosts together
270// ============================================================================
271
273{
274 assertMasks(bit(2) | bit(9), 0x81, OverrideMasks{ bit(2), 0x01 } | OverrideMasks{ bit(9), 0x80 });
275 assertMasks(bit(4), 0x10, OverrideMasks{ bit(4), 0x10 } | OverrideMasks{ bit(4), 0x10 });
276}
277
279{
280 // D12 (remote load 0) and, at 1 h 30, boosts #0 and #1
281 assertMasks(bit(5) | bit(6), 0x05, pinsOnly(bit(12)) | boostsAt(90 * MINUTE));
282 // D4 (D5, D6) and the last boost (D7)
283 assertMasks(bit(5) | bit(6) | bit(7), 0, pinsOnly(bit(4)) | boostsAt(6 * HOUR));
284}
285
287{
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));
290}
291
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
Definition ds18b20.h:38
constexpr int16_t OUTOFRANGE_TEMPERATURE
Definition ds18b20.h:34
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
uint8_t i
DualTariff::Boost readBoost(const uint8_t i)
Reads scheduled boost i from flash.
Definition dualtariff.h:62
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.
Definition load_map.h:79
int main(void)
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)
Definition test_main.cpp:96
void test_boost_window_opens_at_its_start(void)
void test_no_active_pin_forces_nothing(void)
Definition test_main.cpp:91
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.