Skip to content

Commit f09242e

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

File tree

9 files changed

+220
-6
lines changed

9 files changed

+220
-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

+14
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,17 @@ 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 &&
164+
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature;
165+
}
166+
167+
bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const {
168+
for (int i = 0; i < this->nbDays; i++) {
169+
if (this->days[i] != other.days[i]) {
170+
return false;
171+
}
172+
}
173+
return this->timestamp == other.timestamp && this->nbDays == other.nbDays;
174+
}

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

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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/InfiniTimeTheme.h"
7+
#include "displayapp/screens/WeatherSymbols.h"
8+
9+
using namespace Pinetime::Applications::Screens;
10+
11+
Weather::Weather(Controllers::Settings& settingsController,
12+
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, 0);
20+
lv_obj_set_auto_realign(temperature, true);
21+
22+
condition = lv_label_create(lv_scr_act(), nullptr);
23+
lv_obj_set_style_local_text_color(condition, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
24+
lv_label_set_text(condition, "");
25+
lv_obj_align(condition, temperature, LV_ALIGN_OUT_TOP_MID, 0, 0);
26+
lv_obj_set_auto_realign(condition, true);
27+
28+
icon = lv_label_create(lv_scr_act(), nullptr);
29+
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
30+
lv_obj_set_style_local_text_font(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
31+
lv_label_set_text(icon, "");
32+
lv_obj_align(icon, condition, LV_ALIGN_OUT_TOP_MID, 0, 0);
33+
lv_obj_set_auto_realign(icon, true);
34+
35+
forecast = lv_table_create(lv_scr_act(), nullptr);
36+
lv_table_set_col_cnt(forecast, Controllers::SimpleWeatherService::MaxNbForecastDays);
37+
lv_table_set_row_cnt(forecast, 2);
38+
lv_obj_set_style_local_pad_all(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 0);
39+
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
40+
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
41+
42+
forecastSym = lv_table_create(lv_scr_act(), nullptr);
43+
lv_table_set_col_cnt(forecastSym, Controllers::SimpleWeatherService::MaxNbForecastDays);
44+
lv_table_set_row_cnt(forecastSym, 1);
45+
lv_obj_set_style_local_pad_all(forecastSym, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 0);
46+
lv_obj_set_style_local_border_color(forecastSym, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
47+
lv_obj_set_style_local_text_font(forecastSym, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &fontawesome_weathericons);
48+
lv_obj_align(forecastSym, forecast, LV_ALIGN_OUT_TOP_LEFT, 0, 0);
49+
50+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
51+
lv_table_set_col_width(forecastSym, i, 48);
52+
lv_table_set_cell_align(forecastSym, 0, i, LV_LABEL_ALIGN_CENTER);
53+
lv_table_set_col_width(forecast, i, 48);
54+
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER);
55+
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER);
56+
}
57+
58+
taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
59+
}
60+
61+
Weather::~Weather() {
62+
lv_task_del(taskRefresh);
63+
lv_obj_clean(lv_scr_act());
64+
}
65+
66+
void Weather::Refresh() {
67+
currentWeather = weatherService.Current();
68+
if (currentWeather.IsUpdated()) {
69+
auto optCurrentWeather = currentWeather.Get();
70+
if (optCurrentWeather) {
71+
int16_t temp = optCurrentWeather->temperature;
72+
char tempUnit = 'C';
73+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
74+
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp);
75+
tempUnit = 'F';
76+
}
77+
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
78+
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit);
79+
lv_label_set_text(condition, getCondition(optCurrentWeather->iconId));
80+
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
81+
} else {
82+
lv_label_set_text(temperature, "---");
83+
lv_label_set_text(condition, "");
84+
lv_label_set_text(icon, "");
85+
}
86+
}
87+
88+
currentForecast = weatherService.GetForecast();
89+
if (currentForecast.IsUpdated()) {
90+
auto optCurrentForecast = currentForecast.Get();
91+
if (optCurrentForecast) {
92+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
93+
int16_t minTemp = optCurrentForecast->days[i].minTemperature;
94+
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature;
95+
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
96+
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp);
97+
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp);
98+
}
99+
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
100+
maxTemp = maxTemp / 100 + (maxTemp % 100 >= 50 ? 1 : 0);
101+
lv_table_set_cell_value(forecastSym, 0, i, Symbols::GetSymbol(optCurrentForecast->days[0].iconId));
102+
lv_table_set_cell_value_fmt(forecast, 0, i, "%d", minTemp);
103+
lv_table_set_cell_value_fmt(forecast, 1, i, "%d", maxTemp);
104+
}
105+
} else {
106+
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
107+
lv_table_set_cell_value(forecastSym, 0, i, "");
108+
lv_table_set_cell_value(forecast, 0, i, "");
109+
lv_table_set_cell_value(forecast, 1, i, "");
110+
}
111+
}
112+
}
113+
}
114+
115+
const char* Weather::getCondition(const Pinetime::Controllers::SimpleWeatherService::Icons icon) {
116+
switch (icon) {
117+
case Pinetime::Controllers::SimpleWeatherService::Icons::Sun:
118+
return "Clear sky";
119+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudsSun:
120+
return "Few clouds";
121+
case Pinetime::Controllers::SimpleWeatherService::Icons::Clouds:
122+
return "Scattered clouds";
123+
case Pinetime::Controllers::SimpleWeatherService::Icons::BrokenClouds:
124+
return "Broken clouds";
125+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudShowerHeavy:
126+
return "Shower rain";
127+
case Pinetime::Controllers::SimpleWeatherService::Icons::CloudSunRain:
128+
return "Rain";
129+
case Pinetime::Controllers::SimpleWeatherService::Icons::Thunderstorm:
130+
return "Thunderstorm";
131+
case Pinetime::Controllers::SimpleWeatherService::Icons::Snow:
132+
return "Snow";
133+
case Pinetime::Controllers::SimpleWeatherService::Icons::Smog:
134+
return "Mist";
135+
default:
136+
return "";
137+
}
138+
}

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+
const char* getCondition(const Pinetime::Controllers::SimpleWeatherService::Icons icon);
30+
Controllers::Settings& settingsController;
31+
Controllers::SimpleWeatherService& weatherService;
32+
33+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::CurrentWeather>> currentWeather {};
34+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::Forecast>> currentForecast {};
35+
36+
lv_obj_t* condition;
37+
lv_obj_t* icon;
38+
lv_obj_t* temperature;
39+
lv_obj_t* forecast;
40+
lv_obj_t* forecastSym;
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::cloudSun;
50+
51+
static Screens::Screen* Create(AppControllers& controllers) {
52+
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
53+
};
54+
};
55+
}
56+
}

0 commit comments

Comments
 (0)