Skip to content

Commit fe1f581

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

11 files changed

+259
-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/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

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

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

src/displayapp/screens/Weather.h

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
std::array<const char*, 7> WeekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
24+
25+
Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService);
26+
~Weather() override;
27+
28+
void Refresh() override;
29+
30+
private:
31+
Controllers::Settings& settingsController;
32+
Controllers::SimpleWeatherService& weatherService;
33+
34+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::CurrentWeather>> currentWeather {};
35+
Utility::DirtyValue<std::optional<Controllers::SimpleWeatherService::Forecast>> currentForecast {};
36+
37+
lv_obj_t* icon;
38+
lv_obj_t* condition;
39+
lv_obj_t* temperature;
40+
lv_obj_t* minTemperature;
41+
lv_obj_t* maxTemperature;
42+
lv_obj_t* forecast;
43+
44+
lv_task_t* taskRefresh;
45+
};
46+
}
47+
48+
template <>
49+
struct AppTraits<Apps::Weather> {
50+
static constexpr Apps app = Apps::Weather;
51+
static constexpr const char* icon = Screens::Symbols::cloudSunRain;
52+
53+
static Screens::Screen* Create(AppControllers& controllers) {
54+
return new Screens::Weather(controllers.settingsController, *controllers.weatherController);
55+
};
56+
};
57+
}
58+
}

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)