From f07699a4dd8e93feb96137e07cfbdcde62dbfe44 Mon Sep 17 00:00:00 2001 From: FintasticMan Date: Mon, 12 Feb 2024 12:39:54 +0100 Subject: [PATCH] weather: Add function for converting to display temp Handles rounding correctly. --- src/components/ble/SimpleWeatherService.h | 24 ++++++++++++------- src/displayapp/screens/WatchFaceDigital.cpp | 8 +++---- .../screens/WatchFacePineTimeStyle.cpp | 8 +++---- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 4bbefcfc..ebbb371a 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -62,12 +62,14 @@ namespace Pinetime { }; using Location = std::array; // 32 char + \0 (end of string) + using PreciseTemp = int16_t; + using DisplayTemp = int16_t; struct CurrentWeather { CurrentWeather(uint64_t timestamp, - int16_t temperature, - int16_t minTemperature, - int16_t maxTemperature, + PreciseTemp temperature, + PreciseTemp minTemperature, + PreciseTemp maxTemperature, Icons iconId, Location&& location) : timestamp {timestamp}, @@ -79,9 +81,9 @@ namespace Pinetime { } uint64_t timestamp; - int16_t temperature; - int16_t minTemperature; - int16_t maxTemperature; + PreciseTemp temperature; + PreciseTemp minTemperature; + PreciseTemp maxTemperature; Icons iconId; Location location; @@ -93,8 +95,8 @@ namespace Pinetime { uint8_t nbDays; struct Day { - int16_t minTemperature; - int16_t maxTemperature; + PreciseTemp minTemperature; + PreciseTemp maxTemperature; Icons iconId; bool operator==(const Day& other) const; @@ -108,10 +110,14 @@ namespace Pinetime { std::optional Current() const; std::optional GetForecast() const; - static int16_t CelsiusToFahrenheit(int16_t celsius) { + static PreciseTemp CelsiusToFahrenheit(PreciseTemp celsius) { return celsius * 9 / 5 + 3200; } + static DisplayTemp Convert(PreciseTemp temp) { + return temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); + } + private: // 00050000-78fc-48fe-8e23-433b3a1942d0 static constexpr ble_uuid128_t BaseUuid() { diff --git a/src/displayapp/screens/WatchFaceDigital.cpp b/src/displayapp/screens/WatchFaceDigital.cpp index afe00fa5..a5fedfe5 100644 --- a/src/displayapp/screens/WatchFaceDigital.cpp +++ b/src/displayapp/screens/WatchFaceDigital.cpp @@ -174,14 +174,14 @@ void WatchFaceDigital::Refresh() { if (currentWeather.IsUpdated()) { auto optCurrentWeather = currentWeather.Get(); if (optCurrentWeather) { - int16_t temp = optCurrentWeather->temperature; + Controllers::SimpleWeatherService::PreciseTemp preciseTemp = optCurrentWeather->temperature; char tempUnit = 'C'; if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { - temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); + preciseTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(preciseTemp); tempUnit = 'F'; } - temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); - lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit); + Controllers::SimpleWeatherService::DisplayTemp displayTemp = Controllers::SimpleWeatherService::Convert(preciseTemp); + lv_label_set_text_fmt(temperature, "%d°%c", displayTemp, tempUnit); lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId)); } else { lv_label_set_text_static(temperature, ""); diff --git a/src/displayapp/screens/WatchFacePineTimeStyle.cpp b/src/displayapp/screens/WatchFacePineTimeStyle.cpp index e56031f7..af5e2fec 100644 --- a/src/displayapp/screens/WatchFacePineTimeStyle.cpp +++ b/src/displayapp/screens/WatchFacePineTimeStyle.cpp @@ -543,12 +543,12 @@ void WatchFacePineTimeStyle::Refresh() { if (currentWeather.IsUpdated()) { auto optCurrentWeather = currentWeather.Get(); if (optCurrentWeather) { - int16_t temp = optCurrentWeather->temperature; + Controllers::SimpleWeatherService::PreciseTemp preciseTemp = optCurrentWeather->temperature; if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { - temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); + preciseTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(preciseTemp); } - temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); - lv_label_set_text_fmt(temperature, "%d°", temp); + Controllers::SimpleWeatherService::DisplayTemp displayTemp = Controllers::SimpleWeatherService::Convert(preciseTemp); + lv_label_set_text_fmt(temperature, "%d°", displayTemp); lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId)); } else { lv_label_set_text(temperature, "--");