Skip to content

Commit

Permalink
Feature: Add screensavermode also to diagram drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOberhumer authored and tbnobody committed Dec 24, 2023
1 parent 733a566 commit ac42752
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
11 changes: 6 additions & 5 deletions include/Display_Graphic_Diagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

#define CHART_HEIGHT 20 // chart area hight in pixels
#define CHART_WIDTH 47 // chart area width in pixels
#define DIAG_POSX 80 // position were Diag is drawn at

// Left-Upper position of diagram is drawn
// (text of Y-axis is display left of that pos)
#define DIAG_POSX 80
#define DIAG_POSY 0

class DisplayGraphicDiagramClass {
public:
DisplayGraphicDiagramClass();

void init(Scheduler& scheduler, U8G2* display);
void redraw();
void redraw(uint8_t screenSaverOffsetX);

void updatePeriod();

Expand All @@ -34,6 +37,4 @@ class DisplayGraphicDiagramClass {

float _iRunningAverage = 0;
uint16_t _iRunningAverageCnt = 0;

uint8_t _graphPosX = DIAG_POSX;
};
};
12 changes: 7 additions & 5 deletions src/Display_Graphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ void DisplayGraphicClass::loop()
if (Datastore.getIsAtLeastOneReachable()) {
displayPowerSave = false;
if (_isLarge) {
_diagram.redraw();
uint8_t screenSaverOffsetX = enableScreensaver ? (_mExtra % 7) : 0;
_diagram.redraw(screenSaverOffsetX);
}
if (Datastore.getTotalAcPowerEnabled() > 999) {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_kw[_display_language], (Datastore.getTotalAcPowerEnabled() / 1000));
float watts = Datastore.getTotalAcPowerEnabled();
if (watts > 999) {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_kw[_display_language], watts / 1000);
} else {
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_w[_display_language], Datastore.getTotalAcPowerEnabled());
snprintf(_fmtText, sizeof(_fmtText), i18n_current_power_w[_display_language], watts);
}
printText(_fmtText, 0);
_previousMillis = millis();
Expand Down Expand Up @@ -224,4 +226,4 @@ void DisplayGraphicClass::setStatus(const bool turnOn)
_displayTurnedOn = turnOn;
}

DisplayGraphicClass Display;
DisplayGraphicClass Display;
35 changes: 19 additions & 16 deletions src/Display_Graphic_Diagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ void DisplayGraphicDiagramClass::dataPointLoop()
_iRunningAverage = 0;
_iRunningAverageCnt = 0;
}

if (Configuration.get().Display.ScreenSaver) {
_graphPosX = DIAG_POSX - (_graphValuesCount % 2);
}
}

uint32_t DisplayGraphicDiagramClass::getSecondsPerDot()
Expand All @@ -64,41 +60,48 @@ void DisplayGraphicDiagramClass::updatePeriod()
_dataPointTask.setInterval(getSecondsPerDot() * TASK_SECOND);
}

void DisplayGraphicDiagramClass::redraw()
void DisplayGraphicDiagramClass::redraw(uint8_t screenSaverOffsetX)
{
uint8_t graphPosX = DIAG_POSX + ((screenSaverOffsetX > 3) ? 1 : 0); // screenSaverOffsetX expected to be in range 0..6
uint8_t graphPosY = DIAG_POSY;

// draw diagram axis
_display->drawVLine(_graphPosX, graphPosY, CHART_HEIGHT);
_display->drawHLine(_graphPosX, graphPosY + CHART_HEIGHT - 1, CHART_WIDTH);
_display->drawVLine(graphPosX, graphPosY, CHART_HEIGHT);
_display->drawHLine(graphPosX, graphPosY + CHART_HEIGHT - 1, CHART_WIDTH);

_display->drawLine(_graphPosX + 1, graphPosY + 1, _graphPosX + 2, graphPosY + 2); // UP-arrow
_display->drawLine(_graphPosX + CHART_WIDTH - 3, graphPosY + CHART_HEIGHT - 3, _graphPosX + CHART_WIDTH - 2, graphPosY + CHART_HEIGHT - 2); // LEFT-arrow
_display->drawLine(_graphPosX + CHART_WIDTH - 3, graphPosY + CHART_HEIGHT + 1, _graphPosX + CHART_WIDTH - 2, graphPosY + CHART_HEIGHT); // LEFT-arrow
_display->drawLine(graphPosX + 1, graphPosY + 1, graphPosX + 2, graphPosY + 2); // UP-arrow
_display->drawLine(graphPosX - 2, graphPosY + 2, graphPosX - 1, graphPosY + 1); // UP-arrow
_display->drawLine(graphPosX + CHART_WIDTH - 3, graphPosY + CHART_HEIGHT - 3, graphPosX + CHART_WIDTH - 2, graphPosY + CHART_HEIGHT - 2); // LEFT-arrow
_display->drawLine(graphPosX + CHART_WIDTH - 3, graphPosY + CHART_HEIGHT + 1, graphPosX + CHART_WIDTH - 2, graphPosY + CHART_HEIGHT); // LEFT-arrow

// draw AC value
_display->setFont(u8g2_font_tom_thumb_4x6_mr);
_display->setFont(u8g2_font_tom_thumb_4x6_mr); // 4 pixels per char
char fmtText[7];
const float maxWatts = *std::max_element(_graphValues.begin(), _graphValues.end());
snprintf(fmtText, sizeof(fmtText), "%dW", static_cast<uint16_t>(maxWatts));
if (maxWatts > 999) {
snprintf(fmtText, sizeof(fmtText), "%2.1fkW", maxWatts / 1000);
} else {
snprintf(fmtText, sizeof(fmtText), "%dW", static_cast<uint16_t>(maxWatts));
}
const uint8_t textLength = strlen(fmtText);
_display->drawStr(_graphPosX - (textLength * 4), graphPosY + 5, fmtText);
const uint8_t space_and_arrow_pixels = 2;
_display->drawStr(graphPosX - space_and_arrow_pixels - (textLength * 4), graphPosY + 5, fmtText);

// draw chart
const float scaleFactor = maxWatts / CHART_HEIGHT;
uint8_t axisTick = 1;
for (int i = 0; i < _graphValuesCount; i++) {
if (scaleFactor > 0) {
if (i == 0) {
_display->drawPixel(_graphPosX + 1 + i, graphPosY + CHART_HEIGHT - ((_graphValues[i] / scaleFactor) + 0.5)); // + 0.5 to round mathematical
_display->drawPixel(graphPosX + 1 + i, graphPosY + CHART_HEIGHT - ((_graphValues[i] / scaleFactor) + 0.5)); // + 0.5 to round mathematical
} else {
_display->drawLine(_graphPosX + i, graphPosY + CHART_HEIGHT - ((_graphValues[i - 1] / scaleFactor) + 0.5), _graphPosX + 1 + i, graphPosY + CHART_HEIGHT - ((_graphValues[i] / scaleFactor) + 0.5));
_display->drawLine(graphPosX + i, graphPosY + CHART_HEIGHT - ((_graphValues[i - 1] / scaleFactor) + 0.5), graphPosX + 1 + i, graphPosY + CHART_HEIGHT - ((_graphValues[i] / scaleFactor) + 0.5));
}
}

// draw one tick per hour to the x-axis
if (i * getSecondsPerDot() > (3600u * axisTick)) {
_display->drawPixel(_graphPosX + 1 + i, graphPosY + CHART_HEIGHT);
_display->drawPixel(graphPosX + 1 + i, graphPosY + CHART_HEIGHT);
axisTick++;
}
}
Expand Down

0 comments on commit ac42752

Please sign in to comment.