weather: Switch to std::optional for Forecast days

Also only iterate over the number of days actually in use, rather than
MaxNbForecastDays.
This commit is contained in:
FintasticMan 2024-10-09 12:04:53 +02:00 committed by JF
parent e247bd7019
commit f7c87a700d
3 changed files with 14 additions and 14 deletions

View File

@ -52,7 +52,7 @@ namespace {
SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2])); auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2]));
std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days; std::array<std::optional<SimpleWeatherService::Forecast::Day>, SimpleWeatherService::MaxNbForecastDays> days;
const uint8_t nbDaysInBuffer = dataBuffer[10]; const uint8_t nbDaysInBuffer = dataBuffer[10];
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer); const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
for (int i = 0; i < nbDays; i++) { for (int i = 0; i < nbDays; i++) {
@ -112,9 +112,9 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d", NRF_LOG_INFO("\t[%d] Min: %d - Max : %d - Icon : %d",
i, i,
forecast->days[i].minTemperature.PreciseCelsius(), forecast->days[i]->minTemperature.PreciseCelsius(),
forecast->days[i].maxTemperature.PreciseCelsius(), forecast->days[i]->maxTemperature.PreciseCelsius(),
forecast->days[i].iconId); forecast->days[i]->iconId);
} }
} }
break; break;

View File

@ -63,7 +63,7 @@ namespace Pinetime {
class Temperature { class Temperature {
public: public:
explicit Temperature(int16_t raw = 0) : raw {raw} { explicit Temperature(int16_t raw) : raw {raw} {
} }
[[nodiscard]] int16_t PreciseCelsius() const { [[nodiscard]] int16_t PreciseCelsius() const {
@ -129,7 +129,7 @@ namespace Pinetime {
bool operator==(const Day& other) const; bool operator==(const Day& other) const;
}; };
std::array<Day, MaxNbForecastDays> days; std::array<std::optional<Day>, MaxNbForecastDays> days;
bool operator==(const Forecast& other) const; bool operator==(const Forecast& other) const;
}; };

View File

@ -153,22 +153,22 @@ void Weather::Refresh() {
if (optCurrentForecast) { if (optCurrentForecast) {
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp)); std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) { for (int i = 0; i < optCurrentForecast->nbDays; i++) {
int16_t minTemp = optCurrentForecast->days[i].maxTemperature.Celsius(); int16_t minTemp = optCurrentForecast->days[i]->maxTemperature.Celsius();
int16_t maxTemp = optCurrentForecast->days[i].minTemperature.Celsius(); int16_t maxTemp = optCurrentForecast->days[i]->minTemperature.Celsius();
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) { if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
minTemp = optCurrentForecast->days[i].maxTemperature.Fahrenheit(); minTemp = optCurrentForecast->days[i]->maxTemperature.Fahrenheit();
maxTemp = optCurrentForecast->days[i].minTemperature.Fahrenheit(); maxTemp = optCurrentForecast->days[i]->minTemperature.Fahrenheit();
} }
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i].maxTemperature)); lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i]->maxTemperature));
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i].minTemperature)); lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i]->minTemperature));
uint8_t wday = localTime.tm_wday + i + 1; uint8_t wday = localTime.tm_wday + i + 1;
if (wday > 7) { if (wday > 7) {
wday -= 7; wday -= 7;
} }
const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday)); const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday));
lv_table_set_cell_value(forecast, 0, i, dayOfWeek); lv_table_set_cell_value(forecast, 0, i, dayOfWeek);
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId)); lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i]->iconId));
// Pad cells based on the largest number of digits on each column // Pad cells based on the largest number of digits on each column
char maxPadding[3] = " "; char maxPadding[3] = " ";
char minPadding[3] = " "; char minPadding[3] = " ";