InfiniTimeOrg / InfiniTime

Firmware for Pinetime smartwatch written in C++ and based on FreeRTOS
GNU General Public License v3.0
2.63k stars 902 forks source link

weather: Add function for converting to units #2015

Open FintasticMan opened 4 months ago

FintasticMan commented 4 months ago

Handles rounding correctly.

github-actions[bot] commented 4 months ago
Build size and comparison to main: Section Size Difference
text 373224B -16B
data 940B 0B
bss 63516B 0B
minacode commented 4 months ago

You could use structs of one int to make the types really safe.

JF002 commented 4 months ago

I'm ok with the current state. I however thought about another option.

SimpleWeatherService would define a Temperature type : PineTime::Controllers::SimpleWeatherService::Temperature. It's the internal representation of a temperature in the SompleWeatherService expressed in "°C * 100"

struct Temperature {
  int16_t temperature;
}

Somewhere under DisplayApp, we would define another Temperature type : Pinetime::Applications::Temperature. It represent the temperature for the UI side of InfiniTime as an std::variant (TODO : check that the overhead of std::variant is negligible)

struct TemperatureCelsius {
  int16_t temperature;
}

struct TemperatureFahrenheit {
  int16_t temperature;
}

using Temperature = std::variant<TemperatureCelsius, TemperatureFahrenheit temperature

Then, also under DisplayApp, we define a function that converts from SimpleWeatherService Temperature to "display" temperature :

Pinetime::Applications::Temperature Convert(PineTime::Controllers::SimpleWeatherService::Temperature, Controllers::Settings::WeatherFormat format);

This way, all the "temperature" types are strongly typed and it's impossible to implicitly convert from one to another. Also SimpleWeatherService does not have any code related to the UI anymore.