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

Remove sim SimpleWeatherService and implement needed nimble parts #153

Merged
merged 2 commits into from
Sep 25, 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
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ add_library(sim-base STATIC
sim/nrfx/mdk/nrf52_bitfields.h
# nrf/components/libraries/timer
sim/libraries/gpiote/app_gpiote.h # includes hal/nrf_gpio.h
# nibmle
sim/host/ble_gap.h
sim/host/ble_gatt.h
sim/host/ble_gatt.cpp
sim/host/ble_uuid.h
)
# include the generated lv_conf.h file before anything else
target_include_directories(sim-base PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") # lv_conf.h
Expand Down Expand Up @@ -128,8 +133,6 @@ target_sources(infinisim PUBLIC
sim/components/ble/NavigationService.cpp
sim/components/ble/NimbleController.h
sim/components/ble/NimbleController.cpp
sim/components/ble/SimpleWeatherService.h
sim/components/ble/SimpleWeatherService.cpp
sim/components/brightness/BrightnessController.h
sim/components/brightness/BrightnessController.cpp
sim/components/firmwarevalidator/FirmwareValidator.h
Expand Down Expand Up @@ -196,6 +199,8 @@ target_sources(infinisim PUBLIC
${InfiniTime_DIR}/src/components/settings/Settings.cpp
${InfiniTime_DIR}/src/components/ble/NotificationManager.h
${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp
${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.h
${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.cpp
${InfiniTime_DIR}/src/components/fs/FS.h
${InfiniTime_DIR}/src/components/fs/FS.cpp
${InfiniTime_DIR}/src/components/motor/MotorController.h
Expand Down
81 changes: 77 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <drivers/Hrs3300.h>
#include <drivers/Bma421.h>

# // be sure to get the sim headers for SimpleWeatherService.h
#include "host/ble_gatt.h"
#include "host/ble_uuid.h"

#include "BootloaderVersion.h"
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
Expand Down Expand Up @@ -57,6 +61,9 @@
#include <iostream>
#include <typeinfo>
#include <algorithm>
#include <array>
#include <vector>
#include <span>
#include <cmath> // std::pow

// additional includes for 'saveScreenshot()' function
Expand Down Expand Up @@ -806,19 +813,85 @@ class Framework {
batteryController.voltage = batteryController.percentRemaining * 50;
}

void write_uint64(std::span<uint8_t> data, uint64_t val)
{
assert(data.size() >= 8);
for (int i=0; i<8; i++)
{
data[i] = (val >> (i*8)) & 0xff;
}
}
void write_int16(std::span<uint8_t> data, int16_t val)
{
assert(data.size() >= 2);
data[0] = val & 0xff;
data[1] = (val >> 8) & 0xff;
}
void set_current_weather(uint64_t timestamp, int16_t temperature, int iconId)
{
std::array<uint8_t, 49> dataBuffer {};
std::span<uint8_t> data(dataBuffer);
os_mbuf buffer;
ble_gatt_access_ctxt ctxt;
ctxt.om = &buffer;
buffer.om_data = dataBuffer.data();

// fill buffer with specified format
int16_t minTemperature = temperature;
int16_t maxTemperature = temperature;
dataBuffer.at(0) = 0; // MessageType::CurrentWeather
dataBuffer.at(1) = 0; // Vesion 0
write_uint64(data.subspan(2), timestamp);
write_int16(data.subspan(10), temperature);
write_int16(data.subspan(12), minTemperature);
write_int16(data.subspan(14), maxTemperature);
dataBuffer.at(48) = static_cast<uint8_t>(iconId);

// send weather to SimpleWeatherService
systemTask.nimble().weather().OnCommand(&ctxt);
}
void set_forecast(
uint64_t timestamp,
std::array<
Pinetime::Controllers::SimpleWeatherService::Forecast::Day,
Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days)
{
std::array<uint8_t, 36> dataBuffer {};
std::span<uint8_t> data(dataBuffer);
os_mbuf buffer;
ble_gatt_access_ctxt ctxt;
ctxt.om = &buffer;
buffer.om_data = dataBuffer.data();

// fill buffer with specified format
dataBuffer.at(0) = 1; // MessageType::Forecast
dataBuffer.at(1) = 0; // Vesion 0
write_uint64(data.subspan(2), timestamp);
dataBuffer.at(10) = static_cast<uint8_t>(days.size());
for (int i = 0; i < days.size(); i++)
{
const Pinetime::Controllers::SimpleWeatherService::Forecast::Day &day = days.at(i);
write_int16(data.subspan(11+(i*5)), day.minTemperature);
write_int16(data.subspan(13+(i*5)), day.maxTemperature);
dataBuffer.at(15+(i*5)) = static_cast<uint8_t>(day.iconId);
}
// send Forecast to SimpleWeatherService
systemTask.nimble().weather().OnCommand(&ctxt);
}

void generate_weather_data(bool clear) {
if (clear) {
systemTask.nimble().weather().SetCurrentWeather(0, 0, 0);
set_current_weather(0, 0, 0);
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
systemTask.nimble().weather().SetForecast(0, days);
set_forecast(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);
set_current_weather((uint64_t)timestamp, temperature, rand() % 9);

// Generate forecast data
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
Expand All @@ -827,7 +900,7 @@ class Framework {
(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);
set_forecast((uint64_t)timestamp, days);
}

void handle_touch_and_button() {
Expand Down
138 changes: 0 additions & 138 deletions sim/components/ble/SimpleWeatherService.cpp

This file was deleted.

Loading
Loading