Skip to content

Commit

Permalink
weather: Add forecast days of the week
Browse files Browse the repository at this point in the history
  • Loading branch information
vkareh committed Feb 1, 2024
1 parent 835561a commit 4178fd0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_label_set_text(temperature, "---");
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -10);
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
lv_obj_set_auto_realign(temperature, true);

minTemperature = lv_label_create(lv_scr_act(), nullptr);
Expand All @@ -33,7 +33,7 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
condition = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(condition, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
lv_label_set_text(condition, "");
lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, 0);
lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, -10);
lv_obj_set_auto_realign(condition, true);

icon = lv_label_create(lv_scr_act(), nullptr);
Expand All @@ -43,9 +43,15 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
lv_obj_align(icon, condition, LV_ALIGN_OUT_TOP_MID, 0, 0);
lv_obj_set_auto_realign(icon, true);

// timestamp = lv_label_create(lv_scr_act(), nullptr);
// lv_obj_set_style_local_text_color(timestamp, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
// lv_label_set_text(timestamp, "");
// lv_obj_align(timestamp, temperature, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
// lv_obj_set_auto_realign(timestamp, true);

forecast = lv_table_create(lv_scr_act(), nullptr);
lv_table_set_col_cnt(forecast, Controllers::SimpleWeatherService::MaxNbForecastDays);
lv_table_set_row_cnt(forecast, 2);
lv_table_set_row_cnt(forecast, 4);
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);

Expand All @@ -54,14 +60,15 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
lv_table_set_row_cnt(forecastSym, 1);
lv_obj_set_style_local_border_color(forecastSym, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_text_font(forecastSym, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
lv_obj_align(forecastSym, forecast, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
lv_obj_align(forecastSym, forecast, LV_ALIGN_IN_TOP_LEFT, 0, 24);

for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
lv_table_set_col_width(forecastSym, i, 48);
lv_table_set_cell_align(forecastSym, 0, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_col_width(forecast, i, 48);
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_CENTER);
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_CENTER);
}

taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
Expand All @@ -80,6 +87,7 @@ void Weather::Refresh() {
int16_t temp = optCurrentWeather->temperature;
int16_t minTemp = optCurrentWeather->minTemperature;
int16_t maxTemp = optCurrentWeather->maxTemperature;
// std::tm localTime = *std::localtime((const time_t *)&optCurrentWeather->timestamp);
lv_color_t color = Colors::orange;
if (temp <= 0) {
color = Colors::blue;
Expand All @@ -102,20 +110,24 @@ void Weather::Refresh() {
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(timestamp, " %d:%2d", WeekDays[static_cast<uint8_t>(localTime.tm_wday)], localTime.tm_hour, localTime.tm_min);
} else {
lv_label_set_text(icon, "");
lv_label_set_text(condition, "");
lv_label_set_text(temperature, "---");
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_label_set_text(minTemperature, "");
lv_label_set_text(maxTemperature, "");
// lv_label_set_text(timestamp, "");
}
}

currentForecast = weatherService.GetForecast();
if (currentForecast.IsUpdated()) {
auto optCurrentForecast = currentForecast.Get();
if (optCurrentForecast) {
std::tm localTime = *std::localtime((const time_t *)&optCurrentForecast->timestamp);

for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
Expand All @@ -125,15 +137,21 @@ void Weather::Refresh() {
}
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
uint8_t wday = localTime.tm_wday + i + 1;
if (wday > 6) {
wday -= 7;
}
lv_table_set_cell_value(forecastSym, 0, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
lv_table_set_cell_value_fmt(forecast, 0, i, "%d", minTemp);
lv_table_set_cell_value_fmt(forecast, 1, i, "%d", maxTemp);
lv_table_set_cell_value_fmt(forecast, 0, i, "%s", WeekDays[wday]);
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", minTemp);
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", maxTemp);
}
} else {
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
lv_table_set_cell_value(forecastSym, 0, 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, "");
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/displayapp/screens/Weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Pinetime {

class Weather : public Screen {
public:
const std::array<char*, 7> WeekDays = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};

Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService);
~Weather() override;

Expand All @@ -37,6 +39,7 @@ namespace Pinetime {
lv_obj_t* temperature;
lv_obj_t* minTemperature;
lv_obj_t* maxTemperature;
// lv_obj_t* timestamp;
lv_obj_t* forecastSym;
lv_obj_t* forecast;

Expand Down

0 comments on commit 4178fd0

Please sign in to comment.