From bb18300c9e33a2bab55cb335c82cef30b98a680b Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Thu, 11 Jan 2024 12:52:44 -0500 Subject: [PATCH 1/2] SimpleWeatherService: Generate random weather data Add a new simulation key handler for weather data: - 'w' will generate new weather, cycling through the various conditions - 'W' will clear the weather data --- README.md | 2 ++ main.cpp | 22 +++++++++++++++++++++ sim/components/ble/SimpleWeatherService.cpp | 7 +++++++ sim/components/ble/SimpleWeatherService.h | 1 + 4 files changed, 32 insertions(+) diff --git a/README.md b/README.md index 0fabf4a..9f2ff23 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,8 @@ Using the keyboard the following events can be triggered: - `H` ... stop heartrate - `i` ... take screenshot - `I` ... start/stop Gif screen capture +- `w` ... generate weather data +- `W` ... clear weather data Additionally using the arrow keys the respective swipe gesture can be triggered. For example pressing the UP key triggers a `SwipeUp` gesture. diff --git a/main.cpp b/main.cpp index 8070db7..938b468 100644 --- a/main.cpp +++ b/main.cpp @@ -678,6 +678,7 @@ public: debounce('s', 'S', state[SDL_SCANCODE_S], key_handled_s); debounce('h', 'H', state[SDL_SCANCODE_H], key_handled_h); debounce('i', 'I', state[SDL_SCANCODE_I], key_handled_i); + debounce('w', 'W', state[SDL_SCANCODE_W], key_handled_w); // screen switcher buttons debounce('1', '!'+1, state[SDL_SCANCODE_1], key_handled_1); debounce('2', '!'+2, state[SDL_SCANCODE_2], key_handled_2); @@ -780,6 +781,10 @@ public: } else { gif_manager.close(); } + } else if (key == 'w') { + generate_weather_data(false); + } else if (key == 'W') { + generate_weather_data(true); } else if (key >= '0' && key <= '9') { this->switch_to_screen(key-'0'); } else if (key >= '!'+0 && key <= '!'+9) { @@ -796,6 +801,22 @@ public: batteryController.voltage = batteryController.percentRemaining * 50; } + void generate_weather_data(bool clear) { + static int iconId = -1; + if (clear) { + systemTask.nimble().weather().SetCurrentWeather(0, 0, 0); + return; + } + auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + srand((int)timestamp); + int temperature = (rand() % 81 - 40) * 100; + iconId++; + if (iconId > 8) { + iconId = 0; + } + systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, iconId); + } + void handle_touch_and_button() { int x, y; uint32_t buttons = SDL_GetMouseState(&x, &y); @@ -958,6 +979,7 @@ private: bool key_handled_s = false; // s ... increase step count, S ... decrease step count bool key_handled_h = false; // h ... set heartrate running, H ... stop heartrate bool key_handled_i = false; // i ... take screenshot, I ... start/stop Gif screen capture + bool key_handled_w = false; // w ... generate weather data, W ... clear weather data // numbers from 0 to 9 to switch between screens bool key_handled_1 = false; bool key_handled_2 = false; diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp index 01fc792..ffc881c 100644 --- a/sim/components/ble/SimpleWeatherService.cpp +++ b/sim/components/ble/SimpleWeatherService.cpp @@ -70,6 +70,13 @@ void SimpleWeatherService::Init() { //ble_gatts_add_svcs(serviceDefinition); } +void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId) { + SimpleWeatherService::Location cityName; + cityName[32] = '\0'; + currentWeather = SimpleWeatherService::CurrentWeather((uint64_t)timestamp, temperature, temperature, temperature, SimpleWeatherService::Icons(iconId), std::move(cityName)); + printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId); +} + int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { return 0; diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h index 1c1be02..43d9567 100644 --- a/sim/components/ble/SimpleWeatherService.h +++ b/sim/components/ble/SimpleWeatherService.h @@ -27,6 +27,7 @@ public: explicit SimpleWeatherService(const DateTime& dateTimeController); void Init(); + void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); int OnCommand(struct ble_gatt_access_ctxt* ctxt); From 30abc55b6c46b33cbcd8541ed92357179d9c22b1 Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Thu, 1 Feb 2024 10:56:34 -0500 Subject: [PATCH 2/2] SimpleWeatherService: Generate forecast data --- main.cpp | 20 ++++++++++++++------ sim/components/ble/SimpleWeatherService.cpp | 21 +++++++++++++++++++++ sim/components/ble/SimpleWeatherService.h | 8 +++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 938b468..3e57a1b 100644 --- a/main.cpp +++ b/main.cpp @@ -802,19 +802,27 @@ public: } void generate_weather_data(bool clear) { - static int iconId = -1; if (clear) { systemTask.nimble().weather().SetCurrentWeather(0, 0, 0); + std::array days; + systemTask.nimble().weather().SetForecast(0, days); return; } auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); srand((int)timestamp); - int temperature = (rand() % 81 - 40) * 100; - iconId++; - if (iconId > 8) { - iconId = 0; + + // Generate current weather data + int16_t temperature = (rand() % 81 - 40) * 100; + systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, rand() % 9); + + // Generate forecast data + std::array days; + for (int i = 0; i < Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { + days[i] = Pinetime::Controllers::SimpleWeatherService::Forecast::Day { + (int16_t)(temperature - rand() % 10 * 100), (int16_t)(temperature + rand() % 10 * 100), Pinetime::Controllers::SimpleWeatherService::Icons(rand() % 9) + }; } - systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, iconId); + systemTask.nimble().weather().SetForecast((uint64_t)timestamp, days); } void handle_touch_and_button() { diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp index ffc881c..024545b 100644 --- a/sim/components/ble/SimpleWeatherService.cpp +++ b/sim/components/ble/SimpleWeatherService.cpp @@ -77,6 +77,13 @@ void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t tempera printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId); } +void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array days) { + forecast = SimpleWeatherService::Forecast {timestamp, SimpleWeatherService::MaxNbForecastDays, days}; + for (int i = 0; i < SimpleWeatherService::MaxNbForecastDays; i++) { + printf("forecast: day=%d. min=%d, max=%d icon=%d\n", i, days[i].minTemperature, days[i].maxTemperature, days[i].iconId); + } +} + int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { return 0; @@ -115,3 +122,17 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService 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 == other.maxTemperature && this->minTemperature == other.maxTemperature; +} + +bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const { + for (int i = 0; i < this->nbDays; i++) { + if (this->days[i] != other.days[i]) { + return false; + } + } + return this->timestamp == other.timestamp && this->nbDays == other.nbDays; +} diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h index 43d9567..027518d 100644 --- a/sim/components/ble/SimpleWeatherService.h +++ b/sim/components/ble/SimpleWeatherService.h @@ -27,7 +27,6 @@ public: explicit SimpleWeatherService(const DateTime& dateTimeController); void Init(); - void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); int OnCommand(struct ble_gatt_access_ctxt* ctxt); @@ -81,11 +80,18 @@ public: int16_t minTemperature; int16_t maxTemperature; Icons iconId; + + bool operator==(const Day& other) const; }; std::array days; + + bool operator==(const Forecast& other) const; }; + void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); + void SetForecast(uint64_t timestamp, std::array days); + std::optional Current() const; std::optional GetForecast() const;