From 76a50a48f4198bfc831a9f1fdeeaf97249a3c7c7 Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Wed, 7 Feb 2024 15:12:40 -0500 Subject: [PATCH] SimpleWeatherService: Add sunrise and sunset data --- CMakeLists.txt | 2 +- doc/SimpleWeatherService.md | 14 ++++--- src/components/ble/SimpleWeatherService.cpp | 39 +++++++++++++++++-- src/components/ble/SimpleWeatherService.h | 12 +++++- src/displayapp/fonts/fonts.json | 2 +- src/displayapp/screens/Symbols.h | 3 ++ src/displayapp/screens/WatchFaceDigital.cpp | 2 +- .../screens/WatchFacePineTimeStyle.cpp | 2 +- src/displayapp/screens/Weather.cpp | 4 +- src/displayapp/screens/WeatherSymbols.cpp | 12 +++++- src/displayapp/screens/WeatherSymbols.h | 2 +- 11 files changed, 76 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b5669b003..3250982d6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose Debug or Release") -project(pinetime VERSION 1.14.0 LANGUAGES C CXX ASM) +project(pinetime VERSION 1.15.0 LANGUAGES C CXX ASM) set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 20) diff --git a/doc/SimpleWeatherService.md b/doc/SimpleWeatherService.md index e0fffba788..e6b20aa8d0 100644 --- a/doc/SimpleWeatherService.md +++ b/doc/SimpleWeatherService.md @@ -17,23 +17,25 @@ The host uses this characteristic to update the current weather information and This characteristics accepts a byte array with the following 2-Bytes header: - - [0] Message Type : + - [0] Message Type : - `0` : Current weather - `1` : Forecast - - [1] Message Version : Version `0` is currently supported. Other versions might be added in future releases + - [1] Message Version : + - `0` : Currently supported + - `1` : Adds support for sunrise and sunset -### Current Weather +### Current Weather The byte array must contain the following data: - [0] : Message type = `0` - - [1] : Message version = `0` + - [1] : Message version = `1` - [2][3][4][5][6][7][8][9] : Timestamp (64 bits UNIX timestamp, number of seconds elapsed since 1 JAN 1970) in local time (the same timezone as the one used to set the time) - [10, 11] : Current temperature (°C * 100) - [12, 13] : Minimum temperature (°C * 100) - [14, 15] : Maximum temperature (°C * 100) - [16]..[47] : location (string, unused characters should be set to `0`) - - [48] : icon ID + - [48] : icon ID - 0 = Sun, clear sky - 1 = Few clouds - 2 = Clouds @@ -43,6 +45,8 @@ The byte array must contain the following data: - 6 = Thunderstorm - 7 = Snow - 8 = Mist, smog + - [49, 50] : Sunrise (16 bits, number of minutes elapsed since midnight) + - [51, 52] : Sunset (16 bits, number of minutes elapsed since midnight) ### Forecast diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp index 504cad14b5..22a4701437 100644 --- a/src/components/ble/SimpleWeatherService.cpp +++ b/src/components/ble/SimpleWeatherService.cpp @@ -33,6 +33,10 @@ namespace { (static_cast(data[5]) << 40) + (static_cast(data[6]) << 48) + (static_cast(data[7]) << 56); } + uint16_t ToUInt16(const uint8_t* data) { + return data[0] + (data[1] << 8); + } + int16_t ToInt16(const uint8_t* data) { return data[0] + (data[1] << 8); } @@ -41,12 +45,20 @@ namespace { SimpleWeatherService::Location cityName; std::memcpy(cityName.data(), &dataBuffer[16], 32); cityName[32] = '\0'; + uint16_t sunrise = 0; + uint16_t sunset = 0; + if (dataBuffer[1] > 0) { + sunrise = ToUInt16(&dataBuffer[49]); + sunset = ToUInt16(&dataBuffer[51]); + } return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]), ToInt16(&dataBuffer[10]), ToInt16(&dataBuffer[12]), ToInt16(&dataBuffer[14]), SimpleWeatherService::Icons {dataBuffer[16 + 32]}, - std::move(cityName)); + std::move(cityName), + sunrise, + sunset); } SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { @@ -94,7 +106,7 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { switch (GetMessageType(dataBuffer)) { case MessageType::CurrentWeather: - if (GetVersion(dataBuffer) == 0) { + if (GetVersion(dataBuffer) <= 1) { currentWeather = CreateCurrentWeather(dataBuffer); NRF_LOG_INFO("Current weather :\n\tTimestamp : %d\n\tTemperature:%d\n\tMin:%d\n\tMax:%d\n\tIcon:%d\n\tLocation:%s", currentWeather->timestamp, @@ -103,6 +115,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { currentWeather->maxTemperature, currentWeather->iconId, currentWeather->location.data()); + if (GetVersion(dataBuffer) == 1) { + NRF_LOG_INFO("Sunrise: %d\n\tSunset: %d", currentWeather->sunrise, currentWeather->sunset); + } } break; case MessageType::Forecast: @@ -153,10 +168,28 @@ std::optional SimpleWeatherService::GetForecast( return {}; } +bool SimpleWeatherService::IsNight() const { + if (currentWeather && currentWeather->sunrise > 0 && currentWeather->sunset > 0) { + auto currentTime = dateTimeController.CurrentDateTime().time_since_epoch(); + + // Convert current time to a time_point and get hours and minutes since midnight + auto currentSeconds = std::chrono::duration_cast(currentTime).count(); + std::time_t t = currentSeconds; + std::tm localTime = *std::localtime(&t); + + // Convert hours and minutes to minutes since midnight + uint16_t currentMinutes = localTime.tm_hour * 60 + localTime.tm_min; + + return currentMinutes < currentWeather->sunrise || currentMinutes > currentWeather->sunset; + } + + return false; +} + bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService::CurrentWeather& other) const { return this->iconId == other.iconId && this->temperature == other.temperature && this->timestamp == other.timestamp && this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature && - std::strcmp(this->location.data(), other.location.data()) == 0; + std::strcmp(this->location.data(), other.location.data()) == 0 && this->sunrise == other.sunrise && this->sunset == other.sunset; } bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const { diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 03d2f6ff03..04b16ecb7d 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -69,13 +69,17 @@ namespace Pinetime { int16_t minTemperature, int16_t maxTemperature, Icons iconId, - Location&& location) + Location&& location, + uint16_t sunrise, + uint16_t sunset) : timestamp {timestamp}, temperature {temperature}, minTemperature {minTemperature}, maxTemperature {maxTemperature}, iconId {iconId}, - location {std::move(location)} { + location {std::move(location)}, + sunrise {sunrise}, + sunset {sunset} { } uint64_t timestamp; @@ -84,6 +88,8 @@ namespace Pinetime { int16_t maxTemperature; Icons iconId; Location location; + uint16_t sunrise; + uint16_t sunset; bool operator==(const CurrentWeather& other) const; }; @@ -108,6 +114,8 @@ namespace Pinetime { std::optional Current() const; std::optional GetForecast() const; + bool IsNight() const; + static int16_t CelsiusToFahrenheit(int16_t celsius) { return celsius * 9 / 5 + 3200; } diff --git a/src/displayapp/fonts/fonts.json b/src/displayapp/fonts/fonts.json index 41c383c0d4..944d3fdc0c 100644 --- a/src/displayapp/fonts/fonts.json +++ b/src/displayapp/fonts/fonts.json @@ -68,7 +68,7 @@ "sources": [ { "file": "FontAwesome5-Solid+Brands+Regular.woff", - "range": "0xf185, 0xf6c4, 0xf743, 0xf740, 0xf75f, 0xf0c2, 0xf05e, 0xf73b, 0xf0e7, 0xf2dc" + "range": "0xf185, 0xf186, 0xf6c3, 0xf6c4, 0xf73c, 0xf743, 0xf740, 0xf75f, 0xf0c2, 0xf05e, 0xf73b, 0xf0e7, 0xf2dc" } ], "bpp": 1, diff --git a/src/displayapp/screens/Symbols.h b/src/displayapp/screens/Symbols.h index bd958b285f..4ac7e93d7c 100644 --- a/src/displayapp/screens/Symbols.h +++ b/src/displayapp/screens/Symbols.h @@ -42,8 +42,11 @@ namespace Pinetime { // fontawesome_weathericons.c // static constexpr const char* sun = "\xEF\x86\x85"; + static constexpr const char* moon = "\xEF\x86\x86"; // 0xf186 static constexpr const char* cloudSun = "\xEF\x9B\x84"; + static constexpr const char* cloudMoon = "\xEF\x9B\x83"; // 0xf6c3 static constexpr const char* cloudSunRain = "\xEF\x9D\x83"; + static constexpr const char* cloudMoonRain = "\xEF\x9C\xBC"; // 0xf73c static constexpr const char* cloudShowersHeavy = "\xEF\x9D\x80"; static constexpr const char* smog = "\xEF\x9D\x9F"; static constexpr const char* cloud = "\xEF\x83\x82"; diff --git a/src/displayapp/screens/WatchFaceDigital.cpp b/src/displayapp/screens/WatchFaceDigital.cpp index 2e00ee9819..497c6fb728 100644 --- a/src/displayapp/screens/WatchFaceDigital.cpp +++ b/src/displayapp/screens/WatchFaceDigital.cpp @@ -182,7 +182,7 @@ void WatchFaceDigital::Refresh() { } temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit); - lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId)); + lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId, weatherService.IsNight())); } else { lv_label_set_text_static(temperature, ""); lv_label_set_text(weatherIcon, ""); diff --git a/src/displayapp/screens/WatchFacePineTimeStyle.cpp b/src/displayapp/screens/WatchFacePineTimeStyle.cpp index e56031f74a..25d213f2d8 100644 --- a/src/displayapp/screens/WatchFacePineTimeStyle.cpp +++ b/src/displayapp/screens/WatchFacePineTimeStyle.cpp @@ -549,7 +549,7 @@ void WatchFacePineTimeStyle::Refresh() { } temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0); lv_label_set_text_fmt(temperature, "%d°", temp); - lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId)); + lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId, weatherService.IsNight())); } else { lv_label_set_text(temperature, "--"); lv_label_set_text(weatherIcon, Symbols::ban); diff --git a/src/displayapp/screens/Weather.cpp b/src/displayapp/screens/Weather.cpp index 5321b7cc29..fdee0b761e 100644 --- a/src/displayapp/screens/Weather.cpp +++ b/src/displayapp/screens/Weather.cpp @@ -131,7 +131,7 @@ void Weather::Refresh() { maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); tempUnit = 'F'; } - lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId)); + lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId, weatherService.IsNight())); lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId)); lv_label_set_text_fmt(temperature, "%d°%c", RoundTemperature(temp), tempUnit); lv_label_set_text_fmt(minTemperature, "%d°", RoundTemperature(minTemp)); @@ -169,7 +169,7 @@ void Weather::Refresh() { minTemp = RoundTemperature(minTemp); const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast(wday)); lv_table_set_cell_value(forecast, 0, i, dayOfWeek); - lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId)); + lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId, weatherService.IsNight())); // Pad cells based on the largest number of digits on each column char maxPadding[3] = " "; char minPadding[3] = " "; diff --git a/src/displayapp/screens/WeatherSymbols.cpp b/src/displayapp/screens/WeatherSymbols.cpp index de66312f90..8f1d49f4e9 100644 --- a/src/displayapp/screens/WeatherSymbols.cpp +++ b/src/displayapp/screens/WeatherSymbols.cpp @@ -1,11 +1,18 @@ #include "displayapp/screens/WeatherSymbols.h" -const char* Pinetime::Applications::Screens::Symbols::GetSymbol(const Pinetime::Controllers::SimpleWeatherService::Icons icon) { +const char* Pinetime::Applications::Screens::Symbols::GetSymbol(const Pinetime::Controllers::SimpleWeatherService::Icons icon, + const bool isNight) { switch (icon) { case Pinetime::Controllers::SimpleWeatherService::Icons::Sun: + if (isNight) { + return Symbols::moon; + } return Symbols::sun; break; case Pinetime::Controllers::SimpleWeatherService::Icons::CloudsSun: + if (isNight) { + return Symbols::cloudMoon; + } return Symbols::cloudSun; break; case Pinetime::Controllers::SimpleWeatherService::Icons::Clouds: @@ -24,6 +31,9 @@ const char* Pinetime::Applications::Screens::Symbols::GetSymbol(const Pinetime:: return Symbols::cloudShowersHeavy; break; case Pinetime::Controllers::SimpleWeatherService::Icons::CloudSunRain: + if (isNight) { + return Symbols::cloudMoonRain; + } return Symbols::cloudSunRain; break; case Pinetime::Controllers::SimpleWeatherService::Icons::Smog: diff --git a/src/displayapp/screens/WeatherSymbols.h b/src/displayapp/screens/WeatherSymbols.h index f3eeed5581..f362eff972 100644 --- a/src/displayapp/screens/WeatherSymbols.h +++ b/src/displayapp/screens/WeatherSymbols.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Applications { namespace Screens { namespace Symbols { - const char* GetSymbol(const Pinetime::Controllers::SimpleWeatherService::Icons icon); + const char* GetSymbol(const Pinetime::Controllers::SimpleWeatherService::Icons icon, const bool isNight); const char* GetCondition(const Pinetime::Controllers::SimpleWeatherService::Icons icon); } }