This commit is contained in:
FintasticMan 2024-10-09 10:06:34 +00:00 committed by GitHub
commit ce6334382b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 87 additions and 64 deletions

View file

@ -42,9 +42,9 @@ namespace {
std::memcpy(cityName.data(), &dataBuffer[16], 32); std::memcpy(cityName.data(), &dataBuffer[16], 32);
cityName[32] = '\0'; cityName[32] = '\0';
return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]), return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]),
ToInt16(&dataBuffer[10]), SimpleWeatherService::Temperature(ToInt16(&dataBuffer[10])),
ToInt16(&dataBuffer[12]), SimpleWeatherService::Temperature(ToInt16(&dataBuffer[12])),
ToInt16(&dataBuffer[14]), SimpleWeatherService::Temperature(ToInt16(&dataBuffer[14])),
SimpleWeatherService::Icons {dataBuffer[16 + 32]}, SimpleWeatherService::Icons {dataBuffer[16 + 32]},
std::move(cityName)); std::move(cityName));
} }
@ -52,12 +52,12 @@ namespace {
SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2])); auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2]));
std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days; std::array<std::optional<SimpleWeatherService::Forecast::Day>, SimpleWeatherService::MaxNbForecastDays> days;
const uint8_t nbDaysInBuffer = dataBuffer[10]; const uint8_t nbDaysInBuffer = dataBuffer[10];
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer); const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
for (int i = 0; i < nbDays; i++) { for (int i = 0; i < nbDays; i++) {
days[i] = SimpleWeatherService::Forecast::Day {ToInt16(&dataBuffer[11 + (i * 5)]), days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature(ToInt16(&dataBuffer[11 + (i * 5)])),
ToInt16(&dataBuffer[13 + (i * 5)]), SimpleWeatherService::Temperature(ToInt16(&dataBuffer[13 + (i * 5)])),
SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}}; SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}};
} }
return SimpleWeatherService::Forecast {timestamp, nbDays, days}; return SimpleWeatherService::Forecast {timestamp, nbDays, days};
@ -98,9 +98,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
currentWeather = CreateCurrentWeather(dataBuffer); 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", 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, currentWeather->timestamp,
currentWeather->temperature, currentWeather->temperature.PreciseCelsius(),
currentWeather->minTemperature, currentWeather->minTemperature.PreciseCelsius(),
currentWeather->maxTemperature, currentWeather->maxTemperature.PreciseCelsius(),
currentWeather->iconId, currentWeather->iconId,
currentWeather->location.data()); currentWeather->location.data());
} }
@ -112,9 +112,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d", NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d",
i, i,
forecast->days[i].minTemperature, forecast->days[i]->minTemperature.PreciseCelsius(),
forecast->days[i].maxTemperature, forecast->days[i]->maxTemperature.PreciseCelsius(),
forecast->days[i].iconId); forecast->days[i]->iconId);
} }
} }
break; break;

View file

@ -61,13 +61,42 @@ namespace Pinetime {
Unknown = 255 Unknown = 255
}; };
class Temperature {
public:
explicit Temperature(int16_t raw) : raw {raw} {
}
[[nodiscard]] int16_t PreciseCelsius() const {
return raw;
}
[[nodiscard]] int16_t PreciseFahrenheit() const {
return raw * 9 / 5 + 3200;
}
[[nodiscard]] int16_t Celsius() const {
return (PreciseCelsius() + 50) / 100;
}
[[nodiscard]] int16_t Fahrenheit() const {
return (PreciseFahrenheit() + 50) / 100;
}
bool operator==(const Temperature& other) const {
return raw == other.raw;
}
private:
int16_t raw;
};
using Location = std::array<char, 33>; // 32 char + \0 (end of string) using Location = std::array<char, 33>; // 32 char + \0 (end of string)
struct CurrentWeather { struct CurrentWeather {
CurrentWeather(uint64_t timestamp, CurrentWeather(uint64_t timestamp,
int16_t temperature, Temperature temperature,
int16_t minTemperature, Temperature minTemperature,
int16_t maxTemperature, Temperature maxTemperature,
Icons iconId, Icons iconId,
Location&& location) Location&& location)
: timestamp {timestamp}, : timestamp {timestamp},
@ -79,9 +108,9 @@ namespace Pinetime {
} }
uint64_t timestamp; uint64_t timestamp;
int16_t temperature; Temperature temperature;
int16_t minTemperature; Temperature minTemperature;
int16_t maxTemperature; Temperature maxTemperature;
Icons iconId; Icons iconId;
Location location; Location location;
@ -93,14 +122,14 @@ namespace Pinetime {
uint8_t nbDays; uint8_t nbDays;
struct Day { struct Day {
int16_t minTemperature; Temperature minTemperature;
int16_t maxTemperature; Temperature maxTemperature;
Icons iconId; Icons iconId;
bool operator==(const Day& other) const; bool operator==(const Day& other) const;
}; };
std::array<Day, MaxNbForecastDays> days; std::array<std::optional<Day>, MaxNbForecastDays> days;
bool operator==(const Forecast& other) const; bool operator==(const Forecast& other) const;
}; };
@ -108,10 +137,6 @@ namespace Pinetime {
std::optional<CurrentWeather> Current() const; std::optional<CurrentWeather> Current() const;
std::optional<Forecast> GetForecast() const; std::optional<Forecast> GetForecast() const;
static int16_t CelsiusToFahrenheit(int16_t celsius) {
return celsius * 9 / 5 + 3200;
}
private: private:
// 00050000-78fc-48fe-8e23-433b3a1942d0 // 00050000-78fc-48fe-8e23-433b3a1942d0
static constexpr ble_uuid128_t BaseUuid() { static constexpr ble_uuid128_t BaseUuid() {

View file

@ -2,6 +2,7 @@
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include <cstdio> #include <cstdio>
#include "displayapp/screens/NotificationIcon.h" #include "displayapp/screens/NotificationIcon.h"
#include "displayapp/screens/Symbols.h" #include "displayapp/screens/Symbols.h"
#include "displayapp/screens/WeatherSymbols.h" #include "displayapp/screens/WeatherSymbols.h"
@ -174,13 +175,12 @@ void WatchFaceDigital::Refresh() {
if (currentWeather.IsUpdated()) { if (currentWeather.IsUpdated()) {
auto optCurrentWeather = currentWeather.Get(); auto optCurrentWeather = currentWeather.Get();
if (optCurrentWeather) { if (optCurrentWeather) {
int16_t temp = optCurrentWeather->temperature; int16_t temp = optCurrentWeather->temperature.Celsius();
char tempUnit = 'C'; char tempUnit = 'C';
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); temp = optCurrentWeather->temperature.Fahrenheit();
tempUnit = 'F'; tempUnit = 'F';
} }
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
lv_label_set_text_fmt(temperature, "%d°%c", temp, tempUnit); 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));
} else { } else {

View file

@ -22,7 +22,7 @@
#include "displayapp/screens/WatchFacePineTimeStyle.h" #include "displayapp/screens/WatchFacePineTimeStyle.h"
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include <cstdio> #include <cstdio>
#include <displayapp/Colors.h> #include "displayapp/Colors.h"
#include "displayapp/screens/BatteryIcon.h" #include "displayapp/screens/BatteryIcon.h"
#include "displayapp/screens/BleIcon.h" #include "displayapp/screens/BleIcon.h"
#include "displayapp/screens/NotificationIcon.h" #include "displayapp/screens/NotificationIcon.h"
@ -543,11 +543,10 @@ void WatchFacePineTimeStyle::Refresh() {
if (currentWeather.IsUpdated()) { if (currentWeather.IsUpdated()) {
auto optCurrentWeather = currentWeather.Get(); auto optCurrentWeather = currentWeather.Get();
if (optCurrentWeather) { if (optCurrentWeather) {
int16_t temp = optCurrentWeather->temperature; int16_t temp = optCurrentWeather->temperature.Celsius();
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); temp = optCurrentWeather->temperature.Fahrenheit();
} }
temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
lv_label_set_text_fmt(temperature, "%d°", temp); 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));
} else { } else {

View file

@ -1,5 +1,7 @@
#include "displayapp/screens/Weather.h" #include "displayapp/screens/Weather.h"
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
#include "components/ble/SimpleWeatherService.h" #include "components/ble/SimpleWeatherService.h"
#include "components/datetime/DateTimeController.h" #include "components/datetime/DateTimeController.h"
#include "components/settings/Settings.h" #include "components/settings/Settings.h"
@ -10,31 +12,27 @@
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
namespace { namespace {
lv_color_t TemperatureColor(int16_t temperature) { lv_color_t TemperatureColor(Pinetime::Controllers::SimpleWeatherService::Temperature temp) {
if (temperature <= 0) { // freezing if (temp.Celsius() <= 0) { // freezing
return Colors::blue; return Colors::blue;
} else if (temperature <= 400) { // ice } else if (temp.Celsius() <= 4) { // ice
return LV_COLOR_CYAN; return LV_COLOR_CYAN;
} else if (temperature >= 2700) { // hot } else if (temp.Celsius() >= 27) { // hot
return Colors::deepOrange; return Colors::deepOrange;
} }
return Colors::orange; // normal return Colors::orange; // normal
} }
uint8_t TemperatureStyle(int16_t temperature) { uint8_t TemperatureStyle(Pinetime::Controllers::SimpleWeatherService::Temperature temp) {
if (temperature <= 0) { // freezing if (temp.Celsius() <= 0) { // freezing
return LV_TABLE_PART_CELL3; return LV_TABLE_PART_CELL3;
} else if (temperature <= 400) { // ice } else if (temp.Celsius() <= 4) { // ice
return LV_TABLE_PART_CELL4; return LV_TABLE_PART_CELL4;
} else if (temperature >= 2700) { // hot } else if (temp.Celsius() >= 27) { // hot
return LV_TABLE_PART_CELL6; return LV_TABLE_PART_CELL6;
} }
return LV_TABLE_PART_CELL5; // normal return LV_TABLE_PART_CELL5; // normal
} }
int16_t RoundTemperature(int16_t temp) {
return temp = temp / 100 + (temp % 100 >= 50 ? 1 : 0);
}
} }
Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService) Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleWeatherService& weatherService)
@ -120,22 +118,25 @@ void Weather::Refresh() {
if (currentWeather.IsUpdated()) { if (currentWeather.IsUpdated()) {
auto optCurrentWeather = currentWeather.Get(); auto optCurrentWeather = currentWeather.Get();
if (optCurrentWeather) { if (optCurrentWeather) {
int16_t temp = optCurrentWeather->temperature; int16_t temp = optCurrentWeather->temperature.Celsius();
int16_t minTemp = optCurrentWeather->minTemperature; int16_t minTemp = optCurrentWeather->minTemperature.Celsius();
int16_t maxTemp = optCurrentWeather->maxTemperature; int16_t maxTemp = optCurrentWeather->maxTemperature.Celsius();
lv_obj_set_style_local_text_color(temperature, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, TemperatureColor(temp));
char tempUnit = 'C'; char tempUnit = 'C';
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
temp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(temp); temp = optCurrentWeather->temperature.Fahrenheit();
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp); minTemp = optCurrentWeather->minTemperature.Fahrenheit();
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); maxTemp = optCurrentWeather->maxTemperature.Fahrenheit();
tempUnit = 'F'; tempUnit = 'F';
} }
lv_obj_set_style_local_text_color(temperature,
LV_LABEL_PART_MAIN,
LV_STATE_DEFAULT,
TemperatureColor(optCurrentWeather->temperature));
lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId)); lv_label_set_text(icon, Symbols::GetSymbol(optCurrentWeather->iconId));
lv_label_set_text(condition, Symbols::GetCondition(optCurrentWeather->iconId)); 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(temperature, "%d°%c", temp, tempUnit);
lv_label_set_text_fmt(minTemperature, "%d°", RoundTemperature(minTemp)); lv_label_set_text_fmt(minTemperature, "%d°", minTemp);
lv_label_set_text_fmt(maxTemperature, "%d°", RoundTemperature(maxTemp)); lv_label_set_text_fmt(maxTemperature, "%d°", maxTemp);
} else { } else {
lv_label_set_text(icon, ""); lv_label_set_text(icon, "");
lv_label_set_text(condition, ""); lv_label_set_text(condition, "");
@ -152,24 +153,22 @@ void Weather::Refresh() {
if (optCurrentForecast) { if (optCurrentForecast) {
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp)); std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { for (int i = 0; i < optCurrentForecast->nbDays; i++) {
int16_t maxTemp = optCurrentForecast->days[i].maxTemperature; int16_t minTemp = optCurrentForecast->days[i]->maxTemperature.Celsius();
int16_t minTemp = optCurrentForecast->days[i].minTemperature; int16_t maxTemp = optCurrentForecast->days[i]->minTemperature.Celsius();
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(maxTemp));
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(minTemp));
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
maxTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(maxTemp); minTemp = optCurrentForecast->days[i]->maxTemperature.Fahrenheit();
minTemp = Controllers::SimpleWeatherService::CelsiusToFahrenheit(minTemp); maxTemp = optCurrentForecast->days[i]->minTemperature.Fahrenheit();
} }
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i]->maxTemperature));
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i]->minTemperature));
uint8_t wday = localTime.tm_wday + i + 1; uint8_t wday = localTime.tm_wday + i + 1;
if (wday > 7) { if (wday > 7) {
wday -= 7; wday -= 7;
} }
maxTemp = RoundTemperature(maxTemp);
minTemp = RoundTemperature(minTemp);
const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday)); const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday));
lv_table_set_cell_value(forecast, 0, i, dayOfWeek); 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));
// Pad cells based on the largest number of digits on each column // Pad cells based on the largest number of digits on each column
char maxPadding[3] = " "; char maxPadding[3] = " ";
char minPadding[3] = " "; char minPadding[3] = " ";