Files
esticky/firmware/meteogram_view.cpp
T

120 lines
4.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "meteogram_view.h"
#include <Arduino.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <GxEPD2_4G.h> // nur für die Farbwerte GxEPD_WHITE … GxEPD_BLACK
namespace {
// Diagrammfläche: oben Tagesnamen, links °C, rechts mm, unten Stundenmarken und Statuszeile
const int PLOT_LEFT = 24;
const int PLOT_RIGHT = 274;
const int PLOT_TOP = 14;
const int PLOT_BOTTOM = 94;
const int PLOT_HEIGHT = PLOT_BOTTOM - PLOT_TOP;
const float PX_PER_HOUR = float(PLOT_RIGHT - PLOT_LEFT) / METEOGRAM_HOURS;
int hourX(float hour) {
return PLOT_LEFT + lroundf(hour * PX_PER_HOUR);
}
void dottedHLine(Adafruit_GFX& g, int x0, int x1, int y, uint16_t color) {
for (int x = x0; x <= x1; x += 3) g.drawPixel(x, y, color);
}
void dashedVLine(Adafruit_GFX& g, int x, int y0, int y1, uint16_t color) {
for (int y = y0; y <= y1; y += 4) g.drawFastVLine(x, y, 2, color);
}
} // namespace
void drawMeteogram(Adafruit_GFX& gfx, const Meteogram& m, const char* location, const StatusInfo& status) {
U8G2_FOR_ADAFRUIT_GFX fonts;
fonts.begin(gfx);
char text[40];
gfx.fillScreen(GxEPD_WHITE);
// Skalen: Temperatur auf volle 5 °C mit mindestens 10 °C Spanne, Regen mindestens 1 mm pro Stunde
float tLow = m.temperature[0], tHigh = m.temperature[0], rainHigh = 0;
for (int i = 0; i < METEOGRAM_HOURS; i++) {
tLow = min(tLow, m.temperature[i]);
tHigh = max(tHigh, m.temperature[i]);
rainHigh = max(rainHigh, m.precipitation[i]);
}
int scaleLow = floorf(tLow / 5) * 5;
int scaleHigh = ceilf(tHigh / 5) * 5;
if (scaleHigh - scaleLow < 10) scaleHigh += 5;
if (scaleHigh - scaleLow < 10) scaleLow -= 5;
int gridStep = scaleHigh - scaleLow > 30 ? 10 : 5;
float rainScale = max(1.0f, ceilf(rainHigh));
auto tempY = [&](float t) {
return PLOT_BOTTOM - (int)lroundf((t - scaleLow) / (scaleHigh - scaleLow) * PLOT_HEIGHT);
};
// Regenwahrscheinlichkeit als hellgraue Fläche (0–100 % der Höhe), Regenmenge als dunkelgraue Balken
for (int i = 0; i < METEOGRAM_HOURS; i++) {
int x0 = hourX(i), x1 = hourX(i + 1);
int probabilityHeight = PLOT_HEIGHT * m.rainProbability[i] / 100;
if (probabilityHeight > 0) {
gfx.fillRect(x0, PLOT_BOTTOM - probabilityHeight, x1 - x0, probabilityHeight, GxEPD_LIGHTGREY);
}
int amountHeight = lroundf(min(m.precipitation[i] / rainScale, 1.0f) * PLOT_HEIGHT);
if (amountHeight > 0) {
gfx.fillRect(x0, PLOT_BOTTOM - amountHeight, max(1, x1 - x0 - 1), amountHeight, GxEPD_DARKGREY);
}
}
// Temperaturraster mit Beschriftung links, Regenskala rechts oben
fonts.setForegroundColor(GxEPD_DARKGREY);
useFont(fonts, u8g2_font_5x7_tf);
for (int t = scaleLow; t <= scaleHigh; t += gridStep) {
int y = tempY(t);
dottedHLine(gfx, PLOT_LEFT, PLOT_RIGHT, y, GxEPD_DARKGREY);
snprintf(text, sizeof(text), "%d°", t);
fonts.drawUTF8(PLOT_LEFT - 3 - fonts.getUTF8Width(text), y + 3, text);
}
snprintf(text, sizeof(text), "%gmm", rainScale);
fonts.drawUTF8(gfx.width() - 2 - fonts.getUTF8Width(text), PLOT_TOP + 6, text);
// Tage: Trennlinien um Mitternacht, Tagesnamen oben, Marken für 6, 12 und 18 Uhr unten
int firstWeekday = weekday(m.start);
for (int d = 0; d < METEOGRAM_DAYS; d++) {
int dayStart = hourX(d * 24), dayEnd = hourX((d + 1) * 24);
if (d > 0) gfx.drawFastVLine(dayStart, 2, PLOT_BOTTOM - 2, GxEPD_DARKGREY);
fonts.setForegroundColor(GxEPD_BLACK);
useFont(fonts, u8g2_font_helvB08_tf);
drawCentered(fonts, d == 0 ? "Heute" : DAY_NAMES[(firstWeekday + d) % 7], (dayStart + dayEnd) / 2, 10);
fonts.setForegroundColor(GxEPD_DARKGREY);
useFont(fonts, u8g2_font_5x7_tf);
for (int h = 6; h < 24; h += 6) {
int x = hourX(d * 24 + h);
gfx.drawFastVLine(x, PLOT_BOTTOM + 1, 2, GxEPD_DARKGREY);
snprintf(text, sizeof(text), "%d", h);
drawCentered(fonts, text, x, PLOT_BOTTOM + 10);
}
}
gfx.drawFastHLine(PLOT_LEFT, PLOT_BOTTOM, PLOT_RIGHT - PLOT_LEFT, GxEPD_BLACK);
// Temperaturkurve, 2 px stark, Punkte jeweils in der Stundenmitte
for (int i = 1; i < METEOGRAM_HOURS; i++) {
int xa = hourX(i - 0.5f), xb = hourX(i + 0.5f);
int ya = tempY(m.temperature[i - 1]), yb = tempY(m.temperature[i]);
gfx.drawLine(xa, ya, xb, yb, GxEPD_BLACK);
gfx.drawLine(xa, ya + 1, xb, yb + 1, GxEPD_BLACK);
}
// jetzt: gestrichelte Linie, wenn die aktuelle Zeit auf den ersten Tag fällt
if (strncmp(m.time, m.start, 10) == 0) {
float nowHour = atoi(m.time + 11) + atoi(m.time + 14) / 60.0f;
dashedVLine(gfx, hourX(nowHour), PLOT_TOP, PLOT_BOTTOM, GxEPD_BLACK);
}
snprintf(text, sizeof(text), "%s · Stand %s", location, m.time + 11);
drawStatusBar(gfx, fonts, text, status);
}