Skip to content

Commit

Permalink
weather: Add support for conditions
Browse files Browse the repository at this point in the history
Use OpenWeatherMap conditions to determine the correct condition icon.
This is more accurate than attempting to calculate the condition based
on precipitation and cloud cover, which often yields the wrong
condition.

Companion PR to Gadgetbridge: https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/3407
  • Loading branch information
vkareh committed Nov 3, 2023
1 parent e6b96c2 commit 91e1b4b
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 23 deletions.
34 changes: 34 additions & 0 deletions src/components/ble/weather/WeatherData.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ namespace Pinetime {
Length
};

/**
* List of weather condition codes used to determine display
* (range of thunderstorm, drizzle, rain, snow, clouds, atmosphere including extreme conditions like tornado, hurricane etc.)
* https://openweathermap.org/weather-conditions
*/
enum class conditiontype {
ClearSky = 0,
FewClouds = 1,
ScatteredClouds = 2,
BrokenClouds = 3,
ShowerRain = 4,
Rain = 5,
Thunderstorm = 6,
Snow = 7,
Mist = 8,
Length
};

/**
* These are used for weather timeline manipulation
* that isn't just adding to the stack of weather events
Expand Down Expand Up @@ -165,6 +183,8 @@ namespace Pinetime {
Clouds = 8,
/** @see humidity */
Humidity = 9,
/** @see condition */
Condition = 10,
Length
};

Expand Down Expand Up @@ -326,6 +346,20 @@ namespace Pinetime {
specialtype type;
};

/** How weather condition is stored */
class Condition : public TimelineHeader {
public:
/**
* Type of condition
*/
conditiontype type;

/**
* Condition code
*/
uint16_t code;
};

/**
* How air quality is stored
*
Expand Down
52 changes: 52 additions & 0 deletions src/components/ble/weather/WeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,34 @@ namespace Pinetime {
}
break;
}
case WeatherData::eventtype::Condition: {
std::unique_ptr<WeatherData::Condition> condition = std::make_unique<WeatherData::Condition>();
condition->timestamp = tmpTimestamp;
condition->eventType = static_cast<WeatherData::eventtype>(tmpEventType);
condition->expires = tmpExpires;

int64_t tmpType = 0;
QCBORDecode_GetInt64InMapSZ(&decodeContext, "Type", &tmpType);
if (tmpType < 0 || tmpType >= static_cast<int64_t>(WeatherData::conditiontype::Length)) {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
condition->type = static_cast<WeatherData::conditiontype>(tmpType);

int64_t tmpCode = 0;
QCBORDecode_GetInt64InMapSZ(&decodeContext, "Code", &tmpCode);
if (tmpCode < 0 || tmpCode >= 65535) {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
condition->code = static_cast<uint16_t>(tmpCode);

if (!AddEventToTimeline(std::move(condition))) {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
break;
}
default: {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
Expand Down Expand Up @@ -509,6 +537,30 @@ namespace Pinetime {
return reinterpret_cast<std::unique_ptr<WeatherData::AirQuality>&>(*this->nullHeader);
}

std::unique_ptr<WeatherData::Special>& WeatherService::GetCurrentSpecial() {
uint64_t currentTimestamp = GetCurrentUnixTimestamp();
for (auto&& header : this->timeline) {
if (header->eventType == WeatherData::eventtype::Special && currentTimestamp >= header->timestamp &&
IsEventStillValid(header, currentTimestamp)) {
return reinterpret_cast<std::unique_ptr<WeatherData::Special>&>(header);
}
}

return reinterpret_cast<std::unique_ptr<WeatherData::Special>&>(*this->nullHeader);
}

std::unique_ptr<WeatherData::Condition>& WeatherService::GetCurrentCondition() {
uint64_t currentTimestamp = GetCurrentUnixTimestamp();
for (auto&& header : this->timeline) {
if (header->eventType == WeatherData::eventtype::Condition && currentTimestamp >= header->timestamp &&
IsEventStillValid(header, currentTimestamp)) {
return reinterpret_cast<std::unique_ptr<WeatherData::Condition>&>(header);
}
}

return reinterpret_cast<std::unique_ptr<WeatherData::Condition>&>(*this->nullHeader);
}

size_t WeatherService::GetTimelineLength() const {
return timeline.size();
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ble/weather/WeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace Pinetime {
std::unique_ptr<WeatherData::Humidity>& GetCurrentHumidity();
std::unique_ptr<WeatherData::Pressure>& GetCurrentPressure();
std::unique_ptr<WeatherData::AirQuality>& GetCurrentQuality();
std::unique_ptr<WeatherData::Special>& GetCurrentSpecial();
std::unique_ptr<WeatherData::Condition>& GetCurrentCondition();

/**
* Searches for the current day's maximum temperature
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Pinetime {
Metronome,
Motion,
Steps,
// Weather,
PassKey,
QuickSettings,
Settings,
Expand Down
4 changes: 2 additions & 2 deletions src/displayapp/fonts/fonts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
{
"file": "FontAwesome5-Solid+Brands+Regular.woff",
"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"
"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, 0xf5d7, 0xf6d3, 0xf6be, 0xf4ba, 0xf6c4"
}
],
"bpp": 1,
Expand Down Expand Up @@ -68,7 +68,7 @@
"sources": [
{
"file": "FontAwesome5-Solid+Brands+Regular.woff",
"range": "0xf185, 0xf6c4, 0xf743, 0xf740, 0xf75f, 0xf0c2, 0xf05e"
"range": "0xf185, 0xf6c4, 0xf743, 0xf740, 0xf75f, 0xf0c2, 0xf05e, 0xf0e7, 0xf2dc"
}
],
"bpp": 1,
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/screens/ApplicationList.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Pinetime {
{Symbols::map, Apps::Navigation, Applications::Screens::Navigation::IsAvailable(filesystem)},
{Symbols::none, Apps::None, false},

// {Symbols::cloudSun, Apps::Weather, true},
// {"M", Apps::Motion},
}};
ScreenList<nScreens> screens;
Expand Down
2 changes: 2 additions & 0 deletions src/displayapp/screens/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace Pinetime {
static constexpr const char* cloudShowersHeavy = "\xEF\x9D\x80";
static constexpr const char* smog = "\xEF\x9D\x9F";
static constexpr const char* cloud = "\xEF\x83\x82";
static constexpr const char* bolt = "\xEF\x83\xA7";
static constexpr const char* snowflake = "\xEF\x8B\x9C";
static constexpr const char* ban = "\xEF\x81\x9E";

// lv_font_sys_48.c
Expand Down
47 changes: 31 additions & 16 deletions src/displayapp/screens/WatchFacePineTimeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/ble/NotificationManager.h"
#include "components/ble/weather/WeatherService.h"
#include "components/motion/MotionController.h"
#include "components/settings/Settings.h"
#include "displayapp/DisplayApp.h"
#include "components/ble/weather/WeatherService.h"

using namespace Pinetime::Applications::Screens;

Expand Down Expand Up @@ -537,24 +537,39 @@ void WatchFacePineTimeStyle::Refresh() {
}
}

if (weatherService.GetCurrentTemperature()->timestamp != 0 && weatherService.GetCurrentClouds()->timestamp != 0 &&
weatherService.GetCurrentPrecipitation()->timestamp != 0) {
if (weatherService.GetCurrentTemperature()->timestamp != 0 && weatherService.GetCurrentCondition()->timestamp != 0) {
nowTemp = (weatherService.GetCurrentTemperature()->temperature / 100);
clouds = (weatherService.GetCurrentClouds()->amount);
precip = (weatherService.GetCurrentPrecipitation()->amount);
if (nowTemp.IsUpdated()) {
lv_label_set_text_fmt(temperature, "%d°", nowTemp.Get());
if ((clouds <= 30) && (precip == 0)) {
lv_label_set_text(weatherIcon, Symbols::sun);
} else if ((clouds >= 70) && (clouds <= 90) && (precip == 1)) {
lv_label_set_text(weatherIcon, Symbols::cloudSunRain);
} else if ((clouds > 90) && (precip == 0)) {
lv_label_set_text(weatherIcon, Symbols::cloud);
} else if ((clouds > 70) && (precip >= 2)) {
lv_label_set_text(weatherIcon, Symbols::cloudShowersHeavy);
} else {
lv_label_set_text(weatherIcon, Symbols::cloudSun);
};
switch (weatherService.GetCurrentCondition()->type) {
case Controllers::WeatherData::conditiontype::ClearSky:
lv_label_set_text(weatherIcon, Symbols::sun);
break;
case Controllers::WeatherData::conditiontype::FewClouds:
case Controllers::WeatherData::conditiontype::ScatteredClouds:
lv_label_set_text(weatherIcon, Symbols::cloudSun);
break;
case Controllers::WeatherData::conditiontype::BrokenClouds:
lv_label_set_text(weatherIcon, Symbols::cloud);
break;
case Controllers::WeatherData::conditiontype::ShowerRain:
lv_label_set_text(weatherIcon, Symbols::cloudShowersHeavy);
break;
case Controllers::WeatherData::conditiontype::Rain:
lv_label_set_text(weatherIcon, Symbols::cloudSunRain);
break;
case Controllers::WeatherData::conditiontype::Thunderstorm:
lv_label_set_text(weatherIcon, Symbols::bolt);
break;
case Controllers::WeatherData::conditiontype::Snow:
lv_label_set_text(weatherIcon, Symbols::snowflake);
break;
case Controllers::WeatherData::conditiontype::Mist:
lv_label_set_text(weatherIcon, Symbols::smog);
break;
default:
break;
}
lv_obj_realign(temperature);
lv_obj_realign(weatherIcon);
}
Expand Down
4 changes: 1 addition & 3 deletions src/displayapp/screens/WatchFacePineTimeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Pinetime {
Controllers::NotificationManager& notificationManager,
Controllers::Settings& settingsController,
Controllers::MotionController& motionController,
Controllers::WeatherService& weather);
Controllers::WeatherService& weatherService);
~WatchFacePineTimeStyle() override;

bool OnTouchEvent(TouchEvents event) override;
Expand Down Expand Up @@ -61,8 +61,6 @@ namespace Pinetime {
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<bool> notificationState {};
Utility::DirtyValue<int16_t> nowTemp {};
int16_t clouds = 0;
int16_t precip = 0;

static Pinetime::Controllers::Settings::Colors GetNext(Controllers::Settings::Colors color);
static Pinetime::Controllers::Settings::Colors GetPrevious(Controllers::Settings::Colors color);
Expand Down
39 changes: 38 additions & 1 deletion src/displayapp/screens/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Weather::Weather(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers:
{[this]() -> std::unique_ptr<Screen> {
return CreateScreenTemperature();
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreenCondition();
},
[this]() -> std::unique_ptr<Screen> {
return CreateScreenAir();
},
Expand Down Expand Up @@ -60,7 +63,7 @@ void Weather::Refresh() {

bool Weather::OnButtonPushed() {
running = false;
return true;
return false;
}

bool Weather::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
Expand Down Expand Up @@ -100,6 +103,40 @@ std::unique_ptr<Screen> Weather::CreateScreenTemperature() {
return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenCondition() {
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
std::unique_ptr<Controllers::WeatherData::Condition>& current = weatherService.GetCurrentCondition();
if (current->timestamp == 0) {
// Do not use the data, it's invalid
lv_label_set_text_fmt(label,
"#FFFF00 Condition#\n\n"
"#444444 %d# \n\n"
"#444444 %d# \n\n"
"%d\n"
"%d\n",
0,
0,
0,
0,
0);
} else {
lv_label_set_text_fmt(label,
"#FFFF00 Condition#\n\n"
"#444444 %d# \n\n"
"#444444 %d# \n\n"
"%llu\n"
"%lu\n",
current->type,
current->code,
current->timestamp,
current->expires);
}
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
return std::unique_ptr<Screen>(new Screens::Label(0, 5, label));
}

std::unique_ptr<Screen> Weather::CreateScreenAir() {
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_recolor(label, true);
Expand Down
4 changes: 3 additions & 1 deletion src/displayapp/screens/Weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ namespace Pinetime {

Controllers::WeatherService& weatherService;

ScreenList<5> screens;
ScreenList<6> screens;

std::unique_ptr<Screen> CreateScreenTemperature();

std::unique_ptr<Screen> CreateScreenCondition();

std::unique_ptr<Screen> CreateScreenAir();

std::unique_ptr<Screen> CreateScreenClouds();
Expand Down

0 comments on commit 91e1b4b

Please sign in to comment.