3-phase PV router
Loading...
Searching...
No Matches
load_map.h
Go to the documentation of this file.
1
24
25#ifndef LOAD_MAP_H
26#define LOAD_MAP_H
27
28#include <stdint.h>
29
31enum class LoadStates : uint8_t
32{
35};
36// enum loadStates {LOAD_ON, LOAD_OFF}; /**< for use if loads are active low (original PCB) */
37
38inline constexpr uint8_t loadTypeMask{ 0xC0 };
39inline constexpr uint8_t loadPinMask{ 0x3F };
40inline constexpr uint8_t loadTypeShift{ 6 };
41
42inline constexpr uint8_t MAX_REMOTE_UNITS{ 3 };
43inline constexpr uint8_t MAX_LOADS_PER_UNIT{ 8 };
44
57inline constexpr uint8_t invalidRemoteUnitEntry{ 0xFE };
58
62namespace Load
63{
68constexpr uint8_t local(uint8_t pin)
69{
70 return pin & loadPinMask;
71}
72
79constexpr uint8_t remote(uint8_t unit, uint8_t ledPin = 0)
80{
81 if ((unit == 0) || (unit > MAX_REMOTE_UNITS))
82 {
84 }
85 return static_cast< uint8_t >(static_cast< uint8_t >(unit << loadTypeShift) | (ledPin & loadPinMask));
86}
87
89constexpr bool isLocal(uint8_t entry)
90{
91 return (entry & loadTypeMask) == 0;
92}
93
95constexpr uint8_t unitOf(uint8_t entry)
96{
97 return static_cast< uint8_t >((entry & loadTypeMask) >> loadTypeShift);
98}
99
101constexpr uint8_t pinOf(uint8_t entry)
102{
103 return entry & loadPinMask;
104}
105
109template< uint8_t N >
110constexpr uint8_t countUnits(const uint8_t (&map)[N])
111{
112 uint8_t highest{ 0 };
113 for (uint8_t i = 0; i < N; ++i)
114 {
115 if (unitOf(map[i]) > highest)
116 {
117 highest = unitOf(map[i]);
118 }
119 }
120 return highest;
121}
122
126template< uint8_t N >
127constexpr uint8_t countRemoteLoads(const uint8_t (&map)[N])
128{
129 uint8_t count{ 0 };
130 for (uint8_t i = 0; i < N; ++i)
131 {
132 if (!isLocal(map[i]))
133 {
134 ++count;
135 }
136 }
137 return count;
138}
139
143template< uint8_t N >
144constexpr uint8_t maxLoadsPerUnit(const uint8_t (&map)[N])
145{
146 uint8_t worst{ 0 };
147 for (uint8_t unit = 1; unit <= countUnits(map); ++unit)
148 {
149 uint8_t count{ 0 };
150 for (uint8_t i = 0; i < N; ++i)
151 {
152 if (unitOf(map[i]) == unit)
153 {
154 ++count;
155 }
156 }
157 if (count > worst)
158 {
159 worst = count;
160 }
161 }
162 return worst;
163}
164
175template< uint8_t N >
176constexpr uint8_t remoteOrdinal(const uint8_t (&map)[N], uint8_t loadNum)
177{
178 uint8_t ordinal{ 0 };
179 for (uint8_t i = 0; i < loadNum; ++i)
180 {
181 if (!isLocal(map[i]))
182 {
183 ++ordinal;
184 }
185 }
186 return ordinal;
187}
188
194template< uint8_t N >
195constexpr uint8_t bitOf(const uint8_t (&map)[N], uint8_t loadNum)
196{
197 uint8_t bit{ 0 };
198 for (uint8_t i = 0; i < loadNum; ++i)
199 {
200 if (unitOf(map[i]) == unitOf(map[loadNum]))
201 {
202 ++bit;
203 }
204 }
205 return bit;
206}
207
214template< uint8_t N >
216{
217 uint16_t local[N];
218 uint8_t remote[N];
219};
220
229template< uint8_t N >
230constexpr OverrideMasks< N > overrideMasks(const uint8_t (&map)[N])
231{
232 OverrideMasks< N > masks{};
233 for (uint8_t i = 0; i < N; ++i)
234 {
235 if (isLocal(map[i]))
236 {
237 masks.local[i] = pinOf(map[i]) < 16 ? static_cast< uint16_t >(1U << pinOf(map[i])) : 0;
238 }
239 else
240 {
241 masks.remote[i] = remoteOrdinal(map, i) < 8 ? static_cast< uint8_t >(1U << remoteOrdinal(map, i)) : 0;
242 }
243 }
244 return masks;
245}
246
257template< uint8_t N >
258constexpr bool isValidMap(const uint8_t (&map)[N])
259{
260 for (const auto &entry : map)
261 {
262 const uint8_t pin{ pinOf(entry) };
263
264 if (entry == invalidRemoteUnitEntry)
265 {
266 continue; // a wrong unit, reported by hasValidRemoteUnits()
267 }
268
269 if (!isLocal(entry) && (pin == 0))
270 {
271 continue; // remote load without status LED
272 }
273
274 if ((pin < 2) || (pin > 13))
275 {
276 return false;
277 }
278 }
279 return true;
280}
281
287template< uint8_t N >
288constexpr bool hasValidRemoteUnits(const uint8_t (&map)[N])
289{
290 for (const auto &entry : map)
291 {
292 if (entry == invalidRemoteUnitEntry)
293 {
294 return false;
295 }
296 }
297 return true;
298}
299} // namespace Load
300
301#endif // LOAD_MAP_H
uint8_t i
constexpr uint8_t MAX_LOADS_PER_UNIT
Definition load_map.h:43
constexpr uint8_t invalidRemoteUnitEntry
The entry Load::remote() returns for a unit outside 1..MAX_REMOTE_UNITS.
Definition load_map.h:57
constexpr uint8_t MAX_REMOTE_UNITS
Definition load_map.h:42
constexpr uint8_t loadTypeShift
Definition load_map.h:40
LoadStates
Definition load_map.h:32
constexpr uint8_t loadPinMask
Definition load_map.h:39
constexpr uint8_t loadTypeMask
Definition load_map.h:38
Authoring and decoding helpers for the packed load map.
Definition load_map.h:63
constexpr bool isLocal(uint8_t entry)
True if the entry describes a locally driven load.
Definition load_map.h:89
constexpr uint8_t maxLoadsPerUnit(const uint8_t(&map)[N])
Largest number of loads assigned to any single remote unit.
Definition load_map.h:144
constexpr uint8_t bitOf(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a load among the loads of its own unit, in ascending map order.
Definition load_map.h:195
constexpr uint8_t local(uint8_t pin)
Declares a load driven locally by the router.
Definition load_map.h:68
constexpr uint8_t countUnits(const uint8_t(&map)[N])
Highest remote unit number appearing in the map (0 if fully local).
Definition load_map.h:110
constexpr uint8_t remoteOrdinal(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a remote load among the remote loads, in ascending map order.
Definition load_map.h:176
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 bool isValidMap(const uint8_t(&map)[N])
Check that every entry of a load map designates a usable pin.
Definition load_map.h:258
constexpr uint8_t countRemoteLoads(const uint8_t(&map)[N])
Number of entries of the map that are driven remotely.
Definition load_map.h:127
constexpr OverrideMasks< N > overrideMasks(const uint8_t(&map)[N])
Build the override bits of a load map.
Definition load_map.h:230
constexpr bool hasValidRemoteUnits(const uint8_t(&map)[N])
Check that every remote load of a map was declared with a unit from 1 to 3.
Definition load_map.h:288
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 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
Override bit of each load, in the local or in the remote override bitmask.
Definition load_map.h:216
uint16_t local[N]
Definition load_map.h:217
uint8_t remote[N]
Definition load_map.h:218