Skip to content

Commit b9fd12e

Browse files
committed
Switch to simpler temperature interface
1 parent be5a916 commit b9fd12e

8 files changed

+79
-76
lines changed

src/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ list(APPEND SOURCE_FILES
398398
displayapp/screens/Styles.cpp
399399
displayapp/screens/WeatherSymbols.cpp
400400
displayapp/Colors.cpp
401-
displayapp/Weather.cpp
402401
displayapp/widgets/Counter.cpp
403402
displayapp/widgets/PageIndicator.cpp
404403
displayapp/widgets/DotIndicator.cpp
@@ -607,7 +606,6 @@ set(INCLUDE_FILES
607606
displayapp/screens/ApplicationList.h
608607
displayapp/screens/CheckboxList.h
609608
displayapp/Apps.h
610-
displayapp/Weather.h
611609
displayapp/screens/Notifications.h
612610
displayapp/screens/HeartRate.h
613611
displayapp/screens/Metronome.h

src/components/ble/SimpleWeatherService.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ namespace {
4242
std::memcpy(cityName.data(), &dataBuffer[16], 32);
4343
cityName[32] = '\0';
4444
return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]),
45-
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[10])},
46-
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[12])},
47-
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[14])},
45+
SimpleWeatherService::Temperature(ToInt16(&dataBuffer[10])),
46+
SimpleWeatherService::Temperature(ToInt16(&dataBuffer[12])),
47+
SimpleWeatherService::Temperature(ToInt16(&dataBuffer[14])),
4848
SimpleWeatherService::Icons {dataBuffer[16 + 32]},
4949
std::move(cityName));
5050
}
@@ -56,8 +56,8 @@ namespace {
5656
const uint8_t nbDaysInBuffer = dataBuffer[10];
5757
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
5858
for (int i = 0; i < nbDays; i++) {
59-
days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature {ToInt16(&dataBuffer[11 + (i * 5)])},
60-
SimpleWeatherService::Temperature {ToInt16(&dataBuffer[13 + (i * 5)])},
59+
days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature(ToInt16(&dataBuffer[11 + (i * 5)])),
60+
SimpleWeatherService::Temperature(ToInt16(&dataBuffer[13 + (i * 5)])),
6161
SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}};
6262
}
6363
return SimpleWeatherService::Forecast {timestamp, nbDays, days};
@@ -98,9 +98,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
9898
currentWeather = CreateCurrentWeather(dataBuffer);
9999
NRF_LOG_INFO("Current weather :\n\tTimestamp : %d\n\tTemperature:%d\n\tMin:%d\n\tMax:%d\n\tIcon:%d\n\tLocation:%s",
100100
currentWeather->timestamp,
101-
currentWeather->temperature,
102-
currentWeather->minTemperature,
103-
currentWeather->maxTemperature,
101+
currentWeather->temperature.PreciseCelsius(),
102+
currentWeather->minTemperature.PreciseCelsius(),
103+
currentWeather->maxTemperature.PreciseCelsius(),
104104
currentWeather->iconId,
105105
currentWeather->location.data());
106106
}
@@ -112,8 +112,8 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
112112
for (int i = 0; i < 5; i++) {
113113
NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d",
114114
i,
115-
forecast->days[i].minTemperature,
116-
forecast->days[i].maxTemperature,
115+
forecast->days[i].minTemperature.PreciseCelsius(),
116+
forecast->days[i].maxTemperature.PreciseCelsius(),
117117
forecast->days[i].iconId);
118118
}
119119
}
@@ -160,8 +160,8 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService
160160
}
161161

162162
bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const {
163-
return this->iconId == other.iconId && this->maxTemperature.temp == other.maxTemperature.temp &&
164-
this->minTemperature.temp == other.maxTemperature.temp;
163+
return this->iconId == other.iconId && this->maxTemperature.raw == other.maxTemperature.raw &&
164+
this->minTemperature.raw == other.maxTemperature.raw;
165165
}
166166

167167
bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const {

src/components/ble/SimpleWeatherService.h

+28-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,34 @@ namespace Pinetime {
6161
Unknown = 255
6262
};
6363

64-
struct Temperature {
65-
int16_t temp;
64+
class Temperature {
65+
public:
66+
explicit Temperature(int16_t raw) {
67+
this->raw = raw;
68+
}
69+
70+
[[nodiscard]] int16_t PreciseCelsius() const {
71+
return raw;
72+
}
73+
74+
[[nodiscard]] int16_t PreciseFahrenheit() const {
75+
return raw * 9 / 5 + 3200;
76+
}
77+
78+
[[nodiscard]] int16_t Celsius() const {
79+
return (PreciseCelsius() + 50) / 100;
80+
}
81+
82+
[[nodiscard]] int16_t Fahrenheit() const {
83+
return (PreciseFahrenheit() + 50) / 100;
84+
}
85+
86+
bool operator==(const Temperature& other) const {
87+
return raw == other.raw;
88+
}
89+
90+
private:
91+
int16_t raw;
6692
};
6793

6894
using Location = std::array<char, 33>; // 32 char + \0 (end of string)

src/displayapp/Weather.cpp

-13
This file was deleted.

src/displayapp/Weather.h

-17
This file was deleted.

src/displayapp/screens/WatchFaceDigital.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <lvgl/lvgl.h>
44
#include <cstdio>
55

6-
#include "displayapp/Weather.h"
76
#include "displayapp/screens/NotificationIcon.h"
87
#include "displayapp/screens/Symbols.h"
98
#include "displayapp/screens/WeatherSymbols.h"
@@ -176,12 +175,13 @@ void WatchFaceDigital::Refresh() {
176175
if (currentWeather.IsUpdated()) {
177176
auto optCurrentWeather = currentWeather.Get();
178177
if (optCurrentWeather) {
178+
int16_t temp = optCurrentWeather->temperature.Celsius();
179179
char tempUnit = 'C';
180180
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
181+
temp = optCurrentWeather->temperature.Fahrenheit();
181182
tempUnit = 'F';
182183
}
183-
Applications::Temperature temp = Applications::Convert(optCurrentWeather->temperature, settingsController.GetWeatherFormat());
184-
lv_label_set_text_fmt(temperature, "%d°%c", temp.temp, tempUnit);
184+
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
185185
lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId));
186186
} else {
187187
lv_label_set_text_static(temperature, "");

src/displayapp/screens/WatchFacePineTimeStyle.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <lvgl/lvgl.h>
2424
#include <cstdio>
2525
#include "displayapp/Colors.h"
26-
#include "displayapp/Weather.h"
2726
#include "displayapp/screens/BatteryIcon.h"
2827
#include "displayapp/screens/BleIcon.h"
2928
#include "displayapp/screens/NotificationIcon.h"
@@ -544,7 +543,10 @@ void WatchFacePineTimeStyle::Refresh() {
544543
if (currentWeather.IsUpdated()) {
545544
auto optCurrentWeather = currentWeather.Get();
546545
if (optCurrentWeather) {
547-
Applications::Temperature temp = Applications::Convert(optCurrentWeather->temperature, settingsController.GetWeatherFormat());
546+
int16_t temp = optCurrentWeather->temperature.Celsius();
547+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
548+
temp = optCurrentWeather->temperature.Fahrenheit();
549+
}
548550
lv_label_set_text_fmt(temperature, "%d°", temp);
549551
lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId));
550552
} else {

src/displayapp/screens/Weather.cpp

+32-25
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55
#include "components/ble/SimpleWeatherService.h"
66
#include "components/datetime/DateTimeController.h"
77
#include "components/settings/Settings.h"
8-
#include "displayapp/Weather.h"
98
#include "displayapp/DisplayApp.h"
109
#include "displayapp/screens/WeatherSymbols.h"
1110
#include "displayapp/InfiniTimeTheme.h"
1211

1312
using namespace Pinetime::Applications::Screens;
1413

1514
namespace {
16-
lv_color_t TemperatureColor(Pinetime::Applications::Temperature temp) {
17-
if (temp.temp <= 0) { // freezing
15+
lv_color_t TemperatureColor(Pinetime::Controllers::SimpleWeatherService::Temperature temp) {
16+
if (temp.Celsius() <= 0) { // freezing
1817
return Colors::blue;
19-
} else if (temp.temp <= 4) { // ice
18+
} else if (temp.Celsius() <= 4) { // ice
2019
return LV_COLOR_CYAN;
21-
} else if (temp.temp >= 27) { // hot
20+
} else if (temp.Celsius() >= 27) { // hot
2221
return Colors::deepOrange;
2322
}
2423
return Colors::orange; // normal
2524
}
2625

27-
uint8_t TemperatureStyle(Pinetime::Applications::Temperature temp) {
28-
if (temp.temp <= 0) { // freezing
26+
uint8_t TemperatureStyle(Pinetime::Controllers::SimpleWeatherService::Temperature temp) {
27+
if (temp.Celsius() <= 0) { // freezing
2928
return LV_TABLE_PART_CELL3;
30-
} else if (temp.temp <= 4) { // ice
29+
} else if (temp.Celsius() <= 4) { // ice
3130
return LV_TABLE_PART_CELL4;
32-
} else if (temp.temp >= 27) { // hot
31+
} else if (temp.Celsius() >= 27) { // hot
3332
return LV_TABLE_PART_CELL6;
3433
}
3534
return LV_TABLE_PART_CELL5; // normal
@@ -119,19 +118,25 @@ void Weather::Refresh() {
119118
if (currentWeather.IsUpdated()) {
120119
auto optCurrentWeather = currentWeather.Get();
121120
if (optCurrentWeather) {
122-
Applications::Temperature temp = Applications::Convert(optCurrentWeather->temperature, settingsController.GetWeatherFormat());
123-
Applications::Temperature minTemp = Applications::Convert(optCurrentWeather->minTemperature, settingsController.GetWeatherFormat());
124-
Applications::Temperature maxTemp = Applications::Convert(optCurrentWeather->maxTemperature, settingsController.GetWeatherFormat());
125-
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, TemperatureColor(temp));
121+
int16_t temp = optCurrentWeather->temperature.Celsius();
122+
int16_t minTemp = optCurrentWeather->minTemperature.Celsius();
123+
int16_t maxTemp = optCurrentWeather->maxTemperature.Celsius();
126124
char tempUnit = 'C';
127125
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
126+
temp = optCurrentWeather->temperature.Fahrenheit();
127+
minTemp = optCurrentWeather->minTemperature.Fahrenheit();
128+
maxTemp = optCurrentWeather->maxTemperature.Fahrenheit();
128129
tempUnit = 'F';
129130
}
131+
lv_obj_set_style_local_text_color(temperature,
132+
LV_LABEL_PART_MAIN,
133+
LV_STATE_DEFAULT,
134+
TemperatureColor(optCurrentWeather->temperature.Celsius()));
130135
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
131136
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId));
132-
lv_label_set_text_fmt(temperature, "%d°%c", temp.temp, tempUnit);
133-
lv_label_set_text_fmt(minTemperature, "%d°", minTemp.temp);
134-
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp.temp);
137+
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
138+
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
139+
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
135140
} else {
136141
lv_label_set_text(icon, "");
137142
lv_label_set_text(condition, "");
@@ -149,12 +154,14 @@ void Weather::Refresh() {
149154
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
150155

151156
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
152-
Applications::Temperature maxTemp =
153-
Applications::Convert(optCurrentForecast->days[i].maxTemperature, settingsController.GetWeatherFormat());
154-
Applications::Temperature minTemp =
155-
Applications::Convert(optCurrentForecast->days[i].minTemperature, settingsController.GetWeatherFormat());
156-
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(maxTemp));
157-
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(minTemp));
157+
int16_t minTemp = optCurrentForecast->days[i].maxTemperature.Celsius();
158+
int16_t maxTemp = optCurrentForecast->days[i].minTemperature.Celsius();
159+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
160+
minTemp = optCurrentForecast->days[i].maxTemperature.Fahrenheit();
161+
maxTemp = optCurrentForecast->days[i].minTemperature.Fahrenheit();
162+
}
163+
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i].maxTemperature));
164+
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i].minTemperature));
158165
uint8_t wday = localTime.tm_wday + i + 1;
159166
if (wday > 7) {
160167
wday -= 7;
@@ -165,16 +172,16 @@ void Weather::Refresh() {
165172
// Pad cells based on the largest number of digits on each column
166173
char maxPadding[3] = " ";
167174
char minPadding[3] = " ";
168-
int diff = snprintf(nullptr, 0, "%d", maxTemp.temp) - snprintf(nullptr, 0, "%d", minTemp.temp);
175+
int diff = snprintf(nullptr, 0, "%d", maxTemp) - snprintf(nullptr, 0, "%d", minTemp);
169176
if (diff <= 0) {
170177
maxPadding[-diff] = '\0';
171178
minPadding[0] = '\0';
172179
} else {
173180
maxPadding[0] = '\0';
174181
minPadding[diff] = '\0';
175182
}
176-
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding, maxTemp.temp);
177-
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding, minTemp.temp);
183+
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding, maxTemp);
184+
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding, minTemp);
178185
}
179186
} else {
180187
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {

0 commit comments

Comments
 (0)