69template<
typename Output = NoOutput >
80 explicit constexpr Relay(
const uint8_t _relay_pin)
95 constexpr Relay(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
112 constexpr Relay(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF)
174 return (
minON != UINT16_MAX) && (
minOFF != UINT16_MAX);
208 bool proceed_relay(
const int32_t currentAvgPower,
const uint16_t overrideBitmask,
const bool onAllowed =
true)
const
210 const bool forced{
isForced(overrideBitmask) };
249 static constexpr uint16_t
toSeconds(
const uint16_t minutes)
251 return minutes * 60UL > UINT16_MAX ? UINT16_MAX :
static_cast< uint16_t
>(minutes * 60UL);
257 return value > 0 ? -value : value;
260 bool isForced(
const uint16_t overrideBitmask)
const
313template<
typename R, u
int8_t N, u
int8_t D >
322 explicit constexpr Engine(
const R (&ref)[N])
374#if defined(__DOXYGEN__)
377 void inc_duration() const __attribute__((optimize(
"-O3")));
394 void proceed_relays(
const uint16_t overrideBitmask,
const bool diversionEnabled =
true)
const
402 if (!diversionEnabled)
404 bool anyChanges{
false };
408 anyChanges |=
relay[--idx].proceed_forced_only(overrideBitmask);
417 const auto currentAvgPower{
ewma_average.getAverageT() };
419 const bool importing{ currentAvgPower > 0 };
424 for (uint8_t step = 0; step != 2 * N; ++step)
426 const bool down{ step < N };
427 if (!down && importing)
432 const uint8_t idx{
static_cast< uint8_t
>(down ? N - 1 - step : step - N) };
433 if (
relay[idx].proceed_relay(currentAvgPower, overrideBitmask, importing || !down))
446 template< uint8_t... I >
463 relay[--idx].inc_duration();
Implements an Exponentially Weighted Moving Average (EWMA).
static void update_average(int16_t currentPower)
Update the sliding average.
constexpr auto size() const
Get the number of relays.
void inc_duration() const
Increment the duration's state of each relay.
constexpr Engine(const R(&ref)[N], index_sequence< I... >)
Copies each relay of ref: an array cannot be initialized from another.
static auto get_average()
Get the current average.
const relayOutput relay[N]
constexpr const auto & get_relay(uint8_t idx) const
Get the relay object.
void proceed_relays(const uint16_t overrideBitmask, const bool diversionEnabled=true) const
Proceed all relays: OFF in decreasing order, ON in increasing order.
constexpr Engine(const R(&ref)[N])
Construct a list of relays.
static EWMA_average< D *60/DATALOG_PERIOD_IN_SECONDS > ewma_average
static constexpr int16_t negativeOf(const int16_t value)
-abs(value), without the abs() macro of Arduino.h.
constexpr auto get_minON() const
Get the minimum ON-time in seconds.
bool proceed_forced_only(const uint16_t overrideBitmask) const
Proceed with the relay while diversion is disabled: ON only while forced.
constexpr auto get_minOFF() const
Get the minimum OFF-time in seconds.
constexpr auto get_surplusThreshold() const
Get the surplus threshold which will turns ON the relay.
auto isRelayON() const
Return the state.
const int16_t importThreshold
bool try_switch(const bool on) const
Turn the relay ON or OFF if the 'time' condition is met.
void inc_duration() const
Increment the duration of the current state.
constexpr Relay(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF)
Construct a new relay Config object with custom parameters.
bool isForced(const uint16_t overrideBitmask) const
const int16_t surplusThreshold
constexpr auto get_importThreshold() const
Get the import threshold which will turns OFF the relay.
constexpr auto get_pin() const
Get the control pin of the relay.
bool proceed_relay(const int32_t currentAvgPower, const uint16_t overrideBitmask, const bool onAllowed=true) const
Proceed with the relay.
constexpr Relay(const uint8_t _relay_pin)
Construct a new relay Config object with default parameters.
static constexpr uint16_t toSeconds(const uint16_t minutes)
Minutes to seconds, in 16 bits.
constexpr Relay(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
Construct a new relay Config object with default/custom parameters.
constexpr bool hasValidMinimumTimes() const
Whether both minimum times fit in 16 bits of seconds (1092 minutes at most).
Basic configuration values to be set by the end-user.
constexpr uint8_t DATALOG_PERIOD_IN_SECONDS
This file implements an Exponentially Weighted Moving Average template class.
typename make_index_sequence_impl< N >::type make_index_sequence
index_sequence< 0, 1, ..., N - 1 >.
The relay engine without its I/O.
The default output of a relay: none, the state only.
static void switched(uint8_t, bool)
Called on each change, before the state changes.
The indexes I..., as a type.
Some useful but missing stl functions templates.
Bit manipulation utility functions.
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.