Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleWeatherService: Generate random weather data #138

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 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,30 @@ class Framework {
batteryController.voltage = batteryController.percentRemaining * 50;
}

void generate_weather_data(bool clear) {
if (clear) {
systemTask.nimble().weather().SetCurrentWeather(0, 0, 0);
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> 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);

// 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<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> 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().SetForecast((uint64_t)timestamp, days);
}

void handle_touch_and_button() {
int x, y;
uint32_t buttons = SDL_GetMouseState(&x, &y);
Expand Down Expand Up @@ -958,6 +987,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
28 changes: 28 additions & 0 deletions sim/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ 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);
}

void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> 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;
Expand Down Expand Up @@ -108,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;
}
NeroBurner marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions sim/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,18 @@ class SimpleWeatherService {
int16_t minTemperature;
int16_t maxTemperature;
Icons iconId;

bool operator==(const Day& other) const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does this function come from? I don't think it is needed for the current implementation, and it is not in the current InfiniTime version of SimpleWeatherService.h

};

std::array<Day, MaxNbForecastDays> days;

bool operator==(const Forecast& other) const;
NeroBurner marked this conversation as resolved.
Show resolved Hide resolved
};

void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId);
void SetForecast(uint64_t timestamp, std::array<Forecast::Day, MaxNbForecastDays> days);

std::optional<CurrentWeather> Current() const;
std::optional<Forecast> GetForecast() const;

Expand Down
Loading