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 "config.h"
35#include "type_traits.hpp"
36
42constexpr uint8_t LOAD(uint8_t loadNum)
43{
44 return physicalLoadPin[loadNum];
45}
46
52constexpr uint8_t RELAY(uint8_t relayNum)
53{
54 return relays.get_relay(relayNum).get_pin();
55}
56
64constexpr uint16_t ALL_LOADS()
65{
66 uint16_t mask{ 0 };
67 for (uint8_t i = 0; i < NO_OF_DUMPLOADS; ++i)
68 {
70 }
71 return mask;
72}
73
81constexpr uint16_t ALL_RELAYS()
82{
83 uint16_t mask{ 0 };
84 for (uint8_t i = 0; i < relays.size(); ++i)
85 {
86 bit_set(mask, relays.get_relay(i).get_pin());
87 }
88 return mask;
89}
90
98constexpr uint16_t ALL_LOADS_AND_RELAYS()
99{
100 return ALL_LOADS() | ALL_RELAYS();
101}
102
103// Valid pins: 2-13, so valid mask is 0b11111111111100
104constexpr uint16_t validPinMask{ 0b11111111111100 };
105
111template< uint8_t... Pins >
112constexpr bool are_pins_valid()
113{
114 return ((validPinMask & (1U << Pins)) && ...);
115}
116
121#define VALIDATE_PINS(...) static_assert(are_pins_valid< __VA_ARGS__ >(), "Invalid pin(s) specified")
122
128template< uint8_t... Pins >
129constexpr uint16_t indicesToBitmask()
130{
131 return ((1U << Pins) | ...);
132}
133
138template< uint8_t MaxPins >
140{
141 uint8_t pins[MaxPins];
142 uint8_t count;
143
147 constexpr PinList()
148 : pins{}, count(0) {}
149
154 constexpr PinList(uint16_t bitmask)
155 : pins{}, count(0)
156 {
157 for (uint8_t pin = 0; pin < 16 && count < MaxPins; ++pin)
158 {
159 if (bitmask & (1U << pin))
160 {
161 pins[count++] = pin; // Store the pin number
162 }
163 }
164 }
165
170 template< typename... Args >
171 constexpr PinList(Args... args)
172 : pins{ static_cast< uint8_t >(args)... }, count(sizeof...(args)) {}
173
178 constexpr uint16_t toBitmask() const
179 {
180 uint16_t result = 0;
181 for (uint8_t i = 0; i < count; ++i)
182 {
183 result |= (1U << pins[i]);
184 }
185 return result;
186 }
187};
188
193template< uint8_t MaxPins >
195{
196 uint8_t pin;
198
204 constexpr KeyIndexPair(uint8_t k, const PinList< MaxPins >& list)
205 : pin(k), indexList(list) {}
206
211 constexpr uint16_t getBitmask() const
212 {
213 return indexList.toBitmask();
214 }
215};
216
227template< uint8_t N, uint8_t MaxPins = NO_OF_DUMPLOADS + relays.size() >
229{
230private:
240 struct Entry
241 {
242 uint8_t pin;
243 uint16_t bitmask;
244 };
245
246 const Entry entries_[N];
247
248public:
254 constexpr OverridePins(const KeyIndexPair< MaxPins > (&pairs)[N])
255 : entries_{} // default initialize
256 {
257 Entry temp[N]{};
258 for (uint8_t i = 0; i < N; ++i)
259 {
260 temp[i] = { pairs[i].pin, pairs[i].getBitmask() };
261 }
262 for (uint8_t i = 0; i < N; ++i)
263 {
264 const_cast< Entry& >(entries_[i]) = temp[i];
265 }
266 }
267
272 constexpr uint8_t size() const
273 {
274 return N;
275 }
276
282 constexpr uint8_t getPin(uint8_t index) const
283 {
284 return index < N ? entries_[index].pin : 0;
285 }
286
292 constexpr uint16_t getBitmask(uint8_t index) const
293 {
294 return index < N ? entries_[index].bitmask : 0;
295 }
296
302 constexpr uint16_t findBitmask(uint8_t pin) const
303 {
304 for (uint8_t i = 0; i < N; ++i)
305 {
306 if (entries_[i].pin == pin)
307 {
308 return entries_[i].bitmask;
309 }
310 }
311 return 0;
312 }
313
320 {
321 Serial.println(F("*** Override Pins Configuration ***"));
322 for (uint8_t i = 0; i < N; ++i)
323 {
324 Serial.print(F("\tPin: "));
325 Serial.print(entries_[i].pin);
326 Serial.print(F("\tBitmask: 0b"));
327 Serial.println(entries_[i].bitmask, BIN);
328 }
329 }
330};
331
336template< uint8_t MaxPins, uint8_t N >
339
340#endif /* UTILS_OVERRIDE_H */
Manages override pins and their associated bitmasks for forced operation.
void printOverrideConfig() const
Print the configured override pins and their bitmasks to Serial during startup.
constexpr OverridePins(const KeyIndexPair< MaxPins >(&pairs)[N])
Constructor. Initializes the override pin mapping from pin-index pairs.
constexpr uint16_t getBitmask(uint8_t index) const
Returns the bitmask for the specified entry index.
const Entry entries_[N]
constexpr uint8_t size() const
Returns the number of override pin entries.
constexpr uint16_t findBitmask(uint8_t pin) const
Finds the bitmask associated with a given pin number.
constexpr uint8_t getPin(uint8_t index) const
Returns the pin number at the specified entry index.
Standard three-phase PVRouter setup with 2 dump loads.
constexpr RelayEngine relays
Definition config.h:126
constexpr uint8_t physicalLoadPin[NO_OF_DUMPLOADS]
Definition config.h:92
constexpr uint8_t NO_OF_DUMPLOADS
Definition config.h:42
uint8_t i
@ RELAY
Definition test_main.cpp:18
Structure holding a pin and its associated index list.
constexpr uint16_t getBitmask() const
Returns the bitmask for the index list.
PinList< MaxPins > indexList
constexpr KeyIndexPair(uint8_t k, const PinList< MaxPins > &list)
Constructor.
Internal structure representing a pin and its associated bitmask.
Wrapper for a list of pins, constructible from variadic arguments.
uint8_t pins[MaxPins]
constexpr PinList()
Default constructor. Initializes with zero pins.
constexpr PinList(uint16_t bitmask)
Constructor from bitmask. Sets pin numbers from bits set in bitmask.
uint8_t count
constexpr uint16_t toBitmask() const
Converts the pin list to a bitmask.
constexpr PinList(Args... args)
Variadic constructor. Initializes with provided pins.
Some useful but missing stl functions templates.
constexpr uint16_t validPinMask
constexpr bool are_pins_valid()
Compile-time validation function for pin values.
constexpr uint16_t ALL_RELAYS()
Returns a bitmask representing all relay pins.
constexpr uint16_t indicesToBitmask()
Helper to convert pins to a bitmask at compile-time.
constexpr uint16_t ALL_LOADS()
Returns a bitmask representing all load pins.
constexpr uint8_t LOAD(uint8_t loadNum)
Returns the pin number for a given load index at compile time.
OverridePins(const KeyIndexPair< MaxPins >(&)[N]) -> OverridePins< N, MaxPins >
Deduction guide for OverridePins template. Allows template argument deduction from constructor argume...
constexpr uint16_t ALL_LOADS_AND_RELAYS()
Returns a bitmask representing all loads and all relays.
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
Definition utils_pins.h:54