From e5b73212f6addcfdb5e306df63d7135e543c4f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sat, 23 Dec 2023 17:18:41 +0100 Subject: [PATCH] Simple Weather Service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store temperatures as int16_t (instead of uint8_t previously). The temperature is expressed in °C * 100. --- doc/SimpleWeatherService.md | 42 +++++++++---------- src/components/ble/SimpleWeatherService.cpp | 31 ++++++++++---- src/components/ble/SimpleWeatherService.h | 16 +++---- .../screens/WatchFacePineTimeStyle.cpp | 2 +- 4 files changed, 53 insertions(+), 38 deletions(-) diff --git a/doc/SimpleWeatherService.md b/doc/SimpleWeatherService.md index 36993900..db8a6f65 100644 --- a/doc/SimpleWeatherService.md +++ b/doc/SimpleWeatherService.md @@ -28,11 +28,11 @@ The byte array must contain the following data: - [0] : Message type = `0` - [1] : Message version = `0` - [2][3][4][5][6][7][8][9] : Timestamp (64 bits UNIX timestamp, number of nanoseconds elapsed since 1 JAN 1970) - - [10] : Current temperature (°C) - - [11] : Minimum temperature (°C) - - [12] : Maximum temperature (°C) - - [13]..[44] : location (string, unused characters should be set to `0`) - - [45] : icon ID + - [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 - 0 = Sun, clear sky - 1 = Few clouds - 2 = Clouds @@ -48,21 +48,21 @@ The byte array must contain the following data: The byte array must contain the following data: - [0] : Message type = `0` - - [0] : Message version = `0` + - [1] : Message version = `0` - [2][3][4][5][6][7][8][9] : Timestamp (64 bits UNIX timestamp, number of nanoseconds elapsed since 1 JAN 1970) - [10] Number of days (Max 5, fields for unused days should be set to `0`) - - [11] Day 0 Minimum temperature - - [12] Day 0 Maximum temperature - - [13] Day 0 Icon ID - - [14] Day 1 Minimum temperature - - [15] Day 1 Maximum temperature - - [16] Day 1 Icon ID - - [17] Day 2 Minimum temperature - - [18] Day 2 Maximum temperature - - [19] Day 2 Icon ID - - [20] Day 3 Minimum temperature - - [21] Day 3 Maximum temperature - - [22] Day 3 Icon ID - - [23] Day 4 Minimum temperature - - [24] Day 4 Maximum temperature - - [25] Day 4 Icon ID \ No newline at end of file + - [11,12] Day 0 Minimum temperature (°C * 100) + - [13,14] Day 0 Maximum temperature (°C * 100) + - [15] Day 0 Icon ID + - [16,17] Day 1 Minimum temperature (°C * 100) + - [18,19] Day 1 Maximum temperature (°C * 100) + - [20] Day 1 Icon ID + - [21,22] Day 2 Minimum temperature (°C * 100) + - [23,24] Day 2 Maximum temperature (°C * 100) + - [25] Day 2 Icon ID + - [26,27] Day 3 Minimum temperature (°C * 100) + - [28,29] Day 3 Maximum temperature (°C * 100) + - [30] Day 3 Icon ID + - [31,32] Day 4 Minimum temperature (°C * 100) + - [33,34] Day 4 Maximum temperature (°C * 100) + - [35] Day 4 Icon ID \ No newline at end of file diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp index 2ba26321..90adb926 100644 --- a/src/components/ble/SimpleWeatherService.cpp +++ b/src/components/ble/SimpleWeatherService.cpp @@ -29,18 +29,30 @@ namespace { enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown }; uint64_t ToUInt64(const uint8_t* data) { - return *(reinterpret_cast(data)); + return data[0] + + (data[1] << 8) + + (data[2] << 16) + + (data[3] << 24) + + (static_cast(data[4]) << 32) + + (static_cast(data[5]) << 48) + + (static_cast(data[6]) << 48) + + (static_cast(data[7]) << 56); + } + + int16_t ToInt16(const uint8_t* data) { + return data[0] + + (data[1] << 8); } SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuffer) { SimpleWeatherService::Location cityName; - std::memcpy(cityName.data(), &dataBuffer[13], 32); + std::memcpy(cityName.data(), &dataBuffer[16], 32); cityName[32] = '\0'; return SimpleWeatherService::CurrentWeather (ToUInt64(&dataBuffer[2]), - dataBuffer[10], - dataBuffer[11], - dataBuffer[12], - SimpleWeatherService::Icons{dataBuffer[13 + 32]}, + ToInt16(&dataBuffer[10]), + ToInt16(&dataBuffer[12]), + ToInt16(&dataBuffer[14]), + SimpleWeatherService::Icons{dataBuffer[16 + 32]}, std::move(cityName)); } @@ -51,7 +63,10 @@ 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 {dataBuffer[11 + (i * 3)], dataBuffer[12 + (i * 3)], SimpleWeatherService::Icons{dataBuffer[13 + (i * 3)]}}; + days[i] = SimpleWeatherService::Forecast::Day { + ToInt16(&dataBuffer[11 + (i * 5)]), + ToInt16(&dataBuffer[13 + (i * 5)]), + SimpleWeatherService::Icons{dataBuffer[15 + (i * 5)]}}; } return SimpleWeatherService::Forecast {timestamp, nbDays, days}; } @@ -95,7 +110,7 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { currentWeather->minTemperature, currentWeather->maxTemperature, currentWeather->iconId, - currentWeather->location); + currentWeather->location.data()); } break; case MessageType::Forecast: diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 561917eb..02a4c1e4 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -64,9 +64,9 @@ namespace Pinetime { using Location = std::array; // 32 char + \0 (end of string) struct CurrentWeather { CurrentWeather(uint64_t timestamp, - uint8_t temperature, - uint8_t minTemperature, - uint8_t maxTemperature, + int16_t temperature, + int16_t minTemperature, + int16_t maxTemperature, Icons iconId, Location&& location) : timestamp {timestamp}, @@ -78,9 +78,9 @@ namespace Pinetime { } uint64_t timestamp; - uint8_t temperature; - uint8_t minTemperature; - uint8_t maxTemperature; + int16_t temperature; + int16_t minTemperature; + int16_t maxTemperature; Icons iconId; Location location; @@ -92,8 +92,8 @@ namespace Pinetime { uint8_t nbDays; struct Day { - uint8_t minTemperature; - uint8_t maxTemperature; + int16_t minTemperature; + int16_t maxTemperature; Icons iconId; }; diff --git a/src/displayapp/screens/WatchFacePineTimeStyle.cpp b/src/displayapp/screens/WatchFacePineTimeStyle.cpp index 5259d553..9885bb42 100644 --- a/src/displayapp/screens/WatchFacePineTimeStyle.cpp +++ b/src/displayapp/screens/WatchFacePineTimeStyle.cpp @@ -543,7 +543,7 @@ void WatchFacePineTimeStyle::Refresh() { if (currentWeather.IsUpdated()) { auto optCurrentWeather = currentWeather.Get(); if (optCurrentWeather) { - lv_label_set_text_fmt(temperature, "%d°", optCurrentWeather->temperature); + lv_label_set_text_fmt(temperature, "%d°", (optCurrentWeather->temperature)/100); lv_label_set_text(weatherIcon, Symbols::GetSymbol(optCurrentWeather->iconId)); lv_obj_realign(temperature); lv_obj_realign(weatherIcon);