3-phase PV router
Loading...
Searching...
No Matches
rotation_groups.h
Go to the documentation of this file.
1
20
21#ifndef ROTATION_GROUPS_H
22#define ROTATION_GROUPS_H
23
24#include <stdint.h>
25
26#include "config_types.h"
27#include "load_map.h"
28
29namespace Rotation
30{
32constexpr uint8_t countLoads(uint32_t mask)
33{
34 uint8_t count{ 0 };
35 for (; mask; mask &= mask - 1)
36 {
37 ++count;
38 }
39 return count;
40}
41
54template< uint8_t NL >
55constexpr uint32_t loadsOf(const uint8_t code, const uint8_t (&loadMap)[NL])
56{
57 static_assert(NL <= 32, "the load numbers of the rotation groups fit 32 bits");
58
60 {
61 const uint8_t loadNum = code - TargetCode::LOAD;
62 return loadNum < NL ? (1UL << loadNum) : 0;
63 }
65 {
66 const uint8_t loadNum = code - TargetCode::LOCAL_LOAD;
67 return loadNum < NL && Load::isLocal(loadMap[loadNum]) ? (1UL << loadNum) : 0;
68 }
70 {
71 return 0;
72 }
73
74 uint32_t loads{ 0 };
76 {
77 for (uint8_t i = 0; i != NL; ++i)
78 {
79 const bool local = Load::isLocal(loadMap[i]);
80 if (code == TargetCode::ALL_LOADS || local == (code == TargetCode::ALL_LOCAL_LOADS))
81 {
82 loads |= 1UL << i;
83 }
84 }
85 return loads;
86 }
88 {
89 return 0; // ALL_RELAYS(), ALL_LOADS_AND_RELAYS()
90 }
91
92 if (code >= REMOTE_PIN_BASE)
93 {
94 uint8_t ordinal = code - REMOTE_PIN_BASE;
95 for (uint8_t i = 0; i != NL; ++i)
96 {
97 if (!Load::isLocal(loadMap[i]) && ordinal-- == 0)
98 {
99 return 1UL << i;
100 }
101 }
102 return 0;
103 }
104
105 for (uint8_t i = 0; i != NL; ++i)
106 {
107 if (Load::isLocal(loadMap[i]) && Load::pinOf(loadMap[i]) == code)
108 {
109 return 1UL << i;
110 }
111 }
112 return 0;
113}
114
116template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
117constexpr uint32_t groupLoads(const RotationGroups< NG, MaxPins >& groups, const uint8_t g, const uint8_t (&loadMap)[NL])
118{
119 uint32_t loads{ 0 };
120 const auto& targets = groups.getGroup(g);
121 for (uint8_t i = 0; i != targets.count; ++i)
122 {
123 loads |= loadsOf(targets.pins[i], loadMap);
124 }
125 return loads;
126}
127
129template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
130constexpr bool targetsAreLoads(const RotationGroups< NG, MaxPins >& groups, const uint8_t (&loadMap)[NL])
131{
132 for (uint8_t g = 0; g != groups.size(); ++g)
133 {
134 const auto& targets = groups.getGroup(g);
135 for (uint8_t i = 0; i != targets.count; ++i)
136 {
137 if (!loadsOf(targets.pins[i], loadMap))
138 {
139 return false;
140 }
141 }
142 }
143 return true;
144}
145
147template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
148constexpr bool groupsHaveTwoLoads(const RotationGroups< NG, MaxPins >& groups, const uint8_t (&loadMap)[NL])
149{
150 for (uint8_t g = 0; g != groups.size(); ++g)
151 {
152 if (countLoads(groupLoads(groups, g, loadMap)) < 2)
153 {
154 return false;
155 }
156 }
157 return true;
158}
159
161template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
162constexpr bool noLoadTwice(const RotationGroups< NG, MaxPins >& groups, const uint8_t (&loadMap)[NL])
163{
164 uint32_t seen{ 0 };
165 for (uint8_t g = 0; g != groups.size(); ++g)
166 {
167 const auto& targets = groups.getGroup(g);
168 for (uint8_t i = 0; i != targets.count; ++i)
169 {
170 const uint32_t loads = loadsOf(targets.pins[i], loadMap);
171 if (seen & loads)
172 {
173 return false;
174 }
175 seen |= loads;
176 }
177 }
178 return true;
179}
180
182template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
183constexpr bool groupsAreConsistent(const RotationGroups< NG, MaxPins >& groups, const uint8_t (&loadMap)[NL])
184{
185 for (uint8_t g = 0; g != groups.size(); ++g)
186 {
187 const uint32_t loads = groupLoads(groups, g, loadMap);
188 bool first{ true };
189 uint8_t unit{ 0 };
190 for (uint8_t i = 0; i != NL; ++i)
191 {
192 if (!(loads & (1UL << i)))
193 {
194 continue;
195 }
196 if (first)
197 {
198 unit = Load::unitOf(loadMap[i]);
199 first = false;
200 }
201 else if (Load::unitOf(loadMap[i]) != unit)
202 {
203 return false;
204 }
205 }
206 }
207 return true;
208}
209
221template< uint8_t NG, uint8_t NL >
222class Table
223{
224public:
230 template< uint8_t MaxPins >
231 constexpr Table(const RotationGroups< NG, MaxPins >& groups, const uint8_t (&loadMap)[NL], const uint8_t (&priorities)[NL])
232 : places_{}, ends_{}, loads_{}
233 {
234 uint8_t k{ 0 };
235 for (uint8_t g = 0; g != NG; ++g)
236 {
237 loads_[g] = groupLoads(groups, g, loadMap);
238 for (uint8_t p = 0; p != NL; ++p)
239 {
240 if (priorities[p] < 32 && (loads_[g] & (1UL << priorities[p])) && k < NL)
241 {
242 places_[k++] = p;
243 }
244 }
245 ends_[g] = k;
246 }
247 }
248
250 constexpr uint8_t size() const
251 {
252 return NG;
253 }
254
256 constexpr uint32_t loads(const uint8_t g) const
257 {
258 return loads_[g];
259 }
260
262 template< uint8_t N >
263 __attribute__((always_inline)) inline void rotate(uint8_t (&prio)[N]) const
264 {
265 uint8_t start{ 0 };
266 for (uint8_t g = 0; g != NG; ++g)
267 {
268 const uint8_t end{ ends_[g] };
269 if (end - start >= 2)
270 {
271 const uint8_t last{ prio[places_[end - 1]] };
272 for (uint8_t i = end - 1; i != start; --i)
273 {
274 prio[places_[i]] = prio[places_[i - 1]];
275 }
276 prio[places_[start]] = last;
277 }
278 start = end;
279 }
280 }
281
282private:
283 uint8_t places_[NL];
284 uint8_t ends_[NG];
285 uint32_t loads_[NG];
286};
287
289template< uint8_t NL >
290class Table< 0, NL >
291{
292public:
293 template< uint8_t MaxPins >
294 constexpr Table(const RotationGroups< 0, MaxPins >& /* groups */, const uint8_t (& /* loadMap */)[NL],
295 const uint8_t (& /* priorities */)[NL])
296 {
297 }
298
300 constexpr uint8_t size() const
301 {
302 return 0;
303 }
304
306 constexpr uint32_t loads(uint8_t) const
307 {
308 return 0;
309 }
310
312 template< uint8_t N >
313 void rotate(uint8_t (& /* prio */)[N]) const
314 {
315 }
316};
317
319template< uint8_t NG, uint8_t MaxPins, uint8_t NL >
320Table(const RotationGroups< NG, MaxPins >&, const uint8_t (&)[NL], const uint8_t (&)[NL]) -> Table< NG, NL >;
321} // namespace Rotation
322
323#endif /* ROTATION_GROUPS_H */
The rotation groups, as in config.h: the loads that take turns at the top of the priority list,...
constexpr uint8_t size() const
Number of groups.
constexpr const PinList< MaxPins > & getGroup(const uint8_t index) const
The targets of group index, as codes.
void rotate(uint8_t(&)[N]) const
Nothing to rotate.
constexpr uint8_t size() const
Number of groups: none.
constexpr uint32_t loads(uint8_t) const
No loads: never called, there for the loops over size().
constexpr Table(const RotationGroups< 0, MaxPins > &, const uint8_t(&)[NL], const uint8_t(&)[NL])
The rotation groups, resolved: the priority places each group holds.
uint8_t places_[NL]
constexpr Table(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL], const uint8_t(&priorities)[NL])
constexpr uint8_t size() const
Number of groups.
uint32_t loads_[NG]
void rotate(uint8_t(&prio)[N]) const
Rotates each group by one among its places; each entry keeps its ON bit.
constexpr uint32_t loads(const uint8_t g) const
The loads of group g: bit n set for load n.
What config.h is written with: its types, constants and helpers.
constexpr uint8_t REMOTE_PIN_BASE
Base value for virtual pins representing remote loads.
uint8_t i
Encoding of the load map: one packed byte per dump load.
constexpr bool isLocal(uint8_t entry)
True if the entry describes a locally driven load.
Definition load_map.h:89
constexpr uint8_t pinOf(uint8_t entry)
The pin of the entry: TRIAC pin if local, status LED (0 = none) if remote.
Definition load_map.h:101
constexpr uint8_t unitOf(uint8_t entry)
The owning unit of the entry: 0 for a local load, 1..3 for a remote one.
Definition load_map.h:95
constexpr bool noLoadTwice(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
No load is named twice, within a group or across groups.
constexpr bool targetsAreLoads(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
Every code of every group stands for at least one load.
Table(const RotationGroups< NG, MaxPins > &, const uint8_t(&)[NL], const uint8_t(&)[NL]) -> Table< NG, NL >
Deduction guide: the groups and the loads from the arguments.
constexpr uint32_t loadsOf(const uint8_t code, const uint8_t(&loadMap)[NL])
The loads a target code stands for, by load number.
constexpr bool groupsHaveTwoLoads(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
Every group holds at least 2 loads.
constexpr uint8_t countLoads(uint32_t mask)
Number of bits set in mask.
constexpr bool groupsAreConsistent(const RotationGroups< NG, MaxPins > &groups, const uint8_t(&loadMap)[NL])
In each group, every load is local, or every load is on the same remote unit.
constexpr uint32_t groupLoads(const RotationGroups< NG, MaxPins > &groups, const uint8_t g, const uint8_t(&loadMap)[NL])
The loads of group g, all its codes together.
constexpr uint8_t RELAY
constexpr uint8_t MAX_LOCAL_LOADS
constexpr uint8_t MAX_LOADS
constexpr uint8_t LOAD
constexpr uint8_t ALL_LOADS
constexpr uint8_t ALL_LOCAL_LOADS
constexpr uint8_t MAX_RELAYS
constexpr uint8_t ALL_REMOTE_LOADS
constexpr uint8_t LOCAL_LOAD