3-phase PV router
Loading...
Searching...
No Matches
General Processing

Functions and routines that handle general system processing tasks. More...

Files

file  override_masks.h
 The forced outputs: the override pins, and the scheduled boosts of the dual tariff.
 
file  pin_edges.h
 The edges of the active-LOW input pins: pressed, released, or no change.
 

Classes

class  EWMA_average< A >
 Implements an Exponentially Weighted Moving Average (EWMA). More...
 

Functions

OverrideMasks getOverrideBitmask (const bool bOffPeak, const int16_t currentTemperature_x100)
 Gets the combined bitmasks of all active override pins and dual tariff forcing.
 
void handlePerSecondTasks (bool &bOffPeak, int16_t &iTemperature_x100)
 Handles tasks that need to be executed every second.
 
bool isDiversionEnabled ()
 Reads the diversion pin.
 
bool isRouterOff ()
 Tells whether the router is OFF.
 
void logLoadPriorities ()
 Prints the load priorities to the Serial output.
 
void loop ()
 Main processor loop.
 
bool proceedDualTariffLogic ()
 Handles dual tariff state transitions and priority rotation during off-peak periods.
 
void proceedLoadPriorities (const bool bOverridden)
 Handles load priority rotation.
 
void proceedRotation ()
 Proceeds with load priority rotation.
 
template<uint8_t pin>
PinEdges readPinEdges ()
 Reads an active-LOW input pin, and tells how it changed since the previous reading.
 
void sendResults (bool bOffPeak)
 Prints or sends telemetry data logs based on the selected output format.
 
void updatePowerAndVoltageData ()
 Updates power and voltage data for all phases.
 

Detailed Description

Functions and routines that handle general system processing tasks.

This group includes functions that are not time-critical but are essential for the overall operation of the system. These tasks include monitoring system states, managing load priorities, and handling diversion logic.

Function Documentation

◆ getOverrideBitmask()

OverrideMasks getOverrideBitmask ( const bool bOffPeak,
const int16_t currentTemperature_x100 )

Gets the combined bitmasks of all active override pins and dual tariff forcing.

This function calculates the complete override bitmasks (local and remote) by combining external override pins with dual tariff automatic forcing. The bitwise OR operation automatically handles precedence where external overrides take priority over dual tariff forcing for the same pins.

Parameters
bOffPeaktrue if the system is in an off-peak period (used for dual tariff logic).
currentTemperature_x100Current temperature multiplied by 100 (used for dual tariff logic).
Returns
OverrideMasks struct containing both local (physical pins) and remote (virtual pins) bitmasks.
  • The logic is Forcing::pinForcing() (override_masks.h); this wrapper reads the pins.
  • First checks all configured external override pins (active LOW) and sets corresponding bits.
  • Local bitmask controls physical pins (loads/relays on pins 2-13).
  • Remote bitmask controls remote loads (bit n = remote load n).
  • Then applies dual tariff forcing using bitwise OR operation (local and remote).
  • OR operation ensures external overrides take precedence (1|x = 1).
  • Function is called atomically to prevent race conditions with ISR.

Definition at line 113 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handlePerSecondTasks()

void handlePerSecondTasks ( bool & bOffPeak,
int16_t & iTemperature_x100 )

Handles tasks that need to be executed every second.

This function performs various tasks that are triggered once per second, ensuring proper system operation and load management.

  • Increments the absence of diverted energy count if no energy is being diverted.
  • Toggles the watchdog pin if the feature is enabled.
  • Checks and updates the diversion state.
  • Manages load priorities and overriding based on the current temperature.
  • Updates relay durations and proceeds with relay state transitions if relay diversion is enabled.
Note
Override pins are checked at 1 Hz (once per second) for debouncing and to minimize ISR communication overhead. This means override pin changes have a latency of 0-1 second before taking effect. This is acceptable since override pins are typically used for manual control or scheduled operations (e.g., dual tariff forcing) rather than time-critical automatic control.
Parameters
bOffPeakReference to the off-peak state flag.
iTemperature_x100Current temperature multiplied by 100 (default to 0 if temperature sensing is disabled).

Definition at line 512 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDiversionEnabled()

bool isDiversionEnabled ( )

Reads the diversion pin.

Returns
true unless the diversion pin is LOW. With diversion OFF, the surplus is not diverted any more, but forced loads and relays stay ON.

Definition at line 155 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRouterOff()

bool isRouterOff ( )

Tells whether the router is OFF.

Returns
true in calibration mode, or while the router OFF pin is LOW. With the router OFF, every load and relay is OFF, forcing included.

Definition at line 182 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logLoadPriorities()

void logLoadPriorities ( )
inline

Prints the load priorities to the Serial output.

This function logs the current load priorities and states to the Serial output at startup and after each priority rotation. It provides a detailed view of the load configuration and their respective priorities.

  • Each load's priority and state are printed in a human-readable format.
  • Like every text message, it is only printed in human-readable output mode.

Definition at line 660 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loop()

void loop ( )

Main processor loop.

This function handles non-time-critical tasks such as load management, telemetry updates, and temperature sensing. It processes ADC data through flags set by the ISR and ensures proper system operation.

  • Executes tasks triggered by the b_newMainsCycle flag, which is set once per mains cycle; with temperature sensing, the cycle's 1-Wire transaction first, before any other work.
  • Handles per-second tasks such as load priority management and diversion state updates.
  • Processes data logging events and updates power, voltage, and temperature data.
  • Sends telemetry results and updates relay states if relay diversion is enabled.

Definition at line 564 of file main.cpp.

Here is the call graph for this function:

◆ proceedDualTariffLogic()

bool proceedDualTariffLogic ( )

Handles dual tariff state transitions and priority rotation during off-peak periods.

This function manages dual tariff state detection and triggers priority rotation when transitioning to off-peak periods. The actual load forcing is handled in getOverrideBitmask().

Returns
true if the system is in a low tariff (off-peak) period.
false if the system is in a high tariff (on-peak) period.
  • Detects transitions between off-peak and on-peak periods using the dual tariff pin.
  • Triggers automatic priority rotation when entering off-peak period.
  • Logs transitions between tariff periods for debugging purposes.
  • Load forcing logic (including temperature conditions) is handled atomically in getOverrideBitmask().

Definition at line 249 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proceedLoadPriorities()

void proceedLoadPriorities ( const bool bOverridden)

Handles load priority rotation.

This function manages load priority rotation behavior based on the system configuration. It supports priority rotation via pin control or automatic rotation. Override logic is handled in getOverrideBitmask().

Parameters
bOverriddentrue while a load, relay or remote load is forced: no rotation then.
  • The rotation pin works with or without dual tariff.
  • Automatic rotation: after a defined period without diversion; with dual tariff, proceedDualTariffLogic rotates when off-peak starts instead.
  • The rotation pin is read every second, forced or not: a press during forcing is dropped, not carried over to its end.
  • Override logic (external pins + dual tariff forcing) is handled atomically in getOverrideBitmask().

Definition at line 294 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ proceedRotation()

void proceedRotation ( )

Proceeds with load priority rotation.

This function triggers the rotation of load priorities and waits until the rotation is completed. It ensures that the new load priorities are logged after the rotation.

  • Sets the b_reOrderLoads flag to initiate the rotation process.
  • Waits in a loop until the rotation is completed by the ISR.
  • Logs the updated load priorities after the rotation.

Definition at line 218 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readPinEdges()

template<uint8_t pin>
PinEdges readPinEdges ( )

Reads an active-LOW input pin, and tells how it changed since the previous reading.

Template Parameters
pinthe input pin: each one keeps its own previous level, released at startup.
Returns
its state and its edges.

Read each pin every second, whether its edges are used or not: a previous level left stale would turn a press made long ago into an edge now.

Definition at line 142 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendResults()

void sendResults ( bool bOffPeak)
inline

Prints or sends telemetry data logs based on the selected output format.

This function handles the transmission of telemetry data in various formats, such as human-readable text, IoT telemetry, or JSON format. It also ensures that the first incomplete datalogging event is skipped during startup.

Parameters
bOffPeakIndicates whether the system is in an off-peak tariff period.
  • If an RF module is configured but did not start (missing, unpowered or badly wired), it retries its initialization, once per datalog period: RF logging and remote loads only send once it is up.
  • If RF communication is enabled, it sends RF data.
  • Depending on the SERIAL_OUTPUT_TYPE, it prints data in text format, sends telemetry data, or outputs data in JSON format.
  • Skips the first datalogging event during startup to avoid incomplete data.

Definition at line 602 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updatePowerAndVoltageData()

void updatePowerAndVoltageData ( )

Updates power and voltage data for all phases.

This function calculates the power and voltage for each phase based on the accumulated data during the datalogging period. It also updates the total power.

  • Computes the power for each phase using the accumulated power data and calibration factors.
  • Calculates the RMS voltage for each phase using the accumulated voltage squared data.
  • Integer arithmetic only (datalog_math.h): the calibration factors are fixed-point constants.
  • Updates the total power by summing the power of all phases.

Definition at line 385 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function: