33 constexpr Step kinds[]{ Step::ADDRESS, Step::READ, Step::CLOSE };
34 return Action{ kinds[k % 3],
static_cast< uint8_t
>(k / 3) };
37void assertAction(
const Action expected,
const Action actual,
const char* msg)
39 TEST_ASSERT_EQUAL_UINT8_MESSAGE(
static_cast< uint8_t
>(expected.
step),
static_cast< uint8_t
>(actual.
step), msg);
40 if (expected.
step != Step::NONE)
42 TEST_ASSERT_EQUAL_UINT8_MESSAGE(expected.
sensor, actual.
sensor, msg);
55 TEST_ASSERT_EQUAL_UINT16(39, C50);
56 TEST_ASSERT_EQUAL_UINT16(46, C60);
57 TEST_ASSERT_TRUE(C50 * 1000UL / 50 >= 760);
58 TEST_ASSERT_TRUE(C60 * 1000UL / 60 >= 760);
69 assertAction({ Step::CONVERT, 0 }, schedule.
next(
true),
"first cycle: the conversion");
70 for (uint16_t
i = 1;
i != C50; ++
i)
72 assertAction({ Step::NONE, 0 }, schedule.
next(
true),
"during the conversion: nothing");
74 for (uint16_t k = 0; k != 6; ++k)
76 assertAction(readStep(k), schedule.
next(
true),
"the reads, one transaction per cycle");
78 TEST_ASSERT_TRUE(schedule.
done());
79 for (uint16_t
i = 0;
i != 300; ++
i)
81 assertAction({ Step::NONE, 0 }, schedule.
next(
true),
"round over: nothing until the next period");
88 uint16_t
cycle{ 0 }, convertAt{ 0 }, addressAt{ 0 };
92 if (a.step == Step::CONVERT)
96 if (a.step == Step::ADDRESS)
102 TEST_ASSERT_EQUAL_UINT16(C60, addressAt - convertAt);
109 assertAction({ Step::NONE, 0 }, schedule.
next(
false),
"late: no conversion");
110 assertAction({ Step::CONVERT, 0 }, schedule.
next(
true),
"on time: the conversion");
111 for (uint16_t
i = 1;
i != C50; ++
i)
115 assertAction({ Step::NONE, 0 }, schedule.
next(
false),
"late: the read waits");
116 assertAction({ Step::ADDRESS, 0 }, schedule.
next(
true),
"");
117 assertAction({ Step::NONE, 0 }, schedule.
next(
false),
"late: the read waits");
118 assertAction({ Step::READ, 0 }, schedule.
next(
true),
"");
119 assertAction({ Step::CLOSE, 0 }, schedule.
next(
true),
"");
120 TEST_ASSERT_TRUE(schedule.
done());
128 for (uint16_t
i = 1;
i != C50; ++
i)
130 schedule.
next(
false);
132 assertAction({ Step::ADDRESS, 0 }, schedule.
next(
true),
"conversion time over");
139 for (uint16_t
i = 1;
i != C50; ++
i)
143 assertAction({ Step::ADDRESS, 0 }, schedule.
next(
true),
"");
145 assertAction({ Step::ADDRESS, 1 }, schedule.
next(
true),
"the next sensor, on the next cycle");
146 assertAction({ Step::READ, 1 }, schedule.
next(
true),
"");
148 assertAction({ Step::CLOSE, 1 }, schedule.
next(
true),
"");
149 assertAction({ Step::ADDRESS, 2 }, schedule.
next(
true),
"");
151 TEST_ASSERT_TRUE(schedule.
done());
152 assertAction({ Step::NONE, 0 }, schedule.
next(
true),
"the last sensor skipped: round over");
158 for (uint16_t
i = 0;
i != C50 + 3; ++
i)
162 TEST_ASSERT_TRUE(schedule.
done());
164 TEST_ASSERT_FALSE(schedule.
done());
165 assertAction({ Step::CONVERT, 0 }, schedule.
next(
true),
"a new round");
174 for (uint16_t
i = 0;
i != C50 + 3; ++
i)
178 assertAction({ Step::NONE, 0 }, schedule.
next(
true),
"the cycle that ends the period: nothing");
180 assertAction({ Step::CONVERT, 0 }, schedule.
next(
true),
"the conversion, in that same cycle");
181 for (uint16_t
i = 1;
i != C50; ++
i)
183 assertAction({ Step::NONE, 0 }, schedule.
next(
true),
"during the conversion: nothing");
185 assertAction({ Step::ADDRESS, 0 }, schedule.
next(
true),
"");
193 for (uint16_t
i = 0;
i != C50 + 1; ++
i)
198 assertAction({ Step::READ, 0 }, schedule.
next(
true),
"the round goes on");
205 uint32_t lfsr{ 0xACE1U };
206 int32_t lastConvert{ -100000 };
207 uint16_t rounds{ 0 };
210 lfsr = (lfsr >> 1) ^ (-(lfsr & 1U) & 0xB400U);
211 const bool onTime{ (lfsr & 7U) != 0 };
212 if (
cycle % 250 == 0)
217 if (a.step != Step::NONE)
219 TEST_ASSERT_TRUE_MESSAGE(onTime,
"a transaction on a late cycle");
221 if (a.step == Step::CONVERT)
226 if (a.step == Step::ADDRESS || a.step == Step::READ)
228 TEST_ASSERT_TRUE_MESSAGE(
cycle - lastConvert >= C50,
"a read before the end of the conversion");
231 TEST_ASSERT_EQUAL_UINT16(80, rounds);
245template< u
int8_t N, u
int8_t F >
251 for (uint16_t tick = 1; tick != periodCycles; ++tick)
253 const bool onTime{ (tick % F) != 1 };
254 schedule.
next(onTime);
The round of the sensors, one transaction per mains cycle.
bool done() const
Whether the round is over.
bool newPeriod()
A datalog period begins: a new round, once this one is over.
Action next(const bool onTime)
The transaction of this mains cycle: called once per cycle, when the loop sees it.
void skipRestOf(const uint8_t idx)
The sensor did not answer its ADDRESS: its other transactions are skipped.
constexpr uint16_t conversionCycles(const uint8_t supplyFrequency)
The mains cycles from the conversion to the first read.
Step
The 1-Wire transaction of a mains cycle.
constexpr bool fitsInPeriod(const uint8_t noOfSensors, const uint8_t supplyFrequency, const uint16_t periodCycles)
Whether a round ends within its datalog period.
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_new_period_continues_an_unfinished_round()
void test_never_two_transactions_in_one_cycle_and_never_before_the_conversion_time()
void test_first_read_conversion_cycles_after_the_conversion()
void test_round_of_two_sensors()
void test_conversion_in_the_cycle_that_ends_the_period()
void test_new_period_restarts_a_finished_round()
void test_conversion_cycles_cover_750_ms()
void test_late_cycles_still_count_for_the_conversion()
void test_rounds_fit_their_period()
void test_late_cycles_postpone_without_losing_a_step()
void test_a_sensor_that_does_not_answer_is_skipped()
static bool roundEndsInPeriod(const uint16_t periodCycles)
A datalog period of the main loop: the round must be over before the cycle that ends the period,...
The transaction to run, and the sensor it reads (ADDRESS, READ, CLOSE).
Which 1-Wire transaction of the temperature sensors runs on which mains cycle.