From e247bd701903cc507ba0e0ac4f938ab4616562e7 Mon Sep 17 00:00:00 2001 From: FintasticMan Date: Wed, 2 Oct 2024 11:58:32 +0200 Subject: Switch to simpler temperature interface --- src/components/ble/SimpleWeatherService.cpp | 27 +++++++++++++-------------- src/components/ble/SimpleWeatherService.h | 29 +++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 16 deletions(-) (limited to 'src/components') diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp index a1f0439e..a58c3a76 100644 --- a/src/components/ble/SimpleWeatherService.cpp +++ b/src/components/ble/SimpleWeatherService.cpp @@ -42,9 +42,9 @@ namespace { std::memcpy(cityName.data(), &dataBuffer[16], 32); cityName[32] = '\0'; return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]), - SimpleWeatherService::Temperature {ToInt16(&dataBuffer[10])}, - SimpleWeatherService::Temperature {ToInt16(&dataBuffer[12])}, - SimpleWeatherService::Temperature {ToInt16(&dataBuffer[14])}, + SimpleWeatherService::Temperature(ToInt16(&dataBuffer[10])), + SimpleWeatherService::Temperature(ToInt16(&dataBuffer[12])), + SimpleWeatherService::Temperature(ToInt16(&dataBuffer[14])), SimpleWeatherService::Icons {dataBuffer[16 + 32]}, std::move(cityName)); } @@ -56,8 +56,8 @@ namespace { const uint8_t nbDaysInBuffer = dataBuffer[10]; const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer); for (int i = 0; i < nbDays; i++) { - days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature {ToInt16(&dataBuffer[11 + (i * 5)])}, - SimpleWeatherService::Temperature {ToInt16(&dataBuffer[13 + (i * 5)])}, + days[i] = SimpleWeatherService::Forecast::Day {SimpleWeatherService::Temperature(ToInt16(&dataBuffer[11 + (i * 5)])), + SimpleWeatherService::Temperature(ToInt16(&dataBuffer[13 + (i * 5)])), SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}}; } return SimpleWeatherService::Forecast {timestamp, nbDays, days}; @@ -98,9 +98,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { 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, - currentWeather->temperature, - currentWeather->minTemperature, - currentWeather->maxTemperature, + currentWeather->temperature.PreciseCelsius(), + currentWeather->minTemperature.PreciseCelsius(), + currentWeather->maxTemperature.PreciseCelsius(), currentWeather->iconId, currentWeather->location.data()); } @@ -112,8 +112,8 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { for (int i = 0; i < 5; i++) { NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d", i, - forecast->days[i].minTemperature, - forecast->days[i].maxTemperature, + forecast->days[i].minTemperature.PreciseCelsius(), + forecast->days[i].maxTemperature.PreciseCelsius(), forecast->days[i].iconId); } } @@ -154,14 +154,13 @@ std::optional SimpleWeatherService::GetForecast( } bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService::CurrentWeather& other) const { - return this->iconId == other.iconId && this->temperature.temp == other.temperature.temp && this->timestamp == other.timestamp && - this->maxTemperature.temp == other.maxTemperature.temp && this->minTemperature.temp == other.maxTemperature.temp && + 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; } bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const { - return this->iconId == other.iconId && this->maxTemperature.temp == other.maxTemperature.temp && - this->minTemperature.temp == other.maxTemperature.temp; + return this->iconId == other.iconId && this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature; } bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const { diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index ee40fd6f..36bbea48 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -61,8 +61,33 @@ namespace Pinetime { Unknown = 255 }; - struct Temperature { - int16_t temp; + class Temperature { + public: + explicit Temperature(int16_t raw = 0) : 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; // 32 char + \0 (end of string) -- cgit v1.2.3-70-g09d2