Skip to content

Commit

Permalink
SimpleWeatherService: Generate random weather data
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vkareh committed Feb 1, 2024
1 parent 3ba07f5 commit 45a795e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ class Framework {
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);
Expand Down Expand Up @@ -780,6 +781,10 @@ class Framework {
} 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) {
Expand All @@ -796,6 +801,22 @@ class Framework {
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);
Expand Down Expand Up @@ -958,6 +979,7 @@ class Framework {
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;
Expand Down
7 changes: 7 additions & 0 deletions sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SimpleWeatherService {
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);

Expand Down

0 comments on commit 45a795e

Please sign in to comment.