Files
esticky/firmware/weather_view.cpp

237 lines
8.5 KiB
C++

#include "weather_view.h"
#include <Arduino.h>
#include <math.h>
#include <stdlib.h>
#include <GxEPD2_4G.h> // nur für die Farbwerte GxEPD_WHITE … GxEPD_BLACK
namespace {
// Layout (Querformat 296x128): links aktuelles Wetter, rechts drei Tage, unten Statuszeile
const int LEFT_PANEL_WIDTH = 150;
const int DAY_COLUMN_WIDTH = 48;
enum class Sky { Clear, PartlyCloudy, Overcast, Fog, Drizzle, Rain, Snow, Thunder };
Sky skyFor(int code) {
if (code == 0) return Sky::Clear;
if (code <= 2) return Sky::PartlyCloudy;
if (code == 3) return Sky::Overcast;
if (code == 45 || code == 48) return Sky::Fog;
if (code >= 51 && code <= 57) return Sky::Drizzle;
if ((code >= 61 && code <= 67) || (code >= 80 && code <= 82)) return Sky::Rain;
if ((code >= 71 && code <= 77) || code == 85 || code == 86) return Sky::Snow;
if (code >= 95) return Sky::Thunder;
return Sky::Overcast;
}
const char* conditionText(int code, bool isDay) {
switch (code) {
case 0: return isDay ? "Sonnig" : "Klar";
case 1: return "Überwiegend klar";
case 2: return "Teils bewölkt";
case 3: return "Bedeckt";
case 45: case 48: return "Nebel";
case 51: case 53: case 55: return "Nieselregen";
case 56: case 57: return "Eisiger Niesel";
case 61: return "Leichter Regen";
case 63: return "Regen";
case 65: return "Starker Regen";
case 66: case 67: return "Eisregen";
case 71: return "Leichter Schnee";
case 73: return "Schnee";
case 75: return "Starker Schnee";
case 77: return "Schneegriesel";
case 80: case 81: return "Regenschauer";
case 82: return "Starke Schauer";
case 85: case 86: return "Schneeschauer";
case 95: return "Gewitter";
case 96: case 99: return "Gewitter, Hagel";
default: return "Unbekannt";
}
}
void formatTemperature(char* buffer, size_t size, float temperature) {
snprintf(buffer, size, "%ld°", lroundf(temperature));
}
// --- Wettersymbole; (cx, cy) ist die Mitte, size die Kantenlänge des Symbolfelds ---
void cloudShape(Adafruit_GFX& g, int cx, int cy, int w, int inset, uint16_t color) {
int h = w * 0.32f;
g.fillRoundRect(cx - w / 2 + inset, cy + inset, w - 2 * inset, h - 2 * inset, h / 2 - inset, color);
g.fillCircle(cx - w * 0.20f, cy + w * 0.05f, w * 0.20f - inset, color);
g.fillCircle(cx + w * 0.12f, cy - w * 0.02f, w * 0.28f - inset, color);
}
// Wolke mit schwarzem Umriss; die Wolke ist etwa 0,62·w hoch, (cx, cy) liegt in ihrer Mitte
void cloud(Adafruit_GFX& g, int cx, int cy, int w, uint16_t fill) {
int outline = w >= 40 ? 3 : 2;
cloudShape(g, cx, cy, w, 0, GxEPD_BLACK);
cloudShape(g, cx, cy, w, outline, fill);
}
void sun(Adafruit_GFX& g, int cx, int cy, int r) {
int rayStart = r + 3;
int rayEnd = r + 3 + max(3, r / 2);
for (int i = 0; i < 8; i++) {
float a = i * PI / 4;
int x1 = cx + cosf(a) * rayStart, y1 = cy + sinf(a) * rayStart;
int x2 = cx + cosf(a) * rayEnd, y2 = cy + sinf(a) * rayEnd;
g.drawLine(x1, y1, x2, y2, GxEPD_BLACK);
g.drawLine(x1 + 1, y1, x2 + 1, y2, GxEPD_BLACK);
g.drawLine(x1, y1 + 1, x2, y2 + 1, GxEPD_BLACK);
}
g.fillCircle(cx, cy, r, GxEPD_BLACK);
g.fillCircle(cx, cy, r - 2, GxEPD_LIGHTGREY);
}
void moon(Adafruit_GFX& g, int cx, int cy, int r) {
g.fillCircle(cx, cy, r, GxEPD_BLACK);
g.fillCircle(cx, cy, r - 2, GxEPD_LIGHTGREY);
g.fillCircle(cx + r * 0.55f, cy - r * 0.35f, r * 0.8f, GxEPD_WHITE);
}
void rainDrops(Adafruit_GFX& g, int cx, int top, int size, int count) {
int length = size * 0.18f;
for (int i = 0; i < count; i++) {
int x = cx + (i - (count - 1) / 2.0f) * size * 0.22f;
for (int t = 0; t < 2; t++) {
g.drawLine(x + t, top, x + t - length / 3, top + length, GxEPD_BLACK);
}
}
}
void snowFlakes(Adafruit_GFX& g, int cx, int top, int size) {
int arm = max(2, (int)(size * 0.06f));
for (int i = 0; i < 3; i++) {
int x = cx + (i - 1) * size * 0.24f;
int y = top + arm + (i % 2) * arm;
g.drawLine(x, y - arm, x, y + arm, GxEPD_BLACK);
g.drawLine(x - arm, y - arm / 2, x + arm, y + arm / 2, GxEPD_BLACK);
g.drawLine(x - arm, y + arm / 2, x + arm, y - arm / 2, GxEPD_BLACK);
}
}
void lightningBolt(Adafruit_GFX& g, int cx, int top, int size) {
int s = size;
g.fillTriangle(cx + s * 0.06f, top, cx - s * 0.08f, top + s * 0.16f, cx + s * 0.03f, top + s * 0.16f, GxEPD_BLACK);
g.fillTriangle(cx - s * 0.03f, top + s * 0.12f, cx + s * 0.08f, top + s * 0.12f, cx - s * 0.06f, top + s * 0.32f,
GxEPD_BLACK);
}
void fogBars(Adafruit_GFX& g, int cx, int cy, int size) {
int thickness = max(2, (int)(size * 0.07f));
for (int i = 0; i < 4; i++) {
int w = size * (i % 2 ? 0.62f : 0.80f);
int y = cy - size * 0.27f + i * size * 0.18f;
int x = cx - w / 2 + (i % 2 ? size * 0.06f : 0);
g.fillRoundRect(x, y, w, thickness, thickness / 2, GxEPD_DARKGREY);
}
}
void weatherIcon(Adafruit_GFX& g, int code, bool isDay, int cx, int cy, int size) {
int cloudWidth = size * 0.8f;
int precipCloudY = cy - size * 0.14f; // Wolke etwas höher, darunter Niederschlag
int precipTop = cy + size * 0.22f;
switch (skyFor(code)) {
case Sky::Clear:
if (isDay) sun(g, cx, cy, size * 0.24f);
else moon(g, cx, cy, size * 0.30f);
break;
case Sky::PartlyCloudy:
if (isDay) sun(g, cx - size * 0.16f, cy - size * 0.14f, size * 0.17f);
else moon(g, cx - size * 0.14f, cy - size * 0.14f, size * 0.22f);
cloud(g, cx + size * 0.08f, cy + size * 0.12f, size * 0.72f, GxEPD_WHITE);
break;
case Sky::Overcast:
cloud(g, cx + size * 0.12f, cy - size * 0.10f, size * 0.62f, GxEPD_DARKGREY);
cloud(g, cx - size * 0.06f, cy + size * 0.08f, cloudWidth, GxEPD_LIGHTGREY);
break;
case Sky::Fog:
fogBars(g, cx, cy, size);
break;
case Sky::Drizzle:
cloud(g, cx, precipCloudY, cloudWidth, GxEPD_LIGHTGREY);
rainDrops(g, cx, precipTop, size, 2);
break;
case Sky::Rain:
cloud(g, cx, precipCloudY, cloudWidth, GxEPD_DARKGREY);
rainDrops(g, cx, precipTop, size, 3);
break;
case Sky::Snow:
cloud(g, cx, precipCloudY, cloudWidth, GxEPD_LIGHTGREY);
snowFlakes(g, cx, precipTop, size);
break;
case Sky::Thunder:
cloud(g, cx, precipCloudY, cloudWidth, GxEPD_DARKGREY);
lightningBolt(g, cx, precipTop - size * 0.04f, size);
break;
}
}
void raindrop(Adafruit_GFX& g, int cx, int bottom, uint16_t color) {
g.fillCircle(cx, bottom - 2, 2, color);
g.fillTriangle(cx - 2, bottom - 2, cx + 2, bottom - 2, cx, bottom - 7, color);
}
} // namespace
void drawWeather(Adafruit_GFX& gfx, const Weather& weather, const char* location, const StatusInfo& status) {
U8G2_FOR_ADAFRUIT_GFX fonts;
fonts.begin(gfx);
char text[40];
gfx.fillScreen(GxEPD_WHITE);
// links: aktuelles Wetter
weatherIcon(gfx, weather.weatherCode, weather.isDay, 36, 38, 64);
fonts.setForegroundColor(GxEPD_BLACK);
useFont(fonts, u8g2_font_logisoso38_tf);
formatTemperature(text, sizeof(text), weather.temperature);
fonts.drawUTF8(76, 58, text);
useFont(fonts, u8g2_font_helvB12_tf);
fonts.drawUTF8(6, 88, conditionText(weather.weatherCode, weather.isDay));
useFont(fonts, u8g2_font_helvR08_tf);
fonts.setForegroundColor(GxEPD_DARKGREY);
snprintf(text, sizeof(text), "Wind %ld km/h · Feuchte %d %%", lroundf(weather.windSpeed), weather.humidity);
fonts.drawUTF8(6, 104, text);
gfx.drawFastVLine(LEFT_PANEL_WIDTH, 6, STATUS_LINE_Y - 12, GxEPD_DARKGREY);
// rechts: drei Tage
for (int i = 0; i < FORECAST_DAYS; i++) {
const DayForecast& day = weather.days[i];
int centerX = LEFT_PANEL_WIDTH + 2 + i * DAY_COLUMN_WIDTH + DAY_COLUMN_WIDTH / 2;
fonts.setForegroundColor(GxEPD_BLACK);
useFont(fonts, u8g2_font_helvB10_tf);
drawCentered(fonts, i == 0 ? "Heute" : DAY_NAMES[weekday(day.date)], centerX, 15);
weatherIcon(gfx, day.weatherCode, true, centerX, 38, 34);
useFont(fonts, u8g2_font_helvB12_tf);
formatTemperature(text, sizeof(text), day.tempMax);
drawCentered(fonts, text, centerX, 74);
fonts.setForegroundColor(GxEPD_DARKGREY);
useFont(fonts, u8g2_font_helvR10_tf);
formatTemperature(text, sizeof(text), day.tempMin);
drawCentered(fonts, text, centerX, 90);
useFont(fonts, u8g2_font_helvR08_tf);
snprintf(text, sizeof(text), "%d %%", day.rainProbability);
int textWidth = fonts.getUTF8Width(text);
int startX = centerX - (textWidth + 7) / 2;
raindrop(gfx, startX + 2, 104, GxEPD_DARKGREY);
fonts.drawUTF8(startX + 7, 104, text);
}
snprintf(text, sizeof(text), "%s · Stand %s", location, weather.time + 11);
drawStatusBar(gfx, fonts, text, status);
}