Skip to content

Commit

Permalink
main: use std::span instead of raw pointer for buffer access
Browse files Browse the repository at this point in the history
  • Loading branch information
NeroBurner committed Sep 25, 2024
1 parent 3507ee5 commit 0e24e2b
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include <algorithm>
#include <array>
#include <vector>
#include <span>
#include <cmath> // std::pow

// additional includes for 'saveScreenshot()' function
Expand Down Expand Up @@ -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<uint8_t> 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<uint8_t> 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<uint8_t, 49> dataBuffer {};
std::span<uint8_t> data(dataBuffer);
os_mbuf buffer;
ble_gatt_access_ctxt ctxt;
ctxt.om = &buffer;
Expand All @@ -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<uint8_t>(iconId);

// send weather to SimpleWeatherService
Expand All @@ -853,6 +857,7 @@ class Framework {
Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days)
{
std::array<uint8_t, 36> dataBuffer {};
std::span<uint8_t> data(dataBuffer);
os_mbuf buffer;
ble_gatt_access_ctxt ctxt;
ctxt.om = &buffer;
Expand All @@ -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<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);
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<uint8_t>(day.iconId);
}
// send Forecast to SimpleWeatherService
Expand Down

0 comments on commit 0e24e2b

Please sign in to comment.