Skip to content

Commit

Permalink
Remove sim SimpleWeatherService and implement needed nimble parts
Browse files Browse the repository at this point in the history
We currently have slightly modified InfiniTime version of
`SimpleWeatherService.h` and `SimpleWeatherService.cpp`, which need to
be updated in sync with InfiniTime.

Instead implement the needed parts of the nimble library to make the
InfiniTime version of the SimpleWeatherService work.
  • Loading branch information
NeroBurner committed Sep 24, 2024
1 parent 7989538 commit 3507ee5
Show file tree
Hide file tree
Showing 8 changed files with 1,222 additions and 280 deletions.
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
76 changes: 72 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,8 @@
#include <iostream>
#include <typeinfo>
#include <algorithm>
#include <array>
#include <vector>
#include <cmath> // std::pow

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

void write_uint64(uint8_t* data, uint64_t val)
{
for (int i=0; i<8; i++)
{
data[i] = (val >> (i*8)) & 0xff;
}
}
void write_int16(uint8_t* data, int16_t val)
{
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 {};
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(&dataBuffer[2], timestamp);
write_int16(&dataBuffer[10], temperature);
write_int16(&dataBuffer[12], minTemperature);
write_int16(&dataBuffer[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 {};
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(&dataBuffer[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(&dataBuffer[11+(i*5)], day.minTemperature);
write_int16(&dataBuffer[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 +895,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

0 comments on commit 3507ee5

Please sign in to comment.