Skip to content

Commit be2274b

Browse files
committed
Refactor diversion state handling: rename b_diversionOff to b_diversionEnabled and update related logic; enhance display functions for "OFF" and "FORC" states.
1 parent 223bc41 commit be2274b

4 files changed

Lines changed: 93 additions & 20 deletions

File tree

Mk2_fasterControl_Full/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ bool proceedLoadPrioritiesAndOverriding(const int16_t currentTemperature_x100)
258258
* @brief Checks and updates the diversion state.
259259
*
260260
* @details This function monitors the state of the diversion pin to determine whether the diversion
261-
* is active or not. If the diversion pin state changes, it updates the `b_diversionOff`
261+
* is active or not. If the diversion pin state changes, it updates the `b_diversionEnabled`
262262
* flag accordingly. Additionally, if debugging is enabled, it logs the state transitions
263263
* (e.g., "Trigger diversion OFF!" or "End diversion OFF!") to the debug output.
264264
*
@@ -284,7 +284,7 @@ void checkDiversionOnOff()
284284
previousState = pinState;
285285
#endif
286286

287-
Shared::b_diversionOff = !pinState;
287+
Shared::b_diversionEnabled = pinState;
288288
}
289289
}
290290

@@ -501,7 +501,7 @@ void loop()
501501
// this action is performed every N times around this processing loop.
502502
timerForDisplayUpdate = 0;
503503

504-
configureValueForDisplay(Shared::EDD_isActive, Shared::copyOf_divertedEnergyTotal_Wh);
504+
configureValueForDisplay(Shared::EDD_isActive, Shared::copyOf_divertedEnergyTotal_Wh, Shared::b_diversionEnabled, Shared::b_overrideLoadOn[0]);
505505
}
506506

507507
if (++perSecondTimer >= SUPPLY_FREQUENCY)

Mk2_fasterControl_Full/processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ void updatePhysicalLoadStates()
352352
}
353353
}
354354

355-
const bool bDiversionOff{ Shared::b_diversionOff };
355+
const bool bDiversionEnabled{ Shared::b_diversionEnabled };
356356
uint8_t idx{ NO_OF_DUMPLOADS };
357357
do
358358
{
359359
--idx;
360360
const auto iLoad{ loadPrioritiesAndState[idx] & loadStateMask };
361-
physicalLoadState[iLoad] = !bDiversionOff && (Shared::b_overrideLoadOn[iLoad] || (loadPrioritiesAndState[idx] & loadStateOnBit)) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF;
361+
physicalLoadState[iLoad] = bDiversionEnabled && (Shared::b_overrideLoadOn[iLoad] || (loadPrioritiesAndState[idx] & loadStateOnBit)) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF;
362362
} while (idx);
363363
}
364364

@@ -448,7 +448,7 @@ void processGridCurrentRawSample(const int16_t rawSample)
448448
*/
449449
void processDivertedCurrentRawSample(const int16_t rawSample)
450450
{
451-
if (Shared::b_diversionOff || Shared::b_overrideLoadOn[0])
451+
if (!Shared::b_diversionEnabled || Shared::b_overrideLoadOn[0])
452452
{
453453
return; // no diverted power when the load is overridden
454454
}

Mk2_fasterControl_Full/shared_var.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ inline volatile bool b_datalogEventPending{ false }; /**< async trigger to
1111
inline volatile bool b_newCycle{ false }; /**< async trigger to signal start of new main cycle based on first phase */
1212
inline volatile bool b_overrideLoadOn[NO_OF_DUMPLOADS]{}; /**< async trigger to force specific load(s) to ON */
1313
inline volatile bool b_reOrderLoads{ false }; /**< async trigger for loads re-ordering */
14-
inline volatile bool b_diversionOff{ false }; /**< async trigger to stop diversion */
14+
inline volatile bool b_diversionEnabled{ true }; /**< async trigger to stop diversion */
1515

1616
inline volatile bool EDD_isActive{ false }; /**< energy diversion detection */
1717
inline volatile uint16_t absenceOfDivertedEnergyCountInSeconds{ 0 }; /**< number of seconds without diverted energy */

Mk2_fasterControl_Full/utils_display.h

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
////////////////////////////////////////////////////////////////////////////////////////
2323

2424
inline constexpr uint8_t noOfDigitLocations{ 4 };
25-
inline constexpr uint8_t noOfPossibleCharacters{ 11 };
25+
inline constexpr uint8_t noOfPossibleCharacters{ 14 };
2626
inline constexpr uint8_t UPDATE_PERIOD_FOR_DISPLAYED_DATA{ 50 }; // mains cycles
2727
inline constexpr uint8_t DISPLAY_SHUTDOWN_IN_HOURS{ 8 }; // auto-reset after this period of inactivity
2828

29-
inline constexpr uint32_t displayShutdown_inMainsCycles{DISPLAY_SHUTDOWN_IN_HOURS * mainsCyclesPerHour };
29+
inline constexpr uint32_t displayShutdown_inMainsCycles{ DISPLAY_SHUTDOWN_IN_HOURS * mainsCyclesPerHour };
3030
inline constexpr uint8_t MAX_DISPLAY_TIME_COUNT{ 10 }; // no of processing loops between display updates
3131

3232
inline uint8_t charsForDisplay[noOfDigitLocations]{ 20, 20, 20, 20 }; // all blank
@@ -71,6 +71,9 @@ inline constexpr uint8_t digitValueMap[noOfPossibleCharacters][noOfDigitSelectio
7171
HIGH, LOW, LOW, LOW, // '8' <- element 8
7272
HIGH, LOW, LOW, HIGH, // '9' <- element 9
7373
HIGH, HIGH, HIGH, HIGH, // ' ' <- element 10
74+
LOW, HIGH, HIGH, HIGH, // 'F' <- element 11
75+
HIGH, LOW, HIGH, LOW, // 'r' <- element 12
76+
LOW, LOW, HIGH, LOW, // 'C' <- element 13
7477
};
7578

7679
// a tidy means of identifying the DP status data when accessing the above table
@@ -213,6 +216,9 @@ inline constexpr uint8_t segMap[noOfPossibleCharacters][noOfSegmentsPerDigit - 1
213216
ON, ON, ON, ON, ON, ON, ON, // '8' <- element 8
214217
ON, ON, ON, ON, OFF, ON, ON, // '9' <- element 9
215218
OFF, OFF, OFF, OFF, OFF, OFF, OFF, // ' ' <- element 10
219+
ON, OFF, OFF, ON, ON, ON, ON, // 'F' <- element 11
220+
OFF, OFF, OFF, OFF, ON, OFF, ON, // 'r' <- element 12
221+
ON, OFF, OFF, ON, ON, ON, OFF, // 'C' <- element 13
216222
};
217223

218224
/**
@@ -332,33 +338,100 @@ inline void initializeDisplay()
332338
}
333339
}
334340

341+
/**
342+
* @brief Displays "OFF" on the 7-segment display.
343+
*
344+
* @details This function configures the display to show "OFF" when diversion is disabled.
345+
* It displays the text right-aligned on the 4-digit display.
346+
* Uses the existing '0' character definition for 'O' to save memory.
347+
*
348+
* @ingroup 7SegDisplay
349+
*/
350+
inline void displayOff()
351+
{
352+
if constexpr (!(TYPE_OF_DISPLAY == DisplayType::SEG || TYPE_OF_DISPLAY == DisplayType::SEG_HW))
353+
{
354+
return;
355+
}
356+
357+
// Set display to " OFF" (right-aligned)
358+
charsForDisplay[0] = 10; // Blank
359+
charsForDisplay[1] = 0; // 'O' (reusing '0' character)
360+
charsForDisplay[2] = 12; // 'F'
361+
charsForDisplay[3] = 12; // 'F'
362+
}
363+
364+
/**
365+
* @brief Displays "FORC" on the 7-segment display for forced load override.
366+
*
367+
* @details This function configures the display to show "FORC" when a load is overridden.
368+
* Uses the existing '0' character definition for 'O' to save memory.
369+
*
370+
* @ingroup 7SegDisplay
371+
*/
372+
inline void displayForced()
373+
{
374+
if constexpr (!(TYPE_OF_DISPLAY == DisplayType::SEG || TYPE_OF_DISPLAY == DisplayType::SEG_HW))
375+
{
376+
return;
377+
}
378+
379+
// Set display to "FORC"
380+
charsForDisplay[0] = 11; // 'F'
381+
charsForDisplay[1] = 0; // 'O' (reusing '0' character)
382+
charsForDisplay[2] = 12; // 'r'
383+
charsForDisplay[3] = 13; // 'C'
384+
}
385+
335386
/**
336387
* @brief Configures the value for display on a 7-segment display.
337388
*
338-
* This function configures the value to be displayed on a 7-segment display.
339-
* It handles both active energy display and a "walking dots" display when the
340-
* energy display is not active.
389+
* @details This function controls what is shown on the 7-segment display based on
390+
* the system state. It handles multiple display modes:
391+
* - "FORC" when a load is in forced override mode
392+
* - "OFF" when diversion is disabled
393+
* - "Walking dots" when energy display is not active
394+
* - Energy values with appropriate decimal point placement
341395
*
342396
* @param _EDD_isActive A boolean indicating whether the energy display is active.
343-
* @param _ValueToDisplay The value to be displayed, represented as a 16-bit unsigned integer.
397+
* @param _ValueToDisplay The energy value to be displayed (16-bit unsigned integer).
398+
* @param _diversionEnabled A boolean indicating if diversion is enabled (default=true).
399+
* @param _loadForced A boolean indicating if a load is in forced override mode (default=false).
344400
*
345-
* When the energy display is active, the function scales the value appropriately
346-
* and assigns digits to the display characters. If the value exceeds 10,000, it
347-
* is rescaled to fit within the display's constraints. The decimal point is placed
348-
* after the first or second digit based on the value.
401+
* Display precedence order:
402+
* 1. Forced load status (shows "FOrC")
403+
* 2. Diversion enabled status (shows "OFF")
404+
* 3. Energy display inactive (shows walking dots)
405+
* 4. Energy value display
349406
*
350-
* When the energy display is not active, the function displays a "walking dots"
351-
* pattern by cycling a dot through the display positions.
407+
* For energy value display:
408+
* - Values up to 9999 show with decimal point after first digit (e.g., 1.234)
409+
* - Values above 9999 are divided by 10 and shown with decimal point after second digit (e.g., 12.34)
352410
*
353411
* @ingroup 7SegDisplay
354412
*/
355-
inline void configureValueForDisplay(const bool _EDD_isActive, const uint16_t _ValueToDisplay)
413+
inline void configureValueForDisplay(const bool _EDD_isActive, const uint16_t _ValueToDisplay, const bool _diversionEnabled = false,
414+
const bool _loadForced = false)
356415
{
357416
if constexpr (!(TYPE_OF_DISPLAY == DisplayType::SEG || TYPE_OF_DISPLAY == DisplayType::SEG_HW))
358417
{
359418
return;
360419
}
361420

421+
// Check for forced load first
422+
if (_loadForced)
423+
{
424+
displayForced();
425+
return;
426+
}
427+
428+
// If diversion is disabled, show "OFF"
429+
if (!_diversionEnabled)
430+
{
431+
displayOff();
432+
return;
433+
}
434+
362435
if (!_EDD_isActive)
363436
{
364437
static uint8_t locationOfDot{ 0 };

0 commit comments

Comments
 (0)