Skip to content

Commit

Permalink
weather: Colorize forecast temperatures
Browse files Browse the repository at this point in the history
  • Loading branch information
vkareh committed Feb 2, 2024
1 parent fe1f581 commit d9e5172
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
66 changes: 47 additions & 19 deletions src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@

using namespace Pinetime::Applications::Screens;

namespace {
lv_color_t temperatureColor(int16_t temperature) {
if (temperature <= 0) { // freezing
return Colors::blue;
} else if (temperature <= 400) { // ice
return LV_COLOR_CYAN;
} else if (temperature >= 2700) { // hot
return Colors::deepOrange;
}
return Colors::orange; // normal
}

uint8_t temperatureStyle(int16_t temperature) {
if (temperature <= 0) { // freezing
return LV_TABLE_PART_CELL3;
} else if (temperature <= 400) { // ice
return LV_TABLE_PART_CELL4;
} else if (temperature >= 2700) { // hot
return LV_TABLE_PART_CELL6;
}
return LV_TABLE_PART_CELL5; // normal
}
}

Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
: settingsController {settingsController}, weatherService {weatherService} {

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

// LV_TABLE_PART_CELL3: Freezing
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, Colors::blue);
// LV_TABLE_PART_CELL4: Ice
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_CYAN);
// LV_TABLE_PART_CELL5: Normal
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, Colors::orange);
// LV_TABLE_PART_CELL6: Hot
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, Colors::deepOrange);

lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
Expand Down Expand Up @@ -81,30 +118,19 @@ void Weather::Refresh() {
int16_t temp = optCurrentWeather->temperature;
int16_t minTemp = optCurrentWeather->minTemperature;
int16_t maxTemp = optCurrentWeather->maxTemperature;
lv_color_t color = Colors::orange;
if (temp <= 0) { // freezing
color = Colors::blue;
} else if (temp <= 400) { // ice danger
color = LV_COLOR_CYAN;
} else if (temp >= 2700) { // hot
color = Colors::deepOrange;
}
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, temperatureColor(temp));
char tempUnit = 'C';
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
tempUnit = 'F';
}
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId));
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color);
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
lv_label_set_text_fmt(temperature, "%d°%c", temp / 100, tempUnit);
lv_label_set_text_fmt(minTemperature, "%d°", minTemp / 100);
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp / 100);
} else {
lv_label_set_text(icon, "");
lv_label_set_text(condition, "");
Expand All @@ -124,27 +150,29 @@ void Weather::Refresh() {
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
lv_table_set_cell_type(forecast, 2, i, temperatureStyle(maxTemp));
lv_table_set_cell_type(forecast, 3, i, temperatureStyle(minTemp));
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
}
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
uint8_t wday = localTime.tm_wday + i + 1;
if (wday > 6) {
wday -= 7;
}
lv_table_set_cell_value(forecast, 0, i, WeekDays[wday]);
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp / 100);
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp / 100);
}
} else {
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
lv_table_set_cell_value(forecast, 0, i, "");
lv_table_set_cell_value(forecast, 1, i, "");
lv_table_set_cell_value(forecast, 2, i, "");
lv_table_set_cell_value(forecast, 3, i, "");
lv_table_set_cell_type(forecast, 2, i, LV_TABLE_PART_CELL1);
lv_table_set_cell_type(forecast, 3, i, LV_TABLE_PART_CELL1);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/libs/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,9 @@ typedef void* lv_obj_user_data_t;
#define LV_USE_TABLE 1
#if LV_USE_TABLE
#define LV_TABLE_COL_MAX 12
#define LV_TABLE_CELL_STYLE_CNT 5
#define LV_TABLE_CELL_STYLE_CNT 6
#define LV_TABLE_PART_CELL5 5
#define LV_TABLE_PART_CELL6 6
#endif


Expand Down

0 comments on commit d9e5172

Please sign in to comment.