From 0e24e2b29f08be4219c7d14e7f3d1d5808f577c9 Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Wed, 25 Sep 2024 21:59:25 +0200 Subject: [PATCH] main: use std::span instead of raw pointer for buffer access --- main.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 17003ac..ffc4299 100644 --- a/main.cpp +++ b/main.cpp @@ -63,6 +63,7 @@ #include #include #include +#include #include // std::pow // additional includes for 'saveScreenshot()' function @@ -812,21 +813,24 @@ class Framework { batteryController.voltage = batteryController.percentRemaining * 50; } - void write_uint64(uint8_t* data, uint64_t val) + void write_uint64(std::span data, uint64_t val) { + assert(data.size() >= 8); for (int i=0; i<8; i++) { data[i] = (val >> (i*8)) & 0xff; } } - void write_int16(uint8_t* data, int16_t val) + void write_int16(std::span 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 dataBuffer {}; + std::span data(dataBuffer); os_mbuf buffer; ble_gatt_access_ctxt ctxt; ctxt.om = &buffer; @@ -837,10 +841,10 @@ class Framework { 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); + 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(iconId); // send weather to SimpleWeatherService @@ -853,6 +857,7 @@ class Framework { Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days) { std::array dataBuffer {}; + std::span data(dataBuffer); os_mbuf buffer; ble_gatt_access_ctxt ctxt; ctxt.om = &buffer; @@ -861,13 +866,13 @@ class Framework { // fill buffer with specified format dataBuffer.at(0) = 1; // MessageType::Forecast dataBuffer.at(1) = 0; // Vesion 0 - write_uint64(&dataBuffer[2], timestamp); + write_uint64(data.subspan(2), timestamp); dataBuffer.at(10) = static_cast(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); + 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(day.iconId); } // send Forecast to SimpleWeatherService