Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weather: Add function for converting to units #2015

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

FintasticMan
Copy link
Member

Handles rounding correctly.

Copy link

github-actions bot commented Feb 12, 2024

Build size and comparison to main:

Section Size Difference
text 373224B -16B
data 940B 0B
bss 63516B 0B

@@ -112,6 +112,10 @@ namespace Pinetime {
return celsius * 9 / 5 + 3200;
}

static int16_t TempToUnits(int16_t temp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this name is not quite representative, since it doesn't include units. Maybe something like "RenderTemp" or "DisplayTemp" or "RoundTemp"? Not entirely sure, naming is hard :/

Other than that, I really like what you're doing centralizing this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of the input and output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input is a temperature in units multiplied by 100, and the output is temperature in the actual unit. I wasn't sure of the naming either, it's a surprisingly difficult operation to give a name to.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's basically FixedPoint2ToIntRounded? 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CentiToUnit? As in centicelsius to celsius or centifahrenheit to fahrenheit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method could simply be called Convert() if we used strong types for arguments. Ex:

static DisplayTemperature Convert(const SimpleWeatherService::RawTemperature temperature)

Using stronger types for

  • temperature coming from the weather service (in "centi celsius degrees")
  • temperature in °C
  • temperature in °F

Would probably help with code readability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just done something like that. I decided not to distinguish between the temperature in C or in F. Do you think this makes sense?

@minacode
Copy link
Contributor

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

@JF002
Copy link
Collaborator

JF002 commented Feb 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants