Skip to content

Commit d9e5172

Browse files
committed
weather: Colorize forecast temperatures
1 parent fe1f581 commit d9e5172

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

src/displayapp/screens/Weather.cpp

+47-19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@
88

99
using namespace Pinetime::Applications::Screens;
1010

11+
namespace {
12+
lv_color_t temperatureColor(int16_t temperature) {
13+
if (temperature <= 0) { // freezing
14+
return Colors::blue;
15+
} else if (temperature <= 400) { // ice
16+
return LV_COLOR_CYAN;
17+
} else if (temperature >= 2700) { // hot
18+
return Colors::deepOrange;
19+
}
20+
return Colors::orange; // normal
21+
}
22+
23+
uint8_t temperatureStyle(int16_t temperature) {
24+
if (temperature <= 0) { // freezing
25+
return LV_TABLE_PART_CELL3;
26+
} else if (temperature <= 400) { // ice
27+
return LV_TABLE_PART_CELL4;
28+
} else if (temperature >= 2700) { // hot
29+
return LV_TABLE_PART_CELL6;
30+
}
31+
return LV_TABLE_PART_CELL5; // normal
32+
}
33+
}
34+
1135
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
1236
: settingsController {settingsController}, weatherService {weatherService} {
1337

@@ -54,6 +78,19 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
5478
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
5579
lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons);
5680

81+
// LV_TABLE_PART_CELL3: Freezing
82+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_BLACK);
83+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, Colors::blue);
84+
// LV_TABLE_PART_CELL4: Ice
85+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_BLACK);
86+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_CYAN);
87+
// LV_TABLE_PART_CELL5: Normal
88+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, LV_COLOR_BLACK);
89+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, Colors::orange);
90+
// LV_TABLE_PART_CELL6: Hot
91+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, LV_COLOR_BLACK);
92+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, Colors::deepOrange);
93+
5794
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
5895

5996
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
@@ -81,30 +118,19 @@ void Weather::Refresh() {
81118
int16_t temp = optCurrentWeather->temperature;
82119
int16_t minTemp = optCurrentWeather->minTemperature;
83120
int16_t maxTemp = optCurrentWeather->maxTemperature;
84-
lv_color_t color = Colors::orange;
85-
if (temp <= 0) { // freezing
86-
color = Colors::blue;
87-
} else if (temp <= 400) { // ice danger
88-
color = LV_COLOR_CYAN;
89-
} else if (temp >= 2700) { // hot
90-
color = Colors::deepOrange;
91-
}
121+
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, temperatureColor(temp));
92122
char tempUnit = 'C';
93123
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
94124
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
95125
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
96126
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
97127
tempUnit = 'F';
98128
}
99-
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
100-
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
101-
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
102129
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
103130
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId));
104-
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
105-
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color);
106-
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
107-
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
131+
lv_label_set_text_fmt(temperature, "%d°%c", temp / 100, tempUnit);
132+
lv_label_set_text_fmt(minTemperature, "%d°", minTemp / 100);
133+
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp / 100);
108134
} else {
109135
lv_label_set_text(icon, "");
110136
lv_label_set_text(condition, "");
@@ -124,27 +150,29 @@ void Weather::Refresh() {
124150
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
125151
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
126152
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
153+
lv_table_set_cell_type(forecast, 2, i, temperatureStyle(maxTemp));
154+
lv_table_set_cell_type(forecast, 3, i, temperatureStyle(minTemp));
127155
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
128156
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
129157
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
130158
}
131-
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
132-
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
133159
uint8_t wday = localTime.tm_wday + i + 1;
134160
if (wday > 6) {
135161
wday -= 7;
136162
}
137163
lv_table_set_cell_value(forecast, 0, i, WeekDays[wday]);
138164
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
139-
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
140-
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
165+
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp / 100);
166+
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp / 100);
141167
}
142168
} else {
143169
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
144170
lv_table_set_cell_value(forecast, 0, i, "");
145171
lv_table_set_cell_value(forecast, 1, i, "");
146172
lv_table_set_cell_value(forecast, 2, i, "");
147173
lv_table_set_cell_value(forecast, 3, i, "");
174+
lv_table_set_cell_type(forecast, 2, i, LV_TABLE_PART_CELL1);
175+
lv_table_set_cell_type(forecast, 3, i, LV_TABLE_PART_CELL1);
148176
}
149177
}
150178
}

src/libs/lv_conf.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,9 @@ typedef void* lv_obj_user_data_t;
729729
#define LV_USE_TABLE 1
730730
#if LV_USE_TABLE
731731
#define LV_TABLE_COL_MAX 12
732-
#define LV_TABLE_CELL_STYLE_CNT 5
732+
#define LV_TABLE_CELL_STYLE_CNT 6
733+
#define LV_TABLE_PART_CELL5 5
734+
#define LV_TABLE_PART_CELL6 6
733735
#endif
734736

735737

0 commit comments

Comments
 (0)