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
6164using namespace Pinetime ::Applications;
6265using 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 ;
0 commit comments