25
25
#include < drivers/Hrs3300.h>
26
26
#include < drivers/Bma421.h>
27
27
28
+ # // be sure to get the sim headers for SimpleWeatherService.h
29
+ #include " host/ble_gatt.h"
30
+ #include " host/ble_uuid.h"
31
+
28
32
#include " BootloaderVersion.h"
29
33
#include " components/battery/BatteryController.h"
30
34
#include " components/ble/BleController.h"
57
61
#include < iostream>
58
62
#include < typeinfo>
59
63
#include < algorithm>
64
+ #include < array>
65
+ #include < vector>
66
+ #include < span>
60
67
#include < cmath> // std::pow
61
68
62
69
// additional includes for 'saveScreenshot()' function
@@ -806,19 +813,85 @@ class Framework {
806
813
batteryController.voltage = batteryController.percentRemaining * 50 ;
807
814
}
808
815
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
+
809
882
void generate_weather_data (bool clear) {
810
883
if (clear) {
811
- systemTask. nimble (). weather (). SetCurrentWeather (0 , 0 , 0 );
884
+ set_current_weather (0 , 0 , 0 );
812
885
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
813
- systemTask. nimble (). weather (). SetForecast (0 , days);
886
+ set_forecast (0 , days);
814
887
return ;
815
888
}
816
889
auto timestamp = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now ());
817
890
srand ((int )timestamp);
818
891
819
892
// Generate current weather data
820
893
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 );
822
895
823
896
// Generate forecast data
824
897
std::array<Pinetime::Controllers::SimpleWeatherService::Forecast::Day, Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days;
@@ -827,7 +900,7 @@ class Framework {
827
900
(int16_t )(temperature - rand () % 10 * 100 ), (int16_t )(temperature + rand () % 10 * 100 ), Pinetime::Controllers::SimpleWeatherService::Icons (rand () % 9 )
828
901
};
829
902
}
830
- systemTask. nimble (). weather (). SetForecast ((uint64_t )timestamp, days);
903
+ set_forecast ((uint64_t )timestamp, days);
831
904
}
832
905
833
906
void handle_touch_and_button () {
0 commit comments