Skip to content

Commit 80d84a5

Browse files
committed
weather: Add new app with forecast
1 parent 0e57f36 commit 80d84a5

9 files changed

+245
-5
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ list(APPEND SOURCE_FILES
379379
displayapp/screens/Navigation.cpp
380380
displayapp/screens/Metronome.cpp
381381
displayapp/screens/Motion.cpp
382+
displayapp/screens/Weather.cpp
382383
displayapp/screens/FirmwareValidation.cpp
383384
displayapp/screens/ApplicationList.cpp
384385
displayapp/screens/Notifications.cpp

src/displayapp/DisplayApp.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "displayapp/screens/BatteryInfo.h"
2828
#include "displayapp/screens/Steps.h"
2929
#include "displayapp/screens/Dice.h"
30+
#include "displayapp/screens/Weather.h"
3031
#include "displayapp/screens/PassKey.h"
3132
#include "displayapp/screens/Error.h"
3233

src/displayapp/apps/Apps.h.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Pinetime {
2828
Motion,
2929
Steps,
3030
Dice,
31+
Weather,
3132
PassKey,
3233
QuickSettings,
3334
Settings,
@@ -41,8 +42,7 @@ namespace Pinetime {
4142
SettingChimes,
4243
SettingShakeThreshold,
4344
SettingBluetooth,
44-
Error,
45-
Weather
45+
Error
4646
};
4747

4848
enum class WatchFace : uint8_t {

src/displayapp/apps/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else ()
1313
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Dice")
1414
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Metronome")
1515
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Navigation")
16-
#set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Weather")
16+
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Weather")
1717
#set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Motion")
1818
set(USERAPP_TYPES "${DEFAULT_USER_APP_TYPES}" CACHE STRING "List of user apps to build into the firmware")
1919
endif ()

src/displayapp/fonts/fonts.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"sources": [
1919
{
2020
"file": "JetBrainsMono-Regular.ttf",
21-
"range": "0x25, 0x2b, 0x2d, 0x2e, 0x30-0x3a, 0x4b-0x4d, 0x66, 0x69, 0x6b, 0x6d, 0x74"
21+
"range": "0x25, 0x2b, 0x2d, 0x2e, 0x30-0x3a, 0x43, 0x46, 0x4b-0x4d, 0x66, 0x69, 0x6b, 0x6d, 0x74, 0xb0"
2222
}
2323
],
2424
"bpp": 1,
@@ -28,7 +28,7 @@
2828
"sources": [
2929
{
3030
"file": "JetBrainsMono-Light.ttf",
31-
"range": "0x25, 0x2D, 0x2F, 0x30-0x3a"
31+
"range": "0x25, 0x2D, 0x2F, 0x30-0x3a, 0x43, 0x46, 0xb0"
3232
}
3333
],
3434
"bpp": 1,

src/displayapp/screens/Weather.cpp

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include "displayapp/screens/Weather.h"
2+
#include <lvgl/lvgl.h>
3+
#include "components/ble/SimpleWeatherService.h"
4+
#include "components/datetime/DateTimeController.h"
5+
#include "components/settings/Settings.h"
6+
#include "displayapp/DisplayApp.h"
7+
#include "displayapp/screens/WeatherSymbols.h"
8+
#include "displayapp/InfiniTimeTheme.h"
9+
10+
using namespace Pinetime::Applications::Screens;
11+
12+
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
13+
: settingsController {settingsController}, weatherService {weatherService} {
14+
15+
temperature = lv_label_create(lv_scr_act(), nullptr);
16+
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
17+
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
18+
lv_label_set_text(temperature, "---");
19+
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -30);
20+
lv_obj_set_auto_realign(temperature, true);
21+
22+
minTemperature = lv_label_create(lv_scr_act(), nullptr);
23+
lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
24+
lv_label_set_text(minTemperature, "");
25+
lv_obj_align(minTemperature, temperature, LV_ALIGN_OUT_LEFT_MID, -10, 0);
26+
lv_obj_set_auto_realign(minTemperature, true);
27+
28+
maxTemperature = lv_label_create(lv_scr_act(), nullptr);
29+
lv_obj_set_style_local_text_color(maxTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bg);
30+
lv_label_set_text(maxTemperature, "");
31+
lv_obj_align(maxTemperature, temperature, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
32+
lv_obj_set_auto_realign(maxTemperature, true);
33+
34+
condition = lv_label_create(lv_scr_act(), nullptr);
35+
lv_obj_set_style_local_text_color(condition, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
36+
lv_label_set_text(condition, "");
37+
lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, -10);
38+
lv_obj_set_auto_realign(condition, true);
39+
40+
icon = lv_label_create(lv_scr_act(), nullptr);
41+
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
42+
lv_obj_set_style_local_text_font(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
43+
lv_label_set_text(icon, "");
44+
lv_obj_align(icon, condition, LV_ALIGN_OUT_TOP_MID, 0, 0);
45+
lv_obj_set_auto_realign(icon, true);
46+
47+
forecast = lv_table_create(lv_scr_act(), nullptr);
48+
lv_table_set_col_cnt(forecast, Controllers::SimpleWeatherService::MaxNbForecastDays);
49+
lv_table_set_row_cnt(forecast, 4);
50+
// LV_TABLE_PART_CELL1: Default table style
51+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
52+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, Colors::lightGray);
53+
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 6);
54+
// LV_TABLE_PART_CELL2: Condition icon
55+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
56+
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
57+
lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons);
58+
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, 6);
59+
60+
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
61+
62+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
63+
lv_table_set_col_width(forecast, i, 48);
64+
lv_table_set_cell_type(forecast, 1, i, LV_TABLE_PART_CELL2);
65+
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_RIGHT);
66+
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_RIGHT);
67+
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_RIGHT);
68+
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_RIGHT);
69+
}
70+
71+
taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
72+
Refresh();
73+
}
74+
75+
Weather::~Weather() {
76+
lv_task_del(taskRefresh);
77+
lv_obj_clean(lv_scr_act());
78+
}
79+
80+
void Weather::Refresh() {
81+
currentWeather = weatherService.Current();
82+
if (currentWeather.IsUpdated()) {
83+
auto optCurrentWeather = currentWeather.Get();
84+
if (optCurrentWeather) {
85+
int16_t temp = optCurrentWeather->temperature;
86+
int16_t minTemp = optCurrentWeather->minTemperature;
87+
int16_t maxTemp = optCurrentWeather->maxTemperature;
88+
lv_color_t color = Colors::orange;
89+
if (temp <= 0) { // freezing
90+
color = Colors::blue;
91+
} else if (temp <= 400) { // ice danger
92+
color = LV_COLOR_CYAN;
93+
} else if (temp >= 2700) { // hot
94+
color = Colors::deepOrange;
95+
}
96+
char tempUnit = 'C';
97+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
98+
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
99+
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
100+
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
101+
tempUnit = 'F';
102+
}
103+
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
104+
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
105+
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
106+
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
107+
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId));
108+
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
109+
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color);
110+
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
111+
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
112+
} else {
113+
lv_label_set_text(icon, "");
114+
lv_label_set_text(condition, "");
115+
lv_label_set_text(temperature, "---");
116+
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
117+
lv_label_set_text(minTemperature, "");
118+
lv_label_set_text(maxTemperature, "");
119+
}
120+
}
121+
122+
currentForecast = weatherService.GetForecast();
123+
if (currentForecast.IsUpdated()) {
124+
auto optCurrentForecast = currentForecast.Get();
125+
if (optCurrentForecast) {
126+
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
127+
128+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
129+
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
130+
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
131+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
132+
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
133+
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
134+
}
135+
uint8_t wday = localTime.tm_wday + i + 1;
136+
if (wday > 6) {
137+
wday -= 7;
138+
}
139+
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
140+
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
141+
const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday));
142+
lv_table_set_cell_value(forecast, 0, i, dayOfWeek);
143+
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
144+
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
145+
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
146+
}
147+
} else {
148+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
149+
lv_table_set_cell_value(forecast, 0, i, "");
150+
lv_table_set_cell_value(forecast, 1, i, "");
151+
lv_table_set_cell_value(forecast, 2, i, "");
152+
lv_table_set_cell_value(forecast, 3, i, "");
153+
}
154+
}
155+
}
156+
}

src/displayapp/screens/Weather.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <lvgl/lvgl.h>
5+
#include "displayapp/screens/Screen.h"
6+
#include "components/ble/SimpleWeatherService.h"
7+
#include "displayapp/apps/Apps.h"
8+
#include "displayapp/Controllers.h"
9+
#include "Symbols.h"
10+
#include "utility/DirtyValue.h"
11+
12+
namespace Pinetime {
13+
14+
namespace Controllers {
15+
class Settings;
16+
}
17+
18+
namespace Applications {
19+
namespace Screens {
20+
21+
class Weather : public Screen {
22+
public:
23+
Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService);
24+
~Weather() override;
25+
26+
void Refresh() override;
27+
28+
private:
29+
Controllers::Settings& settingsController;
30+
Controllers::SimpleWeatherService& weatherService;
31+
32+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::CurrentWeather>> currentWeather {};
33+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::Forecast>> currentForecast {};
34+
35+
lv_obj_t* icon;
36+
lv_obj_t* condition;
37+
lv_obj_t* temperature;
38+
lv_obj_t* minTemperature;
39+
lv_obj_t* maxTemperature;
40+
lv_obj_t* forecast;
41+
42+
lv_task_t* taskRefresh;
43+
};
44+
}
45+
46+
template <>
47+
struct AppTraits<Apps::Weather> {
48+
static constexpr Apps app = Apps::Weather;
49+
static constexpr const char* icon = Screens::Symbols::cloudSunRain;
50+
51+
static Screens::Screen* Create(AppControllers& controllers) {
52+
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
53+
};
54+
};
55+
}
56+
}

src/displayapp/screens/WeatherSymbols.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,28 @@ const char* Pinetime::Applications::Screens::Symbols::GetSymbol(const Pinetime::
3434
break;
3535
}
3636
}
37+
38+
const char* Pinetime::Applications::Screens::Symbols::GetCondition(const Pinetime::Controllers::SimpleWeatherService::Icons icon) {
39+
switch (icon) {
40+
case Pinetime::Controllers::SimpleWeatherService::Icons::Sun:
41+
return "Clear sky";
42+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudsSun:
43+
return "Few clouds";
44+
case Pinetime::Controllers::SimpleWeatherService::Icons::Clouds:
45+
return "Scattered clouds";
46+
case Pinetime::Controllers::SimpleWeatherService::Icons::BrokenClouds:
47+
return "Broken clouds";
48+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudShowerHeavy:
49+
return "Shower rain";
50+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudSunRain:
51+
return "Rain";
52+
case Pinetime::Controllers::SimpleWeatherService::Icons::Thunderstorm:
53+
return "Thunderstorm";
54+
case Pinetime::Controllers::SimpleWeatherService::Icons::Snow:
55+
return "Snow";
56+
case Pinetime::Controllers::SimpleWeatherService::Icons::Smog:
57+
return "Mist";
58+
default:
59+
return "";
60+
}
61+
}

src/displayapp/screens/WeatherSymbols.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Pinetime {
77
namespace Screens {
88
namespace Symbols {
99
const char* GetSymbol(const Pinetime::Controllers::SimpleWeatherService::Icons icon);
10+
const char* GetCondition(const Pinetime::Controllers::SimpleWeatherService::Icons icon);
1011
}
1112
}
1213
}

0 commit comments

Comments
 (0)