Skip to content

Commit f2814dd

Browse files
committed
Use RoundedDiv utility function across project
1 parent 52baa26 commit f2814dd

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/displayapp/DisplayApp.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@
5353
#include "displayapp/screens/settings/SettingBluetooth.h"
5454
#include "displayapp/screens/settings/SettingOTA.h"
5555

56+
#include "utility/Math.h"
57+
5658
#include "libs/lv_conf.h"
5759
#include "UserApps.h"
5860

5961
#include <algorithm>
62+
#include <limits>
6063

6164
using namespace Pinetime::Applications;
6265
using namespace Pinetime::Applications::Display;
@@ -167,18 +170,15 @@ TickType_t DisplayApp::CalculateSleepTime() {
167170
// Calculates how many system ticks DisplayApp should sleep before rendering the next AOD frame
168171
// Next frame time is frame count * refresh period (ms) * tick rate
169172

170-
auto RoundedDiv = [](uint32_t a, uint32_t b) {
171-
return ((a + (b / 2)) / b);
172-
};
173-
// RoundedDiv overflows when numerator + (denominator floordiv 2) > uint32 max
174-
// in this case around 9 hours (=overflow frame count / always on refresh period)
175-
constexpr TickType_t overflowFrameCount = (UINT32_MAX - (1000 / 16)) / ((configTICK_RATE_HZ / 8) * alwaysOnRefreshPeriod);
173+
// Avoid overflow when numerator would be > uint32 max
174+
// in this case around 18 hours (=overflow frame count / always on refresh period)
175+
constexpr TickType_t overflowFrameCount = std::numeric_limits<TickType_t>::max() / ((configTICK_RATE_HZ / 8) * alwaysOnRefreshPeriod);
176176

177177
TickType_t ticksElapsed = xTaskGetTickCount() - alwaysOnStartTime;
178178
// Divide both the numerator and denominator by 8 (=GCD(1000,1024))
179179
// to increase the number of ticks (frames) before the overflow tick is reached
180-
TickType_t targetRenderTick = RoundedDiv((configTICK_RATE_HZ / 8) * alwaysOnFrameCount * alwaysOnRefreshPeriod, 1000 / 8);
181-
180+
TickType_t targetRenderTick =
181+
Utility::RoundedDiv((configTICK_RATE_HZ / 8) * alwaysOnFrameCount * alwaysOnRefreshPeriod, static_cast<uint32_t>(1000 / 8));
182182
// Assumptions
183183

184184
// Tick rate is multiple of 8
@@ -188,7 +188,8 @@ TickType_t DisplayApp::CalculateSleepTime() {
188188
// Frame count must always wraparound more often than the system tick count does
189189
// Always on overflow time (ms) < system tick overflow time (ms)
190190
// Using 64bit ints here to avoid overflow
191-
static_assert((uint64_t) overflowFrameCount * (uint64_t) alwaysOnRefreshPeriod < (uint64_t) UINT32_MAX * 1000ULL / configTICK_RATE_HZ);
191+
static_assert((uint64_t) overflowFrameCount * (uint64_t) alwaysOnRefreshPeriod <
192+
(uint64_t) std::numeric_limits<TickType_t>::max() * 1000ULL / configTICK_RATE_HZ);
192193

193194
if (alwaysOnFrameCount == overflowFrameCount) {
194195
alwaysOnFrameCount = 0;

src/heartratetask/HeartRateTask.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
#include <components/heartrate/HeartRateController.h>
44
#include <limits>
55

6+
#include "utility/Math.h"
7+
68
using namespace Pinetime::Applications;
79

810
namespace {
911
constexpr TickType_t backgroundMeasurementTimeLimit = 30 * configTICK_RATE_HZ;
10-
11-
// dividend + (divisor / 2) must be less than the max T value
12-
template <std::unsigned_integral T>
13-
constexpr T RoundedDiv(T dividend, T divisor) {
14-
return (dividend + (divisor / static_cast<T>(2))) / divisor;
15-
}
1612
}
1713

1814
std::optional<TickType_t> HeartRateTask::BackgroundMeasurementInterval() const {
@@ -48,9 +44,9 @@ TickType_t HeartRateTask::CurrentTaskDelay() {
4844
static_cast<uint64_t>((Pinetime::Controllers::Ppg::deltaTms)) <
4945
std::numeric_limits<uint32_t>::max(),
5046
"Overflow");
51-
TickType_t elapsedTarget = RoundedDiv(static_cast<uint32_t>(configTICK_RATE_HZ / 2) * (static_cast<uint32_t>(count) + 1U) *
52-
static_cast<uint32_t>((Pinetime::Controllers::Ppg::deltaTms)),
53-
static_cast<uint32_t>(1000 / 2));
47+
TickType_t elapsedTarget = Utility::RoundedDiv(static_cast<uint32_t>(configTICK_RATE_HZ / 2) * (static_cast<uint32_t>(count) + 1U) *
48+
static_cast<uint32_t>((Pinetime::Controllers::Ppg::deltaTms)),
49+
static_cast<uint32_t>(1000 / 2));
5450

5551
// On count overflow, reset both count and start time
5652
// Count is 16bit to avoid overflow in elapsedTarget

0 commit comments

Comments
 (0)