3-phase PV router
Loading...
Searching...
No Matches
config_types.h
Go to the documentation of this file.
1
20
21#ifndef CONFIG_TYPES_H
22#define CONFIG_TYPES_H
23
24#include <stdint.h>
25
26#include "type_traits.hpp" // MINUTES(), HOURS(), integral_constant, index_sequence
27#include "load_map.h" // Load::local(), Load::remote()
28#include "utils_bits.h" // unused_pin
29
37
39enum class PcbVersion : uint8_t
40{
43};
44
46enum class RotationModes : uint8_t
47{
51};
52
53
54//--------------------------------------------------------------------------------------------------
55// Targets of the override pins and of the scheduled boosts
56//
62inline constexpr uint8_t REMOTE_PIN_BASE{ 128 };
63
64inline constexpr uint8_t LOCAL_MASK_BITS{ 16 };
65inline constexpr uint8_t REMOTE_MASK_BITS{ 8 };
66
75namespace TargetCode
76{
77inline constexpr uint8_t LOAD{ 0x40 };
78inline constexpr uint8_t LOCAL_LOAD{ 0x60 };
79inline constexpr uint8_t RELAY{ 0x70 };
80
81inline constexpr uint8_t MAX_LOADS{ 32 };
82inline constexpr uint8_t MAX_LOCAL_LOADS{ 16 };
83inline constexpr uint8_t MAX_RELAYS{ 16 };
84
85inline constexpr uint8_t ALL_LOCAL_LOADS{ 0xF0 };
86inline constexpr uint8_t ALL_REMOTE_LOADS{ 0xF1 };
87inline constexpr uint8_t ALL_LOADS{ 0xF2 };
88inline constexpr uint8_t ALL_RELAYS{ 0xF3 };
89inline constexpr uint8_t ALL_LOADS_AND_RELAYS{ 0xF4 };
90} // namespace TargetCode
91
93constexpr uint8_t LOAD(const uint8_t loadNum)
94{
95 return loadNum < TargetCode::MAX_LOADS ? static_cast< uint8_t >(TargetCode::LOAD + loadNum) : 0;
96}
97
99constexpr uint8_t LOCAL_LOAD(const uint8_t loadNum)
100{
101 return loadNum < TargetCode::MAX_LOCAL_LOADS ? static_cast< uint8_t >(TargetCode::LOCAL_LOAD + loadNum) : 0;
102}
103
108constexpr uint8_t REMOTE_LOAD(const uint8_t loadNum)
109{
110 return loadNum < REMOTE_MASK_BITS ? static_cast< uint8_t >(REMOTE_PIN_BASE + loadNum) : 0;
111}
112
114constexpr uint8_t RELAY(const uint8_t relayNum)
115{
116 return relayNum < TargetCode::MAX_RELAYS ? static_cast< uint8_t >(TargetCode::RELAY + relayNum) : 0;
117}
118
120constexpr uint8_t ALL_LOCAL_LOADS()
121{
123}
124
126constexpr uint8_t ALL_REMOTE_LOADS()
127{
129}
130
132constexpr uint8_t ALL_LOADS()
133{
135}
136
138constexpr uint8_t ALL_RELAYS()
139{
141}
142
144constexpr uint8_t ALL_LOADS_AND_RELAYS()
145{
147}
148
156template< uint8_t MaxPins >
158{
159 uint8_t pins[MaxPins];
160 uint8_t count;
161
165 constexpr PinList()
166 : pins{}, count(0) {}
167
172 constexpr PinList(uint16_t bitmask)
173 : pins{}, count(0)
174 {
175 for (uint8_t pin = 0; pin < 16 && count < MaxPins; ++pin)
176 {
177 if (bitmask & (1U << pin))
178 {
179 pins[count++] = pin; // Store the physical pin number
180 }
181 }
182 }
183
190 constexpr PinList(uint32_t bitmask)
191 : pins{}, count(0)
192 {
193 // Extract local pins from lower 16 bits
194 for (uint8_t pin = 0; pin < 16 && count < MaxPins; ++pin)
195 {
196 if (bitmask & (1UL << pin))
197 {
198 pins[count++] = pin; // Store the physical pin number
199 }
200 }
201 // Extract remote loads from upper 16 bits
202 for (uint8_t i = 0; i < 8 && count < MaxPins; ++i)
203 {
204 if (bitmask & (1UL << (16 + i)))
205 {
206 pins[count++] = REMOTE_PIN_BASE + i; // Store virtual pin for remote load
207 }
208 }
209 }
210
215 template< typename... Args >
216 constexpr PinList(Args... args)
217 : pins{ static_cast< uint8_t >(args)... }, count(sizeof...(args)) {}
218
223 constexpr uint16_t toLocalBitmask() const
224 {
225 uint16_t result{ 0 };
226 for (uint8_t i = 0; i < count; ++i)
227 {
228 if (pins[i] < REMOTE_PIN_BASE)
229 {
230 result |= (1U << pins[i]);
231 }
232 }
233 return result;
234 }
235
241 constexpr uint8_t toRemoteBitmask() const
242 {
243 uint8_t result{ 0 };
244 for (uint8_t i = 0; i < count; ++i)
245 {
246 if (pins[i] >= REMOTE_PIN_BASE)
247 {
248 const uint8_t remoteIndex = pins[i] - REMOTE_PIN_BASE;
249 if (remoteIndex < REMOTE_MASK_BITS)
250 {
251 result |= (1U << remoteIndex);
252 }
253 }
254 }
255 return result;
256 }
257};
258
263template< uint8_t MaxPins >
265{
266 uint8_t pin;
268
274 constexpr KeyIndexPair(uint8_t k, const PinList< MaxPins >& list)
275 : pin(k), indexList(list) {}
276
281 constexpr uint16_t getLocalBitmask() const
282 {
283 return indexList.toLocalBitmask();
284 }
285
290 constexpr uint8_t getRemoteBitmask() const
291 {
292 return indexList.toRemoteBitmask();
293 }
294};
295
304template< uint8_t N, uint8_t MaxPins = 16 >
306{
307public:
309 constexpr OverridePins(const KeyIndexPair< MaxPins > (&pairs)[N])
310 : OverridePins(pairs, make_index_sequence< N >{})
311 {
312 }
313
315 constexpr uint8_t size() const
316 {
317 return N;
318 }
319
321 constexpr uint8_t getPin(const uint8_t index) const
322 {
323 return index < N ? entries_[index].pin : 0;
324 }
325
327 constexpr const PinList< MaxPins >& getTargets(const uint8_t index) const
328 {
329 return entries_[index].indexList;
330 }
331
332private:
333 template< uint8_t... I >
334 constexpr OverridePins(const KeyIndexPair< MaxPins > (&pairs)[N], index_sequence< I... > /* is */)
335 : entries_{ pairs[I]... }
336 {
337 }
338
340};
341
343template< uint8_t MaxPins, uint8_t N >
346
347//--------------------------------------------------------------------------------------------------
348// Scheduled boosts
349//
352{
353 uint16_t value;
354
356 explicit constexpr Minutes(const uint16_t v)
357 : value(v) {}
358
360 template< uint16_t V >
362 : value(V) {}
363};
364
367{
369 uint16_t minutes;
370};
371
373inline constexpr uint16_t UNTIL_THE_END{ UINT16_MAX };
374
377{
378 uint16_t minutes;
379};
380
387
389constexpr BoostStart FROM_START(const Minutes m)
390{
391 return BoostStart{ false, m.value };
392}
393
395constexpr BoostStart BEFORE_END(const Minutes m)
396{
397 return BoostStart{ true, m.value };
398}
399
406constexpr BoostLength FOR(const Minutes m)
407{
408 return BoostLength{ m.value == UNTIL_THE_END ? static_cast< uint16_t >(UNTIL_THE_END - 1) : m.value };
409}
410
412constexpr BoostWindow FIRST(const Minutes d)
413{
414 return BoostWindow{ FROM_START(MINUTES(0)), FOR(d) };
415}
416
418constexpr BoostWindow LAST(const Minutes d)
419{
421}
422
424template< uint8_t MaxPins = 16 >
426{
429
431 constexpr BoostEntry(const BoostWindow& w, const PinList< MaxPins >& t)
432 : window(w), targets(t) {}
433
435 constexpr BoostEntry(const BoostStart& s, const PinList< MaxPins >& t)
436 : window{ s, BoostLength{ UNTIL_THE_END } }, targets(t) {}
437
439 constexpr BoostEntry(const BoostStart& s, const BoostLength& l, const PinList< MaxPins >& t)
440 : window{ s, l }, targets(t) {}
441};
442
451template< uint8_t N, uint8_t MaxPins = 16 >
453{
454public:
456 constexpr ScheduledBoosts(const BoostEntry< MaxPins > (&boosts)[N])
457 : ScheduledBoosts(boosts, make_index_sequence< N >{})
458 {
459 }
460
462 constexpr uint8_t size() const
463 {
464 return N;
465 }
466
468 constexpr BoostWindow getWindow(const uint8_t i) const
469 {
470 return entries_[i].window;
471 }
472
474 constexpr const PinList< MaxPins >& getTargets(const uint8_t i) const
475 {
476 return entries_[i].targets;
477 }
478
479private:
480 template< uint8_t... I >
481 constexpr ScheduledBoosts(const BoostEntry< MaxPins > (&boosts)[N], index_sequence< I... > /* is */)
482 : entries_{ boosts[I]... }
483 {
484 }
485
487};
488
490template< uint8_t MaxPins >
491class ScheduledBoosts< 0, MaxPins >
492{
493public:
494 constexpr ScheduledBoosts() = default;
495
497 constexpr uint8_t size() const
498 {
499 return 0;
500 }
501
503 constexpr BoostWindow getWindow(uint8_t) const
504 {
505 return BoostWindow{ FROM_START(MINUTES(0)), BoostLength{ 0 } };
506 }
507
509 constexpr PinList< MaxPins > getTargets(uint8_t) const
510 {
511 return PinList< MaxPins >{};
512 }
513};
514
516template< uint8_t MaxPins, uint8_t N >
519
522
536template< uint8_t N, uint8_t MaxPins = 16 >
538{
539 static_assert(N >= 1 && N <= 10, "rotationGroups holds 1 to 10 groups");
540
541public:
543 constexpr RotationGroups(const PinList< MaxPins >& g0, const PinList< MaxPins >& g1 = {},
544 const PinList< MaxPins >& g2 = {}, const PinList< MaxPins >& g3 = {},
545 const PinList< MaxPins >& g4 = {}, const PinList< MaxPins >& g5 = {},
546 const PinList< MaxPins >& g6 = {}, const PinList< MaxPins >& g7 = {},
547 const PinList< MaxPins >& g8 = {}, const PinList< MaxPins >& g9 = {})
548 : RotationGroups(Ten{ { g0, g1, g2, g3, g4, g5, g6, g7, g8, g9 } }, make_index_sequence< N >{})
549 {
550 }
551
553 constexpr uint8_t size() const
554 {
555 return N;
556 }
557
559 constexpr const PinList< MaxPins >& getGroup(const uint8_t index) const
560 {
561 return groups_[index];
562 }
563
564private:
565 struct Ten
566 {
568 };
569
570 template< uint8_t... I >
571 constexpr RotationGroups(const Ten& ten, index_sequence< I... > /* is */)
572 : groups_{ ten.groups[I]... }
573 {
574 }
575
577};
578
580template< uint8_t MaxPins >
581class RotationGroups< 0, MaxPins >
582{
583public:
584 constexpr RotationGroups() = default;
585
587 constexpr uint8_t size() const
588 {
589 return 0;
590 }
591
593 constexpr PinList< MaxPins > getGroup(uint8_t) const
594 {
595 return PinList< MaxPins >{};
596 }
597};
598
612
613//--------------------------------------------------------------------------------------------------
614// Relays
615//
616
624{
625 uint8_t pin;
628 uint16_t minON;
629 uint16_t minOFF;
630
632 constexpr RelaySetting(const uint8_t p, const int16_t surplus = 1000, const int16_t import = 200, const uint16_t on = 5, const uint16_t off = 5)
633 : pin{ p }, surplusThreshold{ surplus }, importThreshold{ import }, minON{ on }, minOFF{ off }
634 {
635 }
636};
637
646template< uint8_t N, uint8_t D = 10 >
648{
649public:
651 explicit constexpr RelayEngine(const RelaySetting (&ref)[N])
652 : RelayEngine(ref, make_index_sequence< N >{})
653 {
654 }
655
657 constexpr RelayEngine(integral_constant< uint8_t, D > /* ic */, const RelaySetting (&ref)[N])
658 : RelayEngine(ref, make_index_sequence< N >{})
659 {
660 }
661
663 template< uint16_t V >
664 constexpr RelayEngine(integral_constant< uint16_t, V > /* ic */, const RelaySetting (&ref)[N])
665 : RelayEngine(ref, make_index_sequence< N >{})
666 {
667 static_assert(V <= UINT8_MAX, "******** RELAY_FILTER_DELAY cannot exceed 255 minutes ! Please check your config.h ! ********");
668 static_assert(V == D, "******** The MINUTES() given to RelayEngine differs from its template parameter D (RELAY_FILTER_DELAY) ! Please check your config.h ! ********");
669 }
670
672 constexpr uint8_t size() const
673 {
674 return N;
675 }
676
678 static constexpr uint8_t filterDelay()
679 {
680 return D;
681 }
682
684 constexpr const RelaySetting& get_relay(const uint8_t idx) const
685 {
686 return relay_[idx];
687 }
688
689private:
690 template< uint8_t... I >
691 constexpr RelayEngine(const RelaySetting (&ref)[N], index_sequence< I... > /* is */)
692 : relay_{ ref[I]... }
693 {
694 }
695
697};
698
700template< uint8_t N >
702
704template< uint8_t N, uint8_t D >
706
708template< uint8_t N, uint16_t D >
710
711//--------------------------------------------------------------------------------------------------
712// Temperature sensors
713//
728{
729 uint8_t addr[8]{};
730};
731
739template< uint8_t N >
741{
742public:
743 constexpr TemperatureSensing() = delete;
744
746 constexpr TemperatureSensing(const uint8_t pin, const DeviceAddress (&ref)[N])
747 : TemperatureSensing(pin, ref, make_index_sequence< N >{})
748 {
749 }
750
752 constexpr uint8_t size() const
753 {
754 return N;
755 }
756
758 constexpr uint8_t get_pin() const
759 {
760 return sensorPin;
761 }
762
763protected:
764 const uint8_t sensorPin{ unused_pin };
766
767private:
768 template< uint8_t... I >
769 constexpr TemperatureSensing(const uint8_t pin, const DeviceAddress (&ref)[N], index_sequence< I... > /* is */)
770 : sensorPin{ pin }, sensorAddrs{ ref[I]... }
771 {
772 }
773};
774
775#endif /* CONFIG_TYPES_H */
The override pins, as in config.h: each pin with the targets it forces.
constexpr OverridePins(const KeyIndexPair< MaxPins >(&pairs)[N])
The override pins, from a list of { pin, targets }.
constexpr uint8_t getPin(const uint8_t index) const
The override pin at index (0 if out of range).
constexpr uint8_t size() const
Number of override pins.
constexpr const PinList< MaxPins > & getTargets(const uint8_t index) const
The targets of the override pin at index, as codes.
const KeyIndexPair< MaxPins > entries_[N]
constexpr OverridePins(const KeyIndexPair< MaxPins >(&pairs)[N], index_sequence< I... >)
The relays, as in config.h: the sliding average and the settings of each relay.
constexpr uint8_t size() const
Number of relays.
constexpr RelayEngine(integral_constant< uint16_t, V >, const RelaySetting(&ref)[N])
The relays, with a sliding average given with MINUTES().
const RelaySetting relay_[N]
constexpr RelayEngine(const RelaySetting(&ref)[N], index_sequence< I... >)
constexpr RelayEngine(const RelaySetting(&ref)[N])
The relays, with the default sliding average.
constexpr const RelaySetting & get_relay(const uint8_t idx) const
The settings of relay idx.
static constexpr uint8_t filterDelay()
Duration of the sliding average, in minutes: D, the one the relay engine uses.
constexpr RelayEngine(integral_constant< uint8_t, D >, const RelaySetting(&ref)[N])
The relays, with a sliding average given as an integral_constant< uint8_t >.
constexpr RotationGroups()=default
constexpr PinList< MaxPins > getGroup(uint8_t) const
No targets: never called, there for the loops over size().
constexpr uint8_t size() const
Number of groups: none.
The rotation groups, as in config.h: the loads that take turns at the top of the priority list,...
constexpr RotationGroups(const PinList< MaxPins > &g0, const PinList< MaxPins > &g1={}, const PinList< MaxPins > &g2={}, const PinList< MaxPins > &g3={}, const PinList< MaxPins > &g4={}, const PinList< MaxPins > &g5={}, const PinList< MaxPins > &g6={}, const PinList< MaxPins > &g7={}, const PinList< MaxPins > &g8={}, const PinList< MaxPins > &g9={})
The groups, one list of targets each.
const PinList< MaxPins > groups_[N]
constexpr uint8_t size() const
Number of groups.
constexpr RotationGroups(const Ten &ten, index_sequence< I... >)
constexpr const PinList< MaxPins > & getGroup(const uint8_t index) const
The targets of group index, as codes.
constexpr PinList< MaxPins > getTargets(uint8_t) const
No targets: never called, there for the loops over size().
constexpr BoostWindow getWindow(uint8_t) const
No window: never called, there for the loops over size().
constexpr uint8_t size() const
Number of boosts: none.
constexpr ScheduledBoosts()=default
The scheduled boosts, as in config.h: each window with the targets it forces.
constexpr ScheduledBoosts(const BoostEntry< MaxPins >(&boosts)[N])
The boosts, from a list of { window, targets }.
const BoostEntry< MaxPins > entries_[N]
constexpr const PinList< MaxPins > & getTargets(const uint8_t i) const
The targets of boost i, as codes.
constexpr uint8_t size() const
Number of boosts.
constexpr ScheduledBoosts(const BoostEntry< MaxPins >(&boosts)[N], index_sequence< I... >)
constexpr BoostWindow getWindow(const uint8_t i) const
The window of boost i.
constexpr TemperatureSensing()=delete
constexpr TemperatureSensing(const uint8_t pin, const DeviceAddress(&ref)[N], index_sequence< I... >)
const DeviceAddress sensorAddrs[N]
const uint8_t sensorPin
constexpr uint8_t get_pin() const
The pin of the bus.
constexpr TemperatureSensing(const uint8_t pin, const DeviceAddress(&ref)[N])
The sensors on pin, from their addresses.
constexpr uint8_t size() const
Number of sensors.
RotationModes
RotationGroups() -> RotationGroups< 0 >
Deduction guides: the number of groups is the number of lists.
constexpr BoostStart BEFORE_END(const Minutes m)
A start m before the end of the off-peak period.
constexpr uint8_t REMOTE_PIN_BASE
Base value for virtual pins representing remote loads.
constexpr BoostLength FOR(const Minutes m)
A boost lasting m.
constexpr uint8_t ALL_REMOTE_LOADS()
Every remote load.
constexpr uint8_t LOCAL_LOAD(const uint8_t loadNum)
Same as LOAD(), for a load known to be local.
constexpr BoostWindow FIRST(const Minutes d)
The first d of the off-peak period.
constexpr uint8_t ALL_RELAYS()
Every relay (none without relay diversion).
PinList< 16 > RotationGroupTargets
constexpr BoostWindow LAST(const Minutes d)
The last d of the off-peak period, until the end.
constexpr BoostStart FROM_START(const Minutes m)
A start m after the beginning of the off-peak period.
constexpr uint8_t REMOTE_MASK_BITS
SerialOutputType
constexpr uint16_t UNTIL_THE_END
The length of a boost that only the meter ends: the end of the off-peak period stops it anyway.
constexpr uint8_t ALL_LOCAL_LOADS()
Every local load.
PcbVersion
RelayEngine(const RelaySetting(&)[N]) -> RelayEngine< N, 10 >
Deduction guide: the default sliding average.
constexpr uint8_t RELAY(const uint8_t relayNum)
Relay relayNum of relays (0, no relay, if out of range or without relay diversion).
constexpr uint8_t ALL_LOADS()
Every load, local and remote.
OverridePins(const KeyIndexPair< MaxPins >(&)[N]) -> OverridePins< N, MaxPins >
Deduction guide: N and MaxPins from the list.
ScheduledBoosts() -> ScheduledBoosts< 0 >
Deduction guide: no argument, no boost.
constexpr uint8_t LOAD(const uint8_t loadNum)
Load loadNum of physicalLoadPin[], local or remote (0, no load, if out of range).
constexpr uint8_t ALL_LOADS_AND_RELAYS()
Every load and every relay.
constexpr uint8_t LOCAL_MASK_BITS
uint8_t i
typename make_index_sequence_impl< N >::type make_index_sequence
index_sequence< 0, 1, ..., N - 1 >.
Encoding of the load map: one packed byte per dump load.
The codes LOAD(), RELAY() and ALL_...() put in a target list.
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
#define REMOTE_LOAD(n)
When a set of loads is boosted: a window and the targets.
constexpr BoostEntry(const BoostStart &s, const PinList< MaxPins > &t)
A boost from s until the end of the off-peak period.
BoostWindow window
constexpr BoostEntry(const BoostStart &s, const BoostLength &l, const PinList< MaxPins > &t)
A boost from s, lasting l.
constexpr BoostEntry(const BoostWindow &w, const PinList< MaxPins > &t)
A boost with its window and targets.
PinList< MaxPins > targets
How long a boost lasts, in minutes, or UNTIL_THE_END.
uint16_t minutes
Where a boost starts: minutes after the start of the off-peak period, or before its end if fromEnd.
uint16_t minutes
When a load is boosted during the off-peak period.
BoostStart start
BoostLength length
Structure representing the address of a device.
uint8_t addr[8]
Structure holding a pin and its associated index list.
constexpr uint16_t getLocalBitmask() const
Returns the bitmask for local pins in the index list.
PinList< MaxPins > indexList
constexpr KeyIndexPair(uint8_t k, const PinList< MaxPins > &list)
Constructor.
constexpr uint8_t getRemoteBitmask() const
Returns the bitmask for remote loads in the index list.
A number of minutes: the unit of the scheduled boosts, built with HOURS() or MINUTES().
constexpr Minutes(integral_constant< uint16_t, V >)
From MINUTES() or HOURS() (type_traits.hpp), the same macros as for the relays.
constexpr Minutes(const uint16_t v)
v minutes: explicit, so that a bare number is not taken for a duration.
uint16_t value
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 PinList(uint32_t bitmask)
Constructor from combined bitmask. Extracts both local and remote pins.
constexpr PinList()
Default constructor. Initializes with zero pins.
constexpr uint8_t toRemoteBitmask() const
Converts the pin list to a bitmask for remote loads only.
constexpr PinList(uint16_t bitmask)
Constructor from local-only bitmask.
constexpr PinList(Args... args)
Variadic constructor. Initializes with provided pins.
The settings of one relay, as in config.h: { pin, surplus, import, minON, minOFF }...
uint16_t minOFF
constexpr RelaySetting(const uint8_t p, const int16_t surplus=1000, const int16_t import=200, const uint16_t on=5, const uint16_t off=5)
A relay on p; what is left out takes its default.
int16_t surplusThreshold
uint16_t minON
int16_t importThreshold
PinList< MaxPins > groups[10]
The indexes I..., as a type.
Some useful but missing stl functions templates.
#define MINUTES(value)
A compile-time number of minutes, as an integral_constant.
Bit manipulation utility functions.
constexpr uint8_t unused_pin
Definition utils_bits.h:20