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// Valid pins: 2-13, so valid mask is 0b11111111111100
37constexpr uint16_t validPinMask{ 0b11111111111100 };
38
44template< uint8_t... Pins >
45constexpr bool are_pins_valid()
46{
47 return ((validPinMask & (1U << Pins)) && ...);
48}
49
54#define VALIDATE_PINS(...) static_assert(are_pins_valid< __VA_ARGS__ >(), "Invalid pin(s) specified")
55
61template< uint8_t... Pins >
62constexpr uint16_t indicesToBitmask()
63{
64 return ((1U << Pins) | ...);
65}
66
71template< uint8_t MaxPins >
72struct PinList
73{
74 uint8_t pins[MaxPins];
75 uint8_t count;
76
80 constexpr PinList()
81 : pins{}, count(0) {}
82
87 constexpr PinList(uint16_t bitmask)
88 : pins{}, count(0)
89 {
90 for (uint8_t pin = 0; pin < 16 && count < MaxPins; ++pin)
91 {
92 if (bitmask & (1U << pin))
93 {
94 pins[count++] = pin; // Store the pin number
95 }
96 }
97 }
98
103 template< typename... Args >
104 constexpr PinList(Args... args)
105 : pins{ static_cast< uint8_t >(args)... }, count(sizeof...(args)) {}
106
111 constexpr uint16_t toBitmask() const
112 {
113 uint16_t result = 0;
114 for (uint8_t i = 0; i < count; ++i)
115 {
116 result |= (1U << pins[i]);
117 }
118 return result;
119 }
120};
121
126template< uint8_t MaxPins >
128{
129 uint8_t pin;
131
137 constexpr KeyIndexPair(uint8_t k, const PinList< MaxPins >& list)
138 : pin(k), indexList(list) {}
139
144 constexpr uint16_t getBitmask() const
145 {
146 return indexList.toBitmask();
147 }
148};
149
160template< uint8_t N, uint8_t MaxPins = 16 >
162{
163private:
173 struct Entry
174 {
175 uint8_t pin;
176 uint16_t bitmask;
177 };
178
179 const Entry entries_[N];
180
181public:
187 constexpr OverridePins(const KeyIndexPair< MaxPins > (&pairs)[N])
188 : entries_{} // default initialize
189 {
190 Entry temp[N]{};
191 for (uint8_t i = 0; i < N; ++i)
192 {
193 temp[i] = { pairs[i].pin, pairs[i].getBitmask() };
194 }
195 for (uint8_t i = 0; i < N; ++i)
196 {
197 const_cast< Entry& >(entries_[i]) = temp[i];
198 }
199 }
200
205 constexpr uint8_t size() const
206 {
207 return N;
208 }
209
215 constexpr uint8_t getPin(uint8_t index) const
216 {
217 return index < N ? entries_[index].pin : 0;
218 }
219
225 constexpr uint16_t getBitmask(uint8_t index) const
226 {
227 return index < N ? entries_[index].bitmask : 0;
228 }
229
235 constexpr uint16_t findBitmask(uint8_t pin) const
236 {
237 for (uint8_t i = 0; i < N; ++i)
238 {
239 if (entries_[i].pin == pin)
240 {
241 return entries_[i].bitmask;
242 }
243 }
244 return 0;
245 }
246
247#ifdef ARDUINO
253 void printOverrideConfig() const
254 {
255 Serial.println(F("*** Override Pins Configuration ***"));
256 for (uint8_t i = 0; i < N; ++i)
257 {
258 Serial.print(F("\tPin: "));
259 Serial.print(entries_[i].pin);
260 Serial.print(F("\tBitmask: 0b"));
261 Serial.println(entries_[i].bitmask, BIN);
262 }
263 }
264#endif
265};
266
271template< uint8_t MaxPins, uint8_t N >
274
275#endif /* UTILS_OVERRIDE_H */
Manages override pins and their associated bitmasks for forced operation.
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.
uint8_t i
Definition test_main.cpp:92
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 indicesToBitmask()
Helper to convert pins to a bitmask at compile-time.
OverridePins(const KeyIndexPair< MaxPins >(&)[N]) -> OverridePins< N, MaxPins >
Deduction guide for OverridePins template. Allows template argument deduction from constructor argume...