aboutsummaryrefslogtreecommitdiffstats
path: root/src/components
diff options
context:
space:
mode:
authorFintasticMan <finlay.neon.kid@gmail.com>2024-10-02 11:58:32 +0200
committerJF <JF002@users.noreply.github.com>2024-11-04 21:22:38 +0100
commite247bd701903cc507ba0e0ac4f938ab4616562e7 (patch)
tree2b7e71bb5083eb57baa8056b1b78adc8a24946ae /src/components
parent29ad09f4ef54126831d36fe1b99e794059fc5421 (diff)
Switch to simpler temperature interface
Diffstat (limited to 'src/components')
-rw-r--r--src/components/ble/SimpleWeatherService.cpp27
-rw-r--r--src/components/ble/SimpleWeatherService.h29
2 files changed, 40 insertions, 16 deletions
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::Forecast> 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<char, 33>; // 32 char + \0 (end of string)