InfiniTime.git

commit 50c679023f48869a62bb31334fdd747f4e310eff

Author: Jean-François Milants <jf@codingfield.com>

SimpleWeather service : new weather implementation

Fix recovery firmware and code formatting.

 src/components/ble/SimpleWeatherService.cpp | 71 ++++++++--------
 src/components/ble/SimpleWeatherService.h | 38 +++++---
 src/displayapp/DisplayAppRecovery.cpp | 2 
 src/displayapp/DisplayAppRecovery.h | 4 
 src/displayapp/screens/WatchFacePineTimeStyle.cpp | 40 +++++++--


diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp
index fa0ce1987a28e1bebf16f8c0a8bf77adb1b84dc2..729e9352a037a446851fc86fd2ea069d85382e0f 100644
--- a/src/components/ble/SimpleWeatherService.cpp
+++ b/src/components/ble/SimpleWeatherService.cpp
@@ -22,45 +22,45 @@ #include 
 using namespace Pinetime::Controllers;
 
 namespace {
-  enum class MessageType {
-    CurrentWeather,
-    Forecast,
-    Unknown
-  };
+  enum class MessageType { CurrentWeather, Forecast, Unknown };
 
   SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuffer) {
     char cityName[33];
     std::memcpy(&cityName[0], &dataBuffer[13], 32);
     cityName[32] = '\0';
-    return SimpleWeatherService::CurrentWeather{dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
-        ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) + ((uint64_t) dataBuffer[8] << 48) +
-        ((uint64_t) dataBuffer[9] << 54),
-        dataBuffer[10],
-        dataBuffer[11],
-        dataBuffer[12],
-        dataBuffer[13 + 32],
-        cityName};
+    return SimpleWeatherService::CurrentWeather {dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
+                                                   ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) +
+                                                   ((uint64_t) dataBuffer[8] << 48) + ((uint64_t) dataBuffer[9] << 54),
+                                                 dataBuffer[10],
+                                                 dataBuffer[11],
+                                                 dataBuffer[12],
+                                                 dataBuffer[13 + 32],
+                                                 cityName};
   }
 
   SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
     uint64_t timestamp = static_cast<uint64_t>(dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
-                                               ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) + ((uint64_t) dataBuffer[8] << 48) +
-                                               ((uint64_t) dataBuffer[9] << 54));
+                                               ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) +
+                                               ((uint64_t) dataBuffer[8] << 48) + ((uint64_t) dataBuffer[9] << 54));
     uint8_t nbDays = dataBuffer[10];
     std::array<SimpleWeatherService::Forecast::Day, 5> days;
     for (int i = 0; i < nbDays; i++) {
-      days[i] = SimpleWeatherService::Forecast::Day {dataBuffer[11 + (i * 3)],
-                                                     dataBuffer[12 + (i * 3)],
-                                                     dataBuffer[13 + (i * 3)]};
+      days[i] = SimpleWeatherService::Forecast::Day {dataBuffer[11 + (i * 3)], dataBuffer[12 + (i * 3)], dataBuffer[13 + (i * 3)]};
     }
     return SimpleWeatherService::Forecast {timestamp, nbDays, days};
   }
 
   MessageType GetMessageType(const uint8_t* dataBuffer) {
-    switch(dataBuffer[0]) {
-      case 0: return MessageType::CurrentWeather; break;
-      case 1: return MessageType::Forecast; break;
-      default: return MessageType::Unknown; break;
+    switch (dataBuffer[0]) {
+      case 0:
+        return MessageType::CurrentWeather;
+        break;
+      case 1:
+        return MessageType::Forecast;
+        break;
+      default:
+        return MessageType::Unknown;
+        break;
     }
   }
 
@@ -74,7 +74,6 @@   return static_cast(arg)->OnCommand(ctxt);
 }
 
 SimpleWeatherService::SimpleWeatherService(const DateTime& dateTimeController) : dateTimeController(dateTimeController) {
-
 }
 
 void SimpleWeatherService::Init() {
@@ -86,9 +85,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
   const auto* buffer = ctxt->om;
   const auto* dataBuffer = buffer->om_data;
 
-  switch(GetMessageType(dataBuffer)) {
+  switch (GetMessageType(dataBuffer)) {
     case MessageType::CurrentWeather:
-      if(GetVersion(dataBuffer) == 0) {
+      if (GetVersion(dataBuffer) == 0) {
         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,
@@ -100,11 +99,15 @@                      currentWeather->location);
       }
       break;
     case MessageType::Forecast:
-      if(GetVersion(dataBuffer) == 0) {
+      if (GetVersion(dataBuffer) == 0) {
         forecast = CreateForecast(dataBuffer);
         NRF_LOG_INFO("Forecast : Timestamp : %d", forecast->timestamp);
-        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].iconId);
+        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].iconId);
         }
       }
       break;
@@ -116,13 +119,13 @@   return 0;
 }
 
 std::optional<SimpleWeatherService::CurrentWeather> SimpleWeatherService::Current() const {
-  if(currentWeather) {
+  if (currentWeather) {
     auto currentTime = dateTimeController.UTCDateTime().time_since_epoch();
-    auto weatherTpSecond = std::chrono::seconds{currentWeather->timestamp};
+    auto weatherTpSecond = std::chrono::seconds {currentWeather->timestamp};
     auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
     auto delta = currentTime - weatherTp;
 
-    if(delta < std::chrono::hours{24}) {
+    if (delta < std::chrono::hours {24}) {
       return currentWeather;
     }
   }
@@ -130,13 +133,13 @@   return {};
 }
 
 std::optional<SimpleWeatherService::Forecast> SimpleWeatherService::GetForecast() const {
-  if(forecast) {
+  if (forecast) {
     auto currentTime = dateTimeController.UTCDateTime().time_since_epoch();
-    auto weatherTpSecond = std::chrono::seconds{forecast->timestamp};
+    auto weatherTpSecond = std::chrono::seconds {forecast->timestamp};
     auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
     auto delta = currentTime - weatherTp;
 
-    if(delta < std::chrono::hours{24}) {
+    if (delta < std::chrono::hours {24}) {
       return this->forecast;
     }
   }




diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h
index 46d5e187f331e9aca34794805bb1914195d42e05..9a023af1e7878e6c15d162f922300353bdbaf99d 100644
--- a/src/components/ble/SimpleWeatherService.h
+++ b/src/components/ble/SimpleWeatherService.h
@@ -47,12 +47,12 @@
       int OnCommand(struct ble_gatt_access_ctxt* ctxt);
 
       enum class Icons : uint8_t {
-        Sun = 0, // ClearSky
+        Sun = 0,       // ClearSky
         CloudsSun = 1, // FewClouds
-        Clouds = 2, // Scattered clouds
+        Clouds = 2,    // Scattered clouds
         BrokenClouds = 3,
         CloudShowerHeavy = 4, // shower rain
-        CloudSunRain = 5, // rain
+        CloudSunRain = 5,     // rain
         Thunderstorm = 6,
         Snow = 7,
         Smog = 8, // Mist
@@ -60,13 +60,21 @@         Unknown = 255
       };
 
       struct CurrentWeather {
-        CurrentWeather(uint64_t timestamp, uint8_t temperature, uint8_t minTemperature, uint8_t maxTemperature,
-                       uint8_t iconId, const char* location)
-          : timestamp{timestamp}, temperature{temperature}, minTemperature{minTemperature}, maxTemperature{maxTemperature},
-            iconId{iconId} {
+        CurrentWeather(uint64_t timestamp,
+                       uint8_t temperature,
+                       uint8_t minTemperature,
+                       uint8_t maxTemperature,
+                       uint8_t iconId,
+                       const char* location)
+          : timestamp {timestamp},
+            temperature {temperature},
+            minTemperature {minTemperature},
+            maxTemperature {maxTemperature},
+            iconId {iconId} {
           std::memcpy(this->location, location, 32);
           this->location[32] = 0;
         }
+
         uint64_t timestamp;
         uint8_t temperature;
         uint8_t minTemperature;
@@ -81,18 +89,19 @@
       struct Forecast {
         uint64_t timestamp;
         uint8_t nbDays;
+
         struct Day {
           uint8_t minTemperature;
           uint8_t maxTemperature;
           uint8_t iconId;
         };
+
         std::array<Day, 5> days;
       };
 
       std::optional<CurrentWeather> Current() const;
       std::optional<Forecast> GetForecast() const;
 
-
     private:
       // 00050000-78fc-48fe-8e23-433b3a1942d0
       static constexpr ble_uuid128_t BaseUuid() {
@@ -109,13 +118,12 @@       ble_uuid128_t weatherUuid {BaseUuid()};
 
       ble_uuid128_t weatherDataCharUuid {CharUuid(0x00, 0x01)};
 
-      const struct ble_gatt_chr_def characteristicDefinition[2] = {
-        {.uuid = &weatherDataCharUuid.u,
-         .access_cb = WeatherCallback,
-         .arg = this,
-         .flags = BLE_GATT_CHR_F_WRITE,
-         .val_handle = &eventHandle},
-        {0}};
+      const struct ble_gatt_chr_def characteristicDefinition[2] = {{.uuid = &weatherDataCharUuid.u,
+                                                                    .access_cb = WeatherCallback,
+                                                                    .arg = this,
+                                                                    .flags = BLE_GATT_CHR_F_WRITE,
+                                                                    .val_handle = &eventHandle},
+                                                                   {0}};
       const struct ble_gatt_svc_def serviceDefinition[2] = {
         {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &weatherUuid.u, .characteristics = characteristicDefinition},
         {0}};




diff --git a/src/displayapp/DisplayAppRecovery.cpp b/src/displayapp/DisplayAppRecovery.cpp
index 9fe59c40a886c83429d8a18451333ed8ecd2272c..c4bd57669b482f9b68273b6b36527a753ca5d018 100644
--- a/src/displayapp/DisplayAppRecovery.cpp
+++ b/src/displayapp/DisplayAppRecovery.cpp
@@ -122,7 +122,7 @@
 void DisplayApp::Register(Pinetime::System::SystemTask* /*systemTask*/) {
 }
 
-void DisplayApp::Register(Pinetime::Controllers::WeatherService* /*weatherService*/) {
+void DisplayApp::Register(Pinetime::Controllers::SimpleWeatherService* /*weatherService*/) {
 }
 
 void DisplayApp::Register(Pinetime::Controllers::MusicService* /*musicService*/) {




diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h
index 41aedb179373dd0986f8bb59c189469cb3c64523..fd79ef287b07ec0b38e2adcb275753a19d48a666 100644
--- a/src/displayapp/DisplayAppRecovery.h
+++ b/src/displayapp/DisplayAppRecovery.h
@@ -34,7 +34,7 @@     class MotorController;
     class AlarmController;
     class BrightnessController;
     class FS;
-    class WeatherService;
+    class SimpleWeatherService;
     class MusicService;
     class NavigationService;
   }
@@ -69,7 +69,7 @@       };
 
       void PushMessage(Pinetime::Applications::Display::Messages msg);
       void Register(Pinetime::System::SystemTask* systemTask);
-      void Register(Pinetime::Controllers::WeatherService* weatherService);
+      void Register(Pinetime::Controllers::SimpleWeatherService* weatherService);
       void Register(Pinetime::Controllers::MusicService* musicService);
       void Register(Pinetime::Controllers::NavigationService* NavigationService);
 




diff --git a/src/displayapp/screens/WatchFacePineTimeStyle.cpp b/src/displayapp/screens/WatchFacePineTimeStyle.cpp
index 6512249341ffe744d7fb36e5b4e898c1952cc373..baa2e1845bf664fcf12b91b6d90da93bd490ba6d 100644
--- a/src/displayapp/screens/WatchFacePineTimeStyle.cpp
+++ b/src/displayapp/screens/WatchFacePineTimeStyle.cpp
@@ -45,16 +45,36 @@   }
 
   const char* GetIcon(const Pinetime::Controllers::SimpleWeatherService::Icons icon) {
     switch (icon) {
-      case Pinetime::Controllers::SimpleWeatherService::Icons::Sun: return Symbols::sun; break;
-      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudsSun: return Symbols::cloudSun; break;
-      case Pinetime::Controllers::SimpleWeatherService::Icons::Clouds: return Symbols::cloud; break;
-      case Pinetime::Controllers::SimpleWeatherService::Icons::BrokenClouds: return Symbols::cloud; break; // TODO missing symbol
-      case Pinetime::Controllers::SimpleWeatherService::Icons::Thunderstorm: return Symbols::cloud; break; // TODO missing symbol
-      case Pinetime::Controllers::SimpleWeatherService::Icons::Snow: return Symbols::cloud; break; // TODO missing symbol
-      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudShowerHeavy: return Symbols::cloudShowersHeavy; break;
-      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudSunRain: return Symbols::cloudSunRain; break;
-      case Pinetime::Controllers::SimpleWeatherService::Icons::Smog: return Symbols::smog; break;
-      default: return Symbols::ban; break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::Sun:
+        return Symbols::sun;
+        break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudsSun:
+        return Symbols::cloudSun;
+        break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::Clouds:
+        return Symbols::cloud;
+        break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::BrokenClouds:
+        return Symbols::cloud;
+        break; // TODO missing symbol
+      case Pinetime::Controllers::SimpleWeatherService::Icons::Thunderstorm:
+        return Symbols::cloud;
+        break; // TODO missing symbol
+      case Pinetime::Controllers::SimpleWeatherService::Icons::Snow:
+        return Symbols::cloud;
+        break; // TODO missing symbol
+      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudShowerHeavy:
+        return Symbols::cloudShowersHeavy;
+        break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::CloudSunRain:
+        return Symbols::cloudSunRain;
+        break;
+      case Pinetime::Controllers::SimpleWeatherService::Icons::Smog:
+        return Symbols::smog;
+        break;
+      default:
+        return Symbols::ban;
+        break;
     }
   }
 }