3-phase PV router
Loading...
Searching...
No Matches
temp_schedule.h
Go to the documentation of this file.
1
47
48#ifndef TEMP_SCHEDULE_H
49#define TEMP_SCHEDULE_H
50
51#include <stdint.h>
52
53namespace TempSchedule
54{
56enum class Step : uint8_t
57{
63};
64
66struct Action
67{
69 uint8_t sensor;
70};
71
72inline constexpr uint8_t STEPS_PER_SENSOR{ 3 };
73
74inline constexpr uint16_t CONVERSION_MS{ 750 };
75
84constexpr uint16_t conversionCycles(const uint8_t supplyFrequency)
85{
86 return static_cast< uint16_t >((static_cast< uint32_t >(CONVERSION_MS) * supplyFrequency + 999) / 1000 + 1);
87}
88
95constexpr uint16_t roundCycles(const uint8_t noOfSensors, const uint8_t supplyFrequency)
96{
97 return static_cast< uint16_t >(conversionCycles(supplyFrequency) + STEPS_PER_SENSOR * noOfSensors);
98}
99
112constexpr bool fitsInPeriod(const uint8_t noOfSensors, const uint8_t supplyFrequency, const uint16_t periodCycles)
113{
114 const uint32_t cycles{ roundCycles(noOfSensors, supplyFrequency) };
115 return cycles + cycles / supplyFrequency + 2 <= periodCycles;
116}
117
128template< uint8_t N, uint16_t CONVERSION_CYCLES >
130{
131 static_assert(N > 0, "no sensor");
132
133 bool converting{ false };
134 uint16_t sinceConvert{ 0 };
135 uint8_t sensor{ 0 };
136 uint8_t sub{ 0 };
137
138public:
145 Action next(const bool onTime)
146 {
147 if (!converting)
148 {
149 if (!onTime)
150 {
151 return Action{ Step::NONE, 0 };
152 }
153 converting = true;
154 sinceConvert = 0;
155 return Action{ Step::CONVERT, 0 };
156 }
157 if (N == sensor)
158 {
159 return Action{ Step::NONE, 0 }; // round over
160 }
161 if (sinceConvert < CONVERSION_CYCLES)
162 {
163 ++sinceConvert; // the conversion runs in the sensors, late cycles or not
164 }
165 if (sinceConvert < CONVERSION_CYCLES || !onTime)
166 {
167 return Action{ Step::NONE, 0 };
168 }
169 Action action{ Step::ADDRESS, sensor };
170 if (1 == sub)
171 {
172 action.step = Step::READ;
173 }
174 else if (2 == sub)
175 {
176 action.step = Step::CLOSE;
177 }
178 if (++sub == STEPS_PER_SENSOR)
179 {
180 sub = 0;
181 ++sensor;
182 }
183 return action;
184 }
185
191 void skipRestOf(const uint8_t idx)
192 {
193 if (converting && idx == sensor && N != sensor)
194 {
195 sub = 0;
196 ++sensor;
197 }
198 }
199
210 {
211 if (!done())
212 {
213 return false;
214 }
215 converting = false;
216 sensor = 0;
217 sub = 0;
218 return true;
219 }
220
222 bool done() const
223 {
224 return converting && N == sensor;
225 }
226};
227} // namespace TempSchedule
228
229#endif /* TEMP_SCHEDULE_H */
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.
constexpr uint16_t roundCycles(const uint8_t noOfSensors, const uint8_t supplyFrequency)
The mains cycles of a round on time: from the conversion to the last CLOSE.
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.
constexpr uint8_t STEPS_PER_SENSOR
constexpr uint16_t CONVERSION_MS
The transaction to run, and the sensor it reads (ADDRESS, READ, CLOSE).