Skip to content

Commit 3507ee5

Browse files
committed
Remove sim SimpleWeatherService and implement needed nimble parts
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.
1 parent 7989538 commit 3507ee5

8 files changed

+1222
-280
lines changed

CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ add_library(sim-base STATIC
8787
sim/nrfx/mdk/nrf52_bitfields.h
8888
# nrf/components/libraries/timer
8989
sim/libraries/gpiote/app_gpiote.h # includes hal/nrf_gpio.h
90+
# nibmle
91+
sim/host/ble_gap.h
92+
sim/host/ble_gatt.h
93+
sim/host/ble_gatt.cpp
94+
sim/host/ble_uuid.h
9095
)
9196
# include the generated lv_conf.h file before anything else
9297
target_include_directories(sim-base PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") # lv_conf.h
@@ -128,8 +133,6 @@ target_sources(infinisim PUBLIC
128133
sim/components/ble/NavigationService.cpp
129134
sim/components/ble/NimbleController.h
130135
sim/components/ble/NimbleController.cpp
131-
sim/components/ble/SimpleWeatherService.h
132-
sim/components/ble/SimpleWeatherService.cpp
133136
sim/components/brightness/BrightnessController.h
134137
sim/components/brightness/BrightnessController.cpp
135138
sim/components/firmwarevalidator/FirmwareValidator.h
@@ -196,6 +199,8 @@ target_sources(infinisim PUBLIC
196199
${InfiniTime_DIR}/src/components/settings/Settings.cpp
197200
${InfiniTime_DIR}/src/components/ble/NotificationManager.h
198201
${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp
202+
${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.h
203+
${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.cpp
199204
${InfiniTime_DIR}/src/components/fs/FS.h
200205
${InfiniTime_DIR}/src/components/fs/FS.cpp
201206
${InfiniTime_DIR}/src/components/motor/MotorController.h

main.cpp

+72-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <drivers/Hrs3300.h>
2626
#include <drivers/Bma421.h>
2727

28+
# // be sure to get the sim headers for SimpleWeatherService.h
29+
#include "host/ble_gatt.h"
30+
#include "host/ble_uuid.h"
31+
2832
#include "BootloaderVersion.h"
2933
#include "components/battery/BatteryController.h"
3034
#include "components/ble/BleController.h"
@@ -57,6 +61,8 @@
5761
#include <iostream>
5862
#include <typeinfo>
5963
#include <algorithm>
64+
#include <array>
65+
#include <vector>
6066
#include <cmath> // std::pow
6167

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

815+
void write_uint64(uint8_t* data, uint64_t val)
816+
{
817+
for (int i=0; i<8; i++)
818+
{
819+
data[i] = (val >> (i*8)) & 0xff;
820+
}
821+
}
822+
void write_int16(uint8_t* data, int16_t val)
823+
{
824+
data[0] = val & 0xff;
825+
data[1] = (val >> 8) & 0xff;
826+
}
827+
void set_current_weather(uint64_t timestamp, int16_t temperature, int iconId)
828+
{
829+
std::array<uint8_t, 49> dataBuffer {};
830+
os_mbuf buffer;
831+
ble_gatt_access_ctxt ctxt;
832+
ctxt.om = &buffer;
833+
buffer.om_data = dataBuffer.data();
834+
835+
// fill buffer with specified format
836+
int16_t minTemperature = temperature;
837+
int16_t maxTemperature = temperature;
838+
dataBuffer.at(0) = 0; // MessageType::CurrentWeather
839+
dataBuffer.at(1) = 0; // Vesion 0
840+
write_uint64(&dataBuffer[2], timestamp);
841+
write_int16(&dataBuffer[10], temperature);
842+
write_int16(&dataBuffer[12], minTemperature);
843+
write_int16(&dataBuffer[14], maxTemperature);
844+
dataBuffer.at(48) = static_cast<uint8_t>(iconId);
845+
846+
// send weather to SimpleWeatherService
847+
systemTask.nimble().weather().OnCommand(&ctxt);
848+
}
849+
void set_forecast(
850+
uint64_t timestamp,
851+
std::array<
852+
Pinetime::Controllers::SimpleWeatherService::Forecast::Day,
853+
Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days)
854+
{
855+
std::array<uint8_t, 36> dataBuffer {};
856+
os_mbuf buffer;
857+
ble_gatt_access_ctxt ctxt;
858+
ctxt.om = &buffer;
859+
buffer.om_data = dataBuffer.data();
860+
861+
// fill buffer with specified format
862+
dataBuffer.at(0) = 1; // MessageType::Forecast
863+
dataBuffer.at(1) = 0; // Vesion 0
864+
write_uint64(&dataBuffer[2], timestamp);
865+
dataBuffer.at(10) = static_cast<uint8_t>(days.size());
866+
for (int i = 0; i < days.size(); i++)
867+
{
868+
const Pinetime::Controllers::SimpleWeatherService::Forecast::Day &day = days.at(i);
869+
write_int16(&dataBuffer[11+(i*5)], day.minTemperature);
870+
write_int16(&dataBuffer[13+(i*5)], day.maxTemperature);
871+
dataBuffer.at(15+(i*5)) = static_cast<uint8_t>(day.iconId);
872+
}
873+
// send Forecast to SimpleWeatherService
874+
systemTask.nimble().weather().OnCommand(&ctxt);
875+
}
876+
809877
void generate_weather_data(bool clear) {
810878
if (clear) {
811-
systemTask.nimble().weather().SetCurrentWeather(0, 0, 0);
879+
set_current_weather(0, 0, 0);
812880
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
813-
systemTask.nimble().weather().SetForecast(0, days);
881+
set_forecast(0, days);
814882
return;
815883
}
816884
auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
817885
srand((int)timestamp);
818886

819887
// Generate current weather data
820888
int16_t temperature = (rand() % 81 - 40) * 100;
821-
systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, rand() % 9);
889+
set_current_weather((uint64_t)timestamp, temperature, rand() % 9);
822890

823891
// Generate forecast data
824892
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
@@ -827,7 +895,7 @@ class Framework {
827895
(int16_t)(temperature - rand() % 10 * 100), (int16_t)(temperature + rand() % 10 * 100), Pinetime::Controllers::SimpleWeatherService::Icons(rand() % 9)
828896
};
829897
}
830-
systemTask.nimble().weather().SetForecast((uint64_t)timestamp, days);
898+
set_forecast((uint64_t)timestamp, days);
831899
}
832900

833901
void handle_touch_and_button() {

sim/components/ble/SimpleWeatherService.cpp

-138
This file was deleted.

0 commit comments

Comments
 (0)