weather: Add function for converting to display temp

Handles rounding correctly.
This commit is contained in:
FintasticMan 2024-02-12 12:39:54 +01:00
parent c2c53bc6ab
commit f07699a4dd
No known key found for this signature in database
GPG Key ID: A00F1AB6DB1ED386
3 changed files with 23 additions and 17 deletions

View File

@ -62,12 +62,14 @@ namespace Pinetime {
};
using Location = std::array<char, 33>; // 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<CurrentWeather> Current() const;
std::optional<Forecast> 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() {

View File

@ -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, "");

View File

@ -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, "--");