3-phase PV router
Loading...
Searching...
No Matches
utils_override.h
Go to the documentation of this file.
1
30
31#ifndef UTILS_OVERRIDE_H
32#define UTILS_OVERRIDE_H
33
34#include "type_traits.hpp"
35
36#include "config_types.h" // PinList, KeyIndexPair, OverridePins, RelayEngine, the target codes
37#include "utils_bits.h"
38#include "load_map.h"
39
40#ifdef ARDUINO
41#include "debug.h"
42#endif
43
53template< uint8_t N >
54constexpr uint8_t overridePinOf(const uint8_t (&loadMap)[N], uint8_t loadNum)
55{
56 return Load::isLocal(loadMap[loadNum])
57 ? Load::pinOf(loadMap[loadNum])
58 : static_cast< uint8_t >(REMOTE_PIN_BASE + Load::remoteOrdinal(loadMap, loadNum));
59}
60
64template< uint8_t N >
65constexpr uint32_t localLoadsMask(const uint8_t (&loadMap)[N])
66{
67 uint32_t mask{ 0 };
68 for (const auto& entry : loadMap)
69 {
70 if (Load::isLocal(entry))
71 {
72 bit_set(mask, Load::pinOf(entry));
73 }
74 }
75 return mask;
76}
77
81template< uint8_t N >
82constexpr uint32_t remoteLoadsMask(const uint8_t (&loadMap)[N])
83{
84 uint32_t mask{ 0 };
85 for (uint8_t i = 0; i != Load::countRemoteLoads(loadMap); ++i)
86 {
87 bit_set(mask, 16 + i);
88 }
89 return mask;
90}
91
92// Valid pins: 2-13, so valid mask is 0b11111111111100
93constexpr uint16_t validPinMask{ 0b11111111111100 };
94
100template< uint8_t... Pins >
101constexpr bool are_pins_valid()
102{
103 return ((validPinMask & (1U << Pins)) && ...);
104}
105
110#define VALIDATE_PINS(...) static_assert(are_pins_valid< __VA_ARGS__ >(), "Invalid pin(s) specified")
111
117template< uint8_t... Pins >
118constexpr uint16_t indicesToBitmask()
119{
120 return ((1U << Pins) | ...);
121}
122
136template< uint8_t NL, uint8_t NR, uint8_t D >
138{
139public:
145 constexpr TargetMap(const uint8_t (&loadMap)[NL], const RelayEngine< NR, D >& relays, const bool relayDiversion)
146 : loadMap_(loadMap), relays_(relays), relayDiversion_(relayDiversion)
147 {
148 }
149
151 constexpr uint8_t pinOf(const uint8_t code) const
152 {
154 {
155 const uint8_t loadNum = code - TargetCode::LOAD;
156 return loadNum < NL ? overridePinOf(loadMap_, loadNum) : 0;
157 }
159 {
160 const uint8_t loadNum = code - TargetCode::LOCAL_LOAD;
161 return loadNum < NL ? Load::pinOf(loadMap_[loadNum]) : 0;
162 }
164 {
165 const uint8_t relayNum = code - TargetCode::RELAY;
166 return relayDiversion_ && relayNum < NR ? relays_.get_relay(relayNum).pin : 0;
167 }
168 return code;
169 }
170
172 static constexpr bool isGroup(const uint8_t code)
173 {
175 }
176
178 constexpr uint32_t groupMask(const uint8_t code) const
179 {
180 switch (code)
181 {
183 return localLoadsMask(loadMap_);
189 return relaysMask();
192 default:
193 return 0;
194 }
195 }
196
198 template< uint8_t MaxPins >
199 constexpr uint16_t localBitmask(const PinList< MaxPins >& targets) const
200 {
201 uint16_t result{ 0 };
202 for (uint8_t i = 0; i < targets.count; ++i)
203 {
204 const uint8_t code{ targets.pins[i] };
205 result |= isGroup(code) ? static_cast< uint16_t >(groupMask(code)) : PinList< 1 >{ pinOf(code) }.toLocalBitmask();
206 }
207 return result;
208 }
209
211 template< uint8_t MaxPins >
212 constexpr uint8_t remoteBitmask(const PinList< MaxPins >& targets) const
213 {
214 uint8_t result{ 0 };
215 for (uint8_t i = 0; i < targets.count; ++i)
216 {
217 const uint8_t code{ targets.pins[i] };
218 result |= isGroup(code) ? static_cast< uint8_t >(groupMask(code) >> 16) : PinList< 1 >{ pinOf(code) }.toRemoteBitmask();
219 }
220 return result;
221 }
222
223private:
225 constexpr uint32_t relaysMask() const
226 {
227 uint32_t mask{ 0 };
228 if (relayDiversion_)
229 {
230 for (uint8_t i = 0; i < NR; ++i)
231 {
232 bit_set(mask, relays_.get_relay(i).pin);
233 }
234 }
235 return mask;
236 }
237
238 const uint8_t (&loadMap_)[NL];
240 const bool relayDiversion_;
241};
242
254template< uint8_t N, uint8_t MaxPins = 16 >
256{
257private:
269 struct Entry
270 {
271 uint8_t pin;
272 uint16_t localBitmask;
274 };
275
276 const Entry entries_[N];
277
278public:
284 constexpr OverrideTable(const KeyIndexPair< MaxPins > (&pairs)[N])
285 : entries_{} // default initialize
286 {
287 Entry temp[N]{};
288 for (uint8_t i = 0; i < N; ++i)
289 {
290 temp[i] = { pairs[i].pin, pairs[i].getLocalBitmask(), pairs[i].getRemoteBitmask() };
291 }
292 for (uint8_t i = 0; i < N; ++i)
293 {
294 const_cast< Entry& >(entries_[i]) = temp[i];
295 }
296 }
297
304 template< typename Map >
305 constexpr OverrideTable(const OverridePins< N, MaxPins >& pins, const Map& map)
306 : entries_{} // default initialize
307 {
308 Entry temp[N]{};
309 for (uint8_t i = 0; i < N; ++i)
310 {
311 temp[i] = { pins.getPin(i), map.localBitmask(pins.getTargets(i)), map.remoteBitmask(pins.getTargets(i)) };
312 }
313 for (uint8_t i = 0; i < N; ++i)
314 {
315 const_cast< Entry& >(entries_[i]) = temp[i];
316 }
317 }
318
323 constexpr uint8_t size() const
324 {
325 return N;
326 }
327
333 constexpr uint8_t getPin(uint8_t index) const
334 {
335 return index < N ? entries_[index].pin : 0;
336 }
337
343 constexpr uint16_t getLocalBitmask(uint8_t index) const
344 {
345 return index < N ? entries_[index].localBitmask : 0;
346 }
347
353 constexpr uint8_t getRemoteBitmask(uint8_t index) const
354 {
355 return index < N ? entries_[index].remoteBitmask : 0;
356 }
357
363 constexpr uint16_t findLocalBitmask(uint8_t pin) const
364 {
365 for (uint8_t i = 0; i < N; ++i)
366 {
367 if (entries_[i].pin == pin)
368 {
369 return entries_[i].localBitmask;
370 }
371 }
372 return 0;
373 }
374
380 constexpr uint8_t findRemoteBitmask(uint8_t pin) const
381 {
382 for (uint8_t i = 0; i < N; ++i)
383 {
384 if (entries_[i].pin == pin)
385 {
386 return entries_[i].remoteBitmask;
387 }
388 }
389 return 0;
390 }
391
392#ifdef ARDUINO
398 void printOverrideConfig() const
399 {
400 infoln(F("*** Override Pins Configuration ***"));
401 for (uint8_t i = 0; i < N; ++i)
402 {
403 info(F("\tPin: "));
404 info(entries_[i].pin);
405 info(F("\tLocal: 0b"));
406 info(entries_[i].localBitmask, BIN);
407 info(F("\tRemote: 0b"));
408 infoln(entries_[i].remoteBitmask, BIN);
409 }
410 }
411#endif
412};
413
418template< uint8_t MaxPins, uint8_t N >
421
422#endif /* UTILS_OVERRIDE_H */
The override pins, as in config.h: each pin with the targets it forces.
constexpr uint8_t getPin(const uint8_t index) const
The override pin at index (0 if out of range).
constexpr const PinList< MaxPins > & getTargets(const uint8_t index) const
The targets of the override pin at index, as codes.
Manages override pins and their associated bitmasks for forced operation.
constexpr uint8_t findRemoteBitmask(uint8_t pin) const
Finds the remote bitmask associated with a given pin number.
constexpr uint8_t getRemoteBitmask(uint8_t index) const
Returns the remote bitmask for the specified entry index.
constexpr OverrideTable(const KeyIndexPair< MaxPins >(&pairs)[N])
Constructor. Initializes the override pin mapping from pin-index pairs.
constexpr uint8_t getPin(uint8_t index) const
Returns the pin number at the specified entry index.
constexpr uint16_t getLocalBitmask(uint8_t index) const
Returns the local bitmask for the specified entry index.
constexpr uint16_t findLocalBitmask(uint8_t pin) const
Finds the local bitmask associated with a given pin number.
const Entry entries_[N]
constexpr uint8_t size() const
Returns the number of override pin entries.
constexpr OverrideTable(const OverridePins< N, MaxPins > &pins, const Map &map)
Constructor. The override pins of config.h, their codes turned into pins by map.
const uint8_t(& loadMap_)[NL]
constexpr uint8_t pinOf(const uint8_t code) const
The pin that a single code stands for (any code but ALL_...()).
constexpr TargetMap(const uint8_t(&loadMap)[NL], const RelayEngine< NR, D > &relays, const bool relayDiversion)
constexpr uint8_t remoteBitmask(const PinList< MaxPins > &targets) const
The remote loads that targets force, as a bitmask (bit 0 = remote load 0).
const RelayEngine< NR, D > & relays_
static constexpr bool isGroup(const uint8_t code)
Whether code is one of ALL_...(): several pins.
constexpr uint32_t groupMask(const uint8_t code) const
The pins of an ALL_...() code: local pins in the lower 16 bits, remote loads from bit 16.
constexpr uint32_t relaysMask() const
The pins of the relays, none without relay diversion.
constexpr uint16_t localBitmask(const PinList< MaxPins > &targets) const
The local loads and relays that targets force, as a bitmask of pins.
const bool relayDiversion_
constexpr RelayEngine relays
Definition config.h:130
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.
The text output: informational messages (info) and debug messages (debug)
void infoln(const Args &... args)
Print an informational message and a line end, as Serial.println() does.
Definition debug.h:67
void info(const Args &... args)
Print an informational message, as Serial.print() does.
Definition debug.h:57
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 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 uint8_t countRemoteLoads(const uint8_t(&map)[N])
Number of entries of the map that are driven remotely.
Definition load_map.h:127
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 ALL_RELAYS
constexpr uint8_t ALL_LOADS_AND_RELAYS
constexpr uint8_t LOCAL_LOAD
Structure holding a pin and its associated index list.
Internal structure representing a pin and its associated bitmasks.
Wrapper for a list of pins, constructible from variadic arguments.
uint8_t pins[MaxPins]
constexpr uint16_t toLocalBitmask() const
Converts the pin list to a bitmask for local/physical pins only.
constexpr uint8_t toRemoteBitmask() const
Converts the pin list to a bitmask for remote loads only.
uint8_t count
Some useful but missing stl functions templates.
Bit manipulation utility functions.
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
Definition utils_bits.h:30
constexpr uint32_t remoteLoadsMask(const uint8_t(&loadMap)[N])
Bitmask of every remote load (bit 16 + remote rank).
constexpr uint16_t validPinMask
constexpr bool are_pins_valid()
Compile-time validation function for pin values.
constexpr uint16_t indicesToBitmask()
Helper to convert pins to a bitmask at compile-time.
OverrideTable(const KeyIndexPair< MaxPins >(&)[N]) -> OverrideTable< N, MaxPins >
Deduction guide for OverrideTable, from plain pins. Allows template argument deduction from construct...
constexpr uint8_t overridePinOf(const uint8_t(&loadMap)[N], uint8_t loadNum)
Override pin of a load: its physical pin if local, a virtual pin if remote.
constexpr uint32_t localLoadsMask(const uint8_t(&loadMap)[N])
Bitmask of the pins of every local load (lower 16 bits).