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"
5761#include < iostream>
5862#include < typeinfo>
5963#include < algorithm>
64+ #include < array>
65+ #include < vector>
66+ #include < span>
6067#include < cmath> // std::pow
6168
6269// additional includes for 'saveScreenshot()' function
@@ -806,19 +813,85 @@ class Framework {
806813 batteryController.voltage = batteryController.percentRemaining * 50 ;
807814 }
808815
816+ void write_uint64 (std::span<uint8_t > data, uint64_t val)
817+ {
818+ assert (data.size () >= 8 );
819+ for (int i=0 ; i<8 ; i++)
820+ {
821+ data[i] = (val >> (i*8 )) & 0xff ;
822+ }
823+ }
824+ void write_int16 (std::span<uint8_t > data, int16_t val)
825+ {
826+ assert (data.size () >= 2 );
827+ data[0 ] = val & 0xff ;
828+ data[1 ] = (val >> 8 ) & 0xff ;
829+ }
830+ void set_current_weather (uint64_t timestamp, int16_t temperature, int iconId)
831+ {
832+ std::array<uint8_t , 49 > dataBuffer {};
833+ std::span<uint8_t > data (dataBuffer);
834+ os_mbuf buffer;
835+ ble_gatt_access_ctxt ctxt;
836+ ctxt.om = &buffer;
837+ buffer.om_data = dataBuffer.data ();
838+
839+ // fill buffer with specified format
840+ int16_t minTemperature = temperature;
841+ int16_t maxTemperature = temperature;
842+ dataBuffer.at (0 ) = 0 ; // MessageType::CurrentWeather
843+ dataBuffer.at (1 ) = 0 ; // Vesion 0
844+ write_uint64 (data.subspan (2 ), timestamp);
845+ write_int16 (data.subspan (10 ), temperature);
846+ write_int16 (data.subspan (12 ), minTemperature);
847+ write_int16 (data.subspan (14 ), maxTemperature);
848+ dataBuffer.at (48 ) = static_cast <uint8_t >(iconId);
849+
850+ // send weather to SimpleWeatherService
851+ systemTask.nimble ().weather ().OnCommand (&ctxt);
852+ }
853+ void set_forecast (
854+ uint64_t timestamp,
855+ std::array<
856+ Pinetime::Controllers::SimpleWeatherService::Forecast::Day,
857+ Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days)
858+ {
859+ std::array<uint8_t , 36 > dataBuffer {};
860+ std::span<uint8_t > data (dataBuffer);
861+ os_mbuf buffer;
862+ ble_gatt_access_ctxt ctxt;
863+ ctxt.om = &buffer;
864+ buffer.om_data = dataBuffer.data ();
865+
866+ // fill buffer with specified format
867+ dataBuffer.at (0 ) = 1 ; // MessageType::Forecast
868+ dataBuffer.at (1 ) = 0 ; // Vesion 0
869+ write_uint64 (data.subspan (2 ), timestamp);
870+ dataBuffer.at (10 ) = static_cast <uint8_t >(days.size ());
871+ for (int i = 0 ; i < days.size (); i++)
872+ {
873+ const Pinetime::Controllers::SimpleWeatherService::Forecast::Day &day = days.at (i);
874+ write_int16 (data.subspan (11 +(i*5 )), day.minTemperature );
875+ write_int16 (data.subspan (13 +(i*5 )), day.maxTemperature );
876+ dataBuffer.at (15 +(i*5 )) = static_cast <uint8_t >(day.iconId );
877+ }
878+ // send Forecast to SimpleWeatherService
879+ systemTask.nimble ().weather ().OnCommand (&ctxt);
880+ }
881+
809882 void generate_weather_data (bool clear) {
810883 if (clear) {
811- systemTask. nimble (). weather (). SetCurrentWeather (0 , 0 , 0 );
884+ set_current_weather (0 , 0 , 0 );
812885 std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
813- systemTask. nimble (). weather (). SetForecast (0 , days);
886+ set_forecast (0 , days);
814887 return ;
815888 }
816889 auto timestamp = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
817890 srand ((int )timestamp);
818891
819892 // Generate current weather data
820893 int16_t temperature = (rand () % 81 - 40 ) * 100 ;
821- systemTask. nimble (). weather (). SetCurrentWeather ((uint64_t )timestamp, temperature, rand () % 9 );
894+ set_current_weather ((uint64_t )timestamp, temperature, rand () % 9 );
822895
823896 // Generate forecast data
824897 std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
@@ -827,7 +900,7 @@ class Framework {
827900 (int16_t )(temperature - rand () % 10 * 100 ), (int16_t )(temperature + rand () % 10 * 100 ), Pinetime::Controllers::SimpleWeatherService::Icons (rand () % 9 )
828901 };
829902 }
830- systemTask. nimble (). weather (). SetForecast ((uint64_t )timestamp, days);
903+ set_forecast ((uint64_t )timestamp, days);
831904 }
832905
833906 void handle_touch_and_button () {
0 commit comments