From c4022e45f95f74760ccb215453cf6ca8231c36ee Mon Sep 17 00:00:00 2001 From: Moritz Wirger Date: Sun, 13 Aug 2023 01:00:47 +0200 Subject: [PATCH] Fix weather icons exception (#114) --- ESP32_AP-Flasher/include/contentmanager.h | 4 +- ESP32_AP-Flasher/src/contentmanager.cpp | 45 +++++++++++------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/ESP32_AP-Flasher/include/contentmanager.h b/ESP32_AP-Flasher/include/contentmanager.h index 13d3aeb4..64e99979 100644 --- a/ESP32_AP-Flasher/include/contentmanager.h +++ b/ESP32_AP-Flasher/include/contentmanager.h @@ -23,8 +23,8 @@ void drawString(TFT_eSprite &spr, String content, int16_t posx, int16_t posy, St void initSprite(TFT_eSprite &spr, int w, int h, imgParam &imageParams); void drawDate(String &filename, tagRecord *&taginfo, imgParam &imageParams); void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord *&taginfo, imgParam &imageParams); -void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams); -void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams); +void drawWeather(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams); +void drawForecast(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams); int getImgURL(String &filename, String URL, time_t fetched, imgParam &imageParams, String MAC); bool getRssFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams); bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams); diff --git a/ESP32_AP-Flasher/src/contentmanager.cpp b/ESP32_AP-Flasher/src/contentmanager.cpp index 53d8a023..0daa31d6 100644 --- a/ESP32_AP-Flasher/src/contentmanager.cpp +++ b/ESP32_AP-Flasher/src/contentmanager.cpp @@ -593,28 +593,28 @@ void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord /// @param id Icon identifier/index /// @param isNight Use night icons (true) or not (false) /// @return String reference to icon -const String &getWeatherIcon(const uint8_t id, const bool isNight = false) { - static const String weatherIcons[] = {"\uf00d", "\uf00c", "\uf002", "\uf013", "\uf013", "\uf014", "", "", "\uf014", "", "", - "\uf01a", "", "\uf01a", "", "\uf01a", "\uf017", "\uf017", "", "", "", - "\uf019", "", "\uf019", "", "\uf019", "\uf015", "\uf015", "", "", "", - "\uf01b", "", "\uf01b", "", "\uf01b", "", "\uf076", "", "", "\uf01a", - "\uf01a", "\uf01a", "", "", "\uf064", "\uf064", "", "", "", "", - "", "", "", "", "\uf01e", "\uf01d", "", "", "\uf01e"}; +const String getWeatherIcon(const uint8_t id, const bool isNight = false) { + const String weatherIcons[] = {"\uf00d", "\uf00c", "\uf002", "\uf013", "\uf013", "\uf014", "", "", "\uf014", "", "", + "\uf01a", "", "\uf01a", "", "\uf01a", "\uf017", "\uf017", "", "", "", + "\uf019", "", "\uf019", "", "\uf019", "\uf015", "\uf015", "", "", "", + "\uf01b", "", "\uf01b", "", "\uf01b", "", "\uf076", "", "", "\uf01a", + "\uf01a", "\uf01a", "", "", "\uf064", "\uf064", "", "", "", "", + "", "", "", "", "\uf01e", "\uf01d", "", "", "\uf01e"}; if (isNight && id <= 3) { - static const String nightIcons[] = {"\uf02e", "\uf083", "\uf086"}; + const String nightIcons[] = {"\uf02e", "\uf083", "\uf086"}; return nightIcons[id]; } return weatherIcons[id]; } -void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams) { +void drawWeather(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams) { wsLog("get weather"); getLocation(cfgobj); - String lat = cfgobj["#lat"]; - String lon = cfgobj["#lon"]; - String tz = cfgobj["#tz"]; + const String lat = cfgobj["#lat"]; + const String lon = cfgobj["#lon"]; + const String tz = cfgobj["#tz"]; StaticJsonDocument<1000> doc; const bool success = util::httpGetJson("https://api.open-meteo.com/v1/forecast?latitude=" + lat + "&longitude=" + lon + "¤t_weather=true&windspeed_unit=ms&timezone=" + tz, doc, 5000); @@ -626,7 +626,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP const double temperature = currentWeather["temperature"].as(); const int windspeed = currentWeather["windspeed"].as(); const int winddirection = currentWeather["winddirection"].as(); - const uint8_t isday = currentWeather["is_day"].as(); + const bool isNight = currentWeather["is_day"].as() == 0; uint8_t weathercode = currentWeather["weathercode"].as(); if (weathercode > 40) weathercode -= 40; const int beaufort = windSpeedToBeaufort(windspeed); @@ -634,12 +634,12 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP doc.clear(); if (taginfo->hwType == SOLUM_SEG_UK) { - static const String weatherText[] = {"sun", "sun", "sun", "CLDY", "CLDY", "FOG", "", "", "FOG", "", "", - "DRZL", "", "DRZL", "", "DRZL", "ice", "ice", "", "", "", - "rain", "", "rain", "", "rain", "ice", "ice", "", "", "", - "SNOW", "", "SNOW", "", "SNOW", "", "SNOW", "", "", "rain", - "rain", "rain", "", "", "SNOW", "SNOW", "", "", "", "", - "", "", "", "", "STRM", "HAIL", "", "", "HAIL"}; + const String weatherText[] = {"sun", "sun", "sun", "CLDY", "CLDY", "FOG", "", "", "FOG", "", "", + "DRZL", "", "DRZL", "", "DRZL", "ice", "ice", "", "", "", + "rain", "", "rain", "", "rain", "ice", "ice", "", "", "", + "SNOW", "", "SNOW", "", "SNOW", "", "SNOW", "", "", "rain", + "rain", "rain", "", "", "SNOW", "SNOW", "", "", "", "", + "", "", "", "", "STRM", "HAIL", "", "", "HAIL"}; if (temperature < -9.9) { sprintf(imageParams.segments, "%3d^%2d%-4.4s", static_cast(temperature), beaufort, weatherText[weathercode].c_str()); imageParams.symbols = 0x00; @@ -670,7 +670,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP ? TFT_RED : TFT_BLACK; const auto &icon = doc["icon"]; - drawString(spr, getWeatherIcon(weathercode, isday == 0), icon[0], icon[1], "/fonts/weathericons.ttf", icon[3], iconcolor, icon[2]); + drawString(spr, getWeatherIcon(weathercode, isNight), icon[0], icon[1], "/fonts/weathericons.ttf", icon[3], iconcolor, icon[2]); const auto &dir = doc["dir"]; drawString(spr, windDirectionIcon(winddirection), dir[0], dir[1], "/fonts/weathericons.ttf", TC_DATUM, TFT_BLACK, dir[2]); if (weathercode > 10) { @@ -682,9 +682,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP spr.deleteSprite(); } -void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams) { - TFT_eSprite spr = TFT_eSprite(&tft); - +void drawForecast(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams) { wsLog("get weather"); getLocation(cfgobj); @@ -698,6 +696,7 @@ void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, img return; } + TFT_eSprite spr = TFT_eSprite(&tft); tft.setTextWrap(false, false); StaticJsonDocument<512> loc;