Skip to content

Commit

Permalink
Migrate Display_Graphic to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent ad1f1b6 commit ab8679e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
8 changes: 5 additions & 3 deletions include/Display_Graphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "defaults.h"
#include <TaskSchedulerDeclarations.h>
#include <U8g2lib.h>

enum DisplayType_t {
Expand All @@ -16,8 +17,7 @@ class DisplayGraphicClass {
DisplayGraphicClass();
~DisplayGraphicClass();

void init(DisplayType_t type, uint8_t data, uint8_t clk, uint8_t cs, uint8_t reset);
void loop();
void init(Scheduler* scheduler, DisplayType_t type, uint8_t data, uint8_t clk, uint8_t cs, uint8_t reset);
void setContrast(uint8_t contrast);
void setStatus(bool turnOn);
void setOrientation(uint8_t rotation = DISPLAY_ROTATION);
Expand All @@ -28,10 +28,13 @@ class DisplayGraphicClass {
bool enableScreensaver = true;

private:
void loop();
void printText(const char* text, uint8_t line);
void calcLineHeights();
void setFont(uint8_t line);

Task _loopTask;

U8G2* _display;

bool _displayTurnedOn;
Expand All @@ -41,7 +44,6 @@ class DisplayGraphicClass {
uint8_t _mExtra;
uint16_t _period = 1000;
uint16_t _interval = 60000; // interval at which to power save (milliseconds)
uint32_t _lastDisplayUpdate = 0;
uint32_t _previousMillis = 0;
char _fmtText[32];
bool _isLarge = false;
Expand Down
102 changes: 53 additions & 49 deletions src/Display_Graphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ DisplayGraphicClass::~DisplayGraphicClass()
delete _display;
}

void DisplayGraphicClass::init(DisplayType_t type, uint8_t data, uint8_t clk, uint8_t cs, uint8_t reset)
void DisplayGraphicClass::init(Scheduler* scheduler, DisplayType_t type, uint8_t data, uint8_t clk, uint8_t cs, uint8_t reset)
{
_display_type = type;
if (_display_type > DisplayType_t::None) {
Expand All @@ -49,6 +49,12 @@ void DisplayGraphicClass::init(DisplayType_t type, uint8_t data, uint8_t clk, ui
setContrast(DISPLAY_CONTRAST);
setStatus(true);
}

scheduler->addTask(_loopTask);
_loopTask.setCallback(std::bind(&DisplayGraphicClass::loop, this));
_loopTask.setIterations(TASK_FOREVER);
_loopTask.setInterval(_period);
_loopTask.enable();
}

void DisplayGraphicClass::calcLineHeights()
Expand Down Expand Up @@ -137,62 +143,60 @@ void DisplayGraphicClass::loop()
return;
}

if ((millis() - _lastDisplayUpdate) > _period) {

_display->clearBuffer();
bool displayPowerSave = false;

//=====> Actual Production ==========
if (Datastore.getIsAtLeastOneReachable()) {
displayPowerSave = false;
if (Datastore.getTotalAcPowerEnabled() > 999) {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_kw[_display_language], (Datastore.getTotalAcPowerEnabled() / 1000));
} else {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_w[_display_language], Datastore.getTotalAcPowerEnabled());
}
printText(_fmtText, 0);
_previousMillis = millis();
}
//<=======================

//=====> Offline ===========
else {
printText(i18n_offline[_display_language], 0);
// check if it's time to enter power saving mode
if (millis() - _previousMillis >= (_interval * 2)) {
displayPowerSave = enablePowerSafe;
}
}
//<=======================

//=====> Today & Total Production =======
snprintf(_fmtText, sizeof(_fmtText), i18n_yield_today_wh[_display_language], Datastore.getTotalAcYieldDayEnabled());
printText(_fmtText, 1);
_loopTask.setInterval(_period);

snprintf(_fmtText, sizeof(_fmtText), i18n_yield_total_kwh[_display_language], Datastore.getTotalAcYieldTotalEnabled());
printText(_fmtText, 2);
//<=======================
_display->clearBuffer();
bool displayPowerSave = false;

//=====> IP or Date-Time ========
if (!(_mExtra % 10) && NetworkSettings.localIP()) {
printText(NetworkSettings.localIP().toString().c_str(), 3);
//=====> Actual Production ==========
if (Datastore.getIsAtLeastOneReachable()) {
displayPowerSave = false;
if (Datastore.getTotalAcPowerEnabled() > 999) {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_kw[_display_language], (Datastore.getTotalAcPowerEnabled() / 1000));
} else {
// Get current time
time_t now = time(nullptr);
strftime(_fmtText, sizeof(_fmtText), i18n_date_format[_display_language], localtime(&now));
printText(_fmtText, 3);
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_w[_display_language], Datastore.getTotalAcPowerEnabled());
}
printText(_fmtText, 0);
_previousMillis = millis();
}
//<=======================

//=====> Offline ===========
else {
printText(i18n_offline[_display_language], 0);
// check if it's time to enter power saving mode
if (millis() - _previousMillis >= (_interval * 2)) {
displayPowerSave = enablePowerSafe;
}
_display->sendBuffer();
}
//<=======================

_mExtra++;
_lastDisplayUpdate = millis();
//=====> Today & Total Production =======
snprintf(_fmtText, sizeof(_fmtText), i18n_yield_today_wh[_display_language], Datastore.getTotalAcYieldDayEnabled());
printText(_fmtText, 1);

if (!_displayTurnedOn) {
displayPowerSave = true;
}
snprintf(_fmtText, sizeof(_fmtText), i18n_yield_total_kwh[_display_language], Datastore.getTotalAcYieldTotalEnabled());
printText(_fmtText, 2);
//<=======================

_display->setPowerSave(displayPowerSave);
//=====> IP or Date-Time ========
if (!(_mExtra % 10) && NetworkSettings.localIP()) {
printText(NetworkSettings.localIP().toString().c_str(), 3);
} else {
// Get current time
time_t now = time(nullptr);
strftime(_fmtText, sizeof(_fmtText), i18n_date_format[_display_language], localtime(&now));
printText(_fmtText, 3);
}
_display->sendBuffer();

_mExtra++;

if (!_displayTurnedOn) {
displayPowerSave = true;
}

_display->setPowerSave(displayPowerSave);
}

void DisplayGraphicClass::setContrast(uint8_t contrast)
Expand Down
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void setup()
// Initialize Display
MessageOutput.print("Initialize Display... ");
Display.init(
&scheduler,
static_cast<DisplayType_t>(pin.display_type),
pin.display_data,
pin.display_clk,
Expand Down Expand Up @@ -156,6 +157,4 @@ void loop()

WebApi.loop();
yield();
Display.loop();
yield();
}

0 comments on commit ab8679e

Please sign in to comment.