Skip to content

Commit f6cf49a

Browse files
committed
weather: Add new app with forecast
1 parent 2135e12 commit f6cf49a

11 files changed

+239
-6
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/components/ble/SimpleWeatherService.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,16 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService
158158
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature &&
159159
std::strcmp(this->location.data(), other.location.data()) == 0;
160160
}
161+
162+
bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const {
163+
return this->iconId == other.iconId && this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature;
164+
}
165+
166+
bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const {
167+
for (int i = 0; i < this->nbDays; i++) {
168+
if (this->days[i] != other.days[i]) {
169+
return false;
170+
}
171+
}
172+
return this->timestamp == other.timestamp && this->nbDays == other.nbDays;
173+
}

src/components/ble/SimpleWeatherService.h

+4
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ namespace Pinetime {
9696
int16_t minTemperature;
9797
int16_t maxTemperature;
9898
Icons iconId;
99+
100+
bool operator==(const Day& other) const;
99101
};
100102

101103
std::array<Day, MaxNbForecastDays> days;
104+
105+
bool operator==(const Forecast& other) const;
102106
};
103107

104108
std::optional<CurrentWeather> Current() const;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
{
99
"file": "FontAwesome5-Solid+Brands+Regular.woff",
10-
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf522, 0xf743"
10+
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf522, 0xf743, 0xf6c4"
1111
}
1212
],
1313
"bpp": 1,
@@ -18,7 +18,7 @@
1818
"sources": [
1919
{
2020
"file": "JetBrainsMono-Regular.ttf",
21-
"range": "0x25, 0x2b, 0x2d, 0x30-0x3a, 0x4b-0x4d, 0x66, 0x69, 0x6b, 0x6d, 0x74"
21+
"range": "0x25, 0x2b, 0x2d, 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

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "displayapp/screens/Weather.h"
2+
#include <lvgl/lvgl.h>
3+
#include "components/ble/SimpleWeatherService.h"
4+
#include "components/settings/Settings.h"
5+
#include "displayapp/DisplayApp.h"
6+
#include "displayapp/screens/WeatherSymbols.h"
7+
8+
using namespace Pinetime::Applications::Screens;
9+
10+
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
11+
: settingsController {settingsController}, weatherService {weatherService} {
12+
13+
temperature = lv_label_create(lv_scr_act(), nullptr);
14+
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
15+
lv_obj_set_style_local_text_font(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
16+
lv_label_set_text(temperature, "---");
17+
lv_obj_align(temperature, nullptr, LV_ALIGN_CENTER, 0, -10);
18+
lv_obj_set_auto_realign(temperature, true);
19+
20+
minTemperature = lv_label_create(lv_scr_act(), nullptr);
21+
lv_obj_set_style_local_text_color(minTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
22+
lv_label_set_text(minTemperature, "");
23+
lv_obj_align(minTemperature, temperature, LV_ALIGN_OUT_LEFT_MID, -10, 0);
24+
lv_obj_set_auto_realign(minTemperature, true);
25+
26+
maxTemperature = lv_label_create(lv_scr_act(), nullptr);
27+
lv_obj_set_style_local_text_color(maxTemperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
28+
lv_label_set_text(maxTemperature, "");
29+
lv_obj_align(maxTemperature, temperature, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
30+
lv_obj_set_auto_realign(maxTemperature, true);
31+
32+
condition = lv_label_create(lv_scr_act(), nullptr);
33+
lv_obj_set_style_local_text_color(condition, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_SILVER);
34+
lv_label_set_text(condition, "");
35+
lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, 0);
36+
lv_obj_set_auto_realign(condition, true);
37+
38+
icon = lv_label_create(lv_scr_act(), nullptr);
39+
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
40+
lv_obj_set_style_local_text_font(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
41+
lv_label_set_text(icon, "");
42+
lv_obj_align(icon, condition, LV_ALIGN_OUT_TOP_MID, 0, 0);
43+
lv_obj_set_auto_realign(icon, true);
44+
45+
forecast = lv_table_create(lv_scr_act(), nullptr);
46+
lv_table_set_col_cnt(forecast, Controllers::SimpleWeatherService::MaxNbForecastDays);
47+
lv_table_set_row_cnt(forecast, 2);
48+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
49+
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
50+
51+
forecastSym = lv_table_create(lv_scr_act(), nullptr);
52+
lv_table_set_col_cnt(forecastSym, Controllers::SimpleWeatherService::MaxNbForecastDays);
53+
lv_table_set_row_cnt(forecastSym, 1);
54+
lv_obj_set_style_local_border_color(forecastSym, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
55+
lv_obj_set_style_local_text_font(forecastSym, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
56+
lv_obj_align(forecastSym, forecast, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
57+
58+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
59+
lv_table_set_col_width(forecastSym, i, 48);
60+
lv_table_set_cell_align(forecastSym, 0, i, LV_LABEL_ALIGN_CENTER);
61+
lv_table_set_col_width(forecast, i, 48);
62+
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER);
63+
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER);
64+
}
65+
66+
taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
67+
}
68+
69+
Weather::~Weather() {
70+
lv_task_del(taskRefresh);
71+
lv_obj_clean(lv_scr_act());
72+
}
73+
74+
void Weather::Refresh() {
75+
currentWeather = weatherService.Current();
76+
if (currentWeather.IsUpdated()) {
77+
auto optCurrentWeather = currentWeather.Get();
78+
if (optCurrentWeather) {
79+
int16_t temp = optCurrentWeather->temperature;
80+
int16_t minTemp = optCurrentWeather->minTemperature;
81+
int16_t maxTemp = optCurrentWeather->maxTemperature;
82+
char tempUnit = 'C';
83+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
84+
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
85+
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
86+
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
87+
tempUnit = 'F';
88+
}
89+
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
90+
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
91+
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
92+
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
93+
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId));
94+
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
95+
lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
96+
lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
97+
} else {
98+
lv_label_set_text(icon, "");
99+
lv_label_set_text(condition, "");
100+
lv_label_set_text(temperature, "---");
101+
lv_label_set_text(minTemperature, "");
102+
lv_label_set_text(maxTemperature, "");
103+
}
104+
}
105+
106+
currentForecast = weatherService.GetForecast();
107+
if (currentForecast.IsUpdated()) {
108+
auto optCurrentForecast = currentForecast.Get();
109+
if (optCurrentForecast) {
110+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
111+
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
112+
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
113+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
114+
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
115+
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
116+
}
117+
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
118+
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
119+
lv_table_set_cell_value(forecastSym, 0, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
120+
lv_table_set_cell_value_fmt(forecast, 0, i, "%d", minTemp);
121+
lv_table_set_cell_value_fmt(forecast, 1, i, "%d", maxTemp);
122+
}
123+
} else {
124+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
125+
lv_table_set_cell_value(forecastSym, 0, i, "");
126+
lv_table_set_cell_value(forecast, 0, i, "");
127+
lv_table_set_cell_value(forecast, 1, i, "");
128+
}
129+
}
130+
}
131+
}

src/displayapp/screens/Weather.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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* forecastSym;
41+
lv_obj_t* forecast;
42+
43+
lv_task_t* taskRefresh;
44+
};
45+
}
46+
47+
template <>
48+
struct AppTraits<Apps::Weather> {
49+
static constexpr Apps app = Apps::Weather;
50+
static constexpr const char* icon = Screens::Symbols::cloudSun;
51+
52+
static Screens::Screen* Create(AppControllers& controllers) {
53+
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
54+
};
55+
};
56+
}
57+
}

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)