3-phase PV router
Loading...
Searching...
No Matches
utils_dualtariff.h
Go to the documentation of this file.
1
14
15#ifndef UTILS_DUALTARIFF_H
16#define UTILS_DUALTARIFF_H
17
18#include <stdint.h>
19
20#include "config_types.h" // the boost windows, BoostEntry, ScheduledBoosts, PinList
21
31template< uint8_t N, uint8_t MaxPins = 16 >
33{
34private:
35 struct Entry
36 {
38 uint16_t localBitmask;
39 uint8_t remoteBitmask;
40 };
41
43
44public:
46 constexpr BoostSchedule(const BoostEntry< MaxPins > (&boosts)[N])
47 {
48 for (uint8_t i = 0; i < N; ++i)
49 {
50 entries_[i] = { boosts[i].window, boosts[i].targets.toLocalBitmask(), boosts[i].targets.toRemoteBitmask() };
51 }
52 }
53
55 template< typename Map >
56 constexpr BoostSchedule(const ScheduledBoosts< N, MaxPins >& boosts, const Map& map)
57 {
58 for (uint8_t i = 0; i < N; ++i)
59 {
60 entries_[i] = { boosts.getWindow(i), map.localBitmask(boosts.getTargets(i)), map.remoteBitmask(boosts.getTargets(i)) };
61 }
62 }
63
65 constexpr uint8_t size() const
66 {
67 return N;
68 }
69
71 constexpr BoostWindow getWindow(uint8_t i) const
72 {
73 return i < N ? entries_[i].window : BoostWindow{ FROM_START(MINUTES(0)), BoostLength{ 0 } };
74 }
75
77 constexpr uint16_t getLocalBitmask(uint8_t i) const
78 {
79 return i < N ? entries_[i].localBitmask : 0;
80 }
81
83 constexpr uint8_t getRemoteBitmask(uint8_t i) const
84 {
85 return i < N ? entries_[i].remoteBitmask : 0;
86 }
87};
88
90template< uint8_t MaxPins >
91class BoostSchedule< 0, MaxPins >
92{
93public:
94 constexpr BoostSchedule() = default;
95
97 template< typename Map >
98 constexpr BoostSchedule(const ScheduledBoosts< 0, MaxPins >& /* boosts */, const Map& /* map */)
99 {
100 }
101
102 constexpr uint8_t size() const
103 {
104 return 0;
105 }
106
107 constexpr BoostWindow getWindow(uint8_t) const
108 {
109 return BoostWindow{ FROM_START(MINUTES(0)), BoostLength{ 0 } };
110 }
111
112 constexpr uint16_t getLocalBitmask(uint8_t) const
113 {
114 return 0;
115 }
116
117 constexpr uint8_t getRemoteBitmask(uint8_t) const
118 {
119 return 0;
120 }
121};
122
124template< uint8_t MaxPins, uint8_t N >
127
130
132template< uint8_t N, uint8_t MaxPins, typename Map >
134
140namespace DualTariff
141{
148constexpr bool inWindow(const uint32_t elapsed, const uint32_t start, const uint32_t end)
149{
150 return (elapsed >= start) && (elapsed < end);
151}
152
161constexpr uint32_t windowStart(const BoostWindow& window, const uint8_t offPeakHours)
162{
163 const uint32_t minutes{ window.start.fromEnd ? (static_cast< uint32_t >(offPeakHours) * 60U - window.start.minutes) : window.start.minutes };
164 return minutes * 60000UL;
165}
166
172constexpr uint32_t windowEnd(const BoostWindow& window, const uint8_t offPeakHours)
173{
174 if (UNTIL_THE_END == window.length.minutes)
175 {
176 return UINT32_MAX;
177 }
178 return windowStart(window, offPeakHours) + window.length.minutes * 60000UL;
179}
180
182constexpr int32_t startInMinutes(const BoostWindow& window, const uint8_t offPeakHours)
183{
184 const int32_t periodInMin{ static_cast< int32_t >(offPeakHours * 60L) };
185 return window.start.fromEnd ? periodInMin - window.start.minutes : static_cast< int32_t >(window.start.minutes);
186}
187
189constexpr bool startsWithinOffPeak(const BoostWindow& window, const uint8_t offPeakHours)
190{
191 const int32_t startInMin{ startInMinutes(window, offPeakHours) };
192
193 return (startInMin >= 0) && (startInMin < offPeakHours * 60L);
194}
195
202constexpr bool endsWithinOffPeak(const BoostWindow& window, const uint8_t offPeakHours)
203{
204 if (UNTIL_THE_END == window.length.minutes)
205 {
206 return true;
207 }
208 return startInMinutes(window, offPeakHours) + window.length.minutes <= offPeakHours * 60L;
209}
210
212constexpr bool hasLength(const BoostWindow& window)
213{
214 return 0 != window.length.minutes;
215}
216
218struct Boost
219{
220 uint32_t start;
221 uint32_t end;
222 uint16_t local;
223 uint8_t remote;
224};
225
227template< uint8_t N, uint8_t MaxPins >
228constexpr Boost resolve(const BoostSchedule< N, MaxPins >& boosts, const uint8_t i, const uint8_t offPeakHours)
229{
230 const BoostWindow window{ boosts.getWindow(i) };
231 return Boost{ windowStart(window, offPeakHours), windowEnd(window, offPeakHours), boosts.getLocalBitmask(i), boosts.getRemoteBitmask(i) };
232}
233} // namespace DualTariff
234
235#endif // UTILS_DUALTARIFF_H
constexpr BoostSchedule(const ScheduledBoosts< 0, MaxPins > &, const Map &)
No boost in config.h either.
constexpr uint8_t size() const
constexpr BoostSchedule()=default
constexpr uint16_t getLocalBitmask(uint8_t) const
constexpr BoostWindow getWindow(uint8_t) const
constexpr uint8_t getRemoteBitmask(uint8_t) const
The scheduled boosts: a window and the bitmasks of its targets, for each entry.
constexpr BoostSchedule(const ScheduledBoosts< N, MaxPins > &boosts, const Map &map)
Builds the boosts of config.h, their codes turned into pins by map (a TargetMap).
constexpr uint8_t size() const
The number of boosts.
constexpr uint8_t getRemoteBitmask(uint8_t i) const
The remote bitmask of boost i, or 0 if out of bounds.
constexpr BoostSchedule(const BoostEntry< MaxPins >(&boosts)[N])
Builds the boosts from their entries, with plain pins.
constexpr BoostWindow getWindow(uint8_t i) const
The window of boost i, or a window of length 0 if out of bounds.
constexpr uint16_t getLocalBitmask(uint8_t i) const
The local bitmask of boost i, or 0 if out of bounds.
The scheduled boosts, as in config.h: each window with the targets it forces.
constexpr const PinList< MaxPins > & getTargets(const uint8_t i) const
The targets of boost i, as codes.
constexpr BoostWindow getWindow(const uint8_t i) const
The window of boost i.
What config.h is written with: its types, constants and helpers.
constexpr BoostStart FROM_START(const Minutes m)
A start m after the beginning of the off-peak period.
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.
uint8_t i
The windows of the scheduled boosts, in milliseconds from the start of the off-peak period.
constexpr Boost resolve(const BoostSchedule< N, MaxPins > &boosts, const uint8_t i, const uint8_t offPeakHours)
Boost i of boosts, for an off-peak period of offPeakHours.
constexpr bool hasLength(const BoostWindow &window)
Whether the boost lasts at all: a length of 0 is no boost.
constexpr bool inWindow(const uint32_t elapsed, const uint32_t start, const uint32_t end)
Whether the boost is in its window.
constexpr uint32_t windowEnd(const BoostWindow &window, const uint8_t offPeakHours)
The end of the boost window (excluded), in ms from the start of the off-peak period.
constexpr uint32_t windowStart(const BoostWindow &window, const uint8_t offPeakHours)
The start of the boost window, in ms from the start of the off-peak period.
constexpr int32_t startInMinutes(const BoostWindow &window, const uint8_t offPeakHours)
The start of the window, in minutes from the start of the off-peak period: negative if before it.
constexpr bool startsWithinOffPeak(const BoostWindow &window, const uint8_t offPeakHours)
Whether the window starts within the off-peak period: otherwise, it would never open.
constexpr bool endsWithinOffPeak(const BoostWindow &window, const uint8_t offPeakHours)
Whether a window with FOR() ends within the off-peak period (its end may be the end of the period): a...
When a set of loads is boosted: a window and the targets.
How long a boost lasts, in minutes, or UNTIL_THE_END.
uint16_t minutes
uint16_t minutes
When a load is boosted during the off-peak period.
BoostStart start
BoostLength length
A boost resolved for a given off-peak duration: its window in ms and its target masks.
#define MINUTES(value)
A compile-time number of minutes, as an integral_constant.
BoostSchedule() -> BoostSchedule< 0 >
Deduction guide: no argument, no boost.