Skip to content

Commit

Permalink
utility: Add function for calculating a rounded division
Browse files Browse the repository at this point in the history
Make weather service use it.
  • Loading branch information
FintasticMan committed Dec 10, 2024
1 parent 5705fad commit 4a13235
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#undef min

#include "components/datetime/DateTimeController.h"
#include "utility/Math.h"

int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg);

Expand Down Expand Up @@ -75,13 +76,11 @@ namespace Pinetime {
}

[[nodiscard]] int16_t Celsius() const {
int16_t temp = PreciseCelsius();
return (temp + (temp >= 0 ? 50 : -50)) / 100;
return Utility::RoundedDiv(PreciseCelsius(), 100u);
}

[[nodiscard]] int16_t Fahrenheit() const {
int16_t temp = PreciseFahrenheit();
return (temp + (temp >= 0 ? 50 : -50)) / 100;
return Utility::RoundedDiv(PreciseFahrenheit(), 100u);
}

bool operator==(const Temperature& other) const {
Expand Down
5 changes: 5 additions & 0 deletions src/utility/Math.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#pragma once

#include <cstdint>
#include <concepts>

namespace Pinetime {
namespace Utility {
// returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90
int16_t Asin(int16_t arg);

static constexpr auto RoundedDiv(std::integral auto dividend, std::unsigned_integral auto divisor) -> decltype(dividend / divisor) {
return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor;
}
}
}

0 comments on commit 4a13235

Please sign in to comment.