From 854cbe9f94267cabbd4d0d4a20e124040a50ce43 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sun, 23 Oct 2022 21:55:13 +0200 Subject: [PATCH] Kobo: Unbreak frontlight toggle for some specific values Because of floating point computery math stuff. Regression since #9609 c.f., https://github.com/koreader/koreader/pull/9609#issuecomment-1288187080 --- frontend/device/kobo/powerd.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/frontend/device/kobo/powerd.lua b/frontend/device/kobo/powerd.lua index 1e0e15283..582a82c9b 100644 --- a/frontend/device/kobo/powerd.lua +++ b/frontend/device/kobo/powerd.lua @@ -302,8 +302,12 @@ function KoboPowerD:turnOffFrontlightHW() return end ffiUtil.runInSubProcess(function() - for i = 1,5 do - self:setIntensityHW(math.floor(self.fl_intensity - ((self.fl_intensity * (1/5)) * i))) + for i = 1, 5 do + -- NOTE: Do *not* switch to (self.fl_intensity * (1/5) * i) here, it may lead to rounding errors, + -- which is problematic paired w/ math.floor because it doesn't round towards zero, + -- which means we may end up passing -1 to setIntensityHW, which will fail, + -- because we're bypassing the clamping usually done by setIntensity... + self:setIntensityHW(math.floor(self.fl_intensity - (self.fl_intensity / 5 * i))) --- @note: Newer devices appear to block slightly longer on FL ioctls/sysfs, so only sleep on older devices, --- otherwise we get a jump and not a ramp ;). if not self.device:hasNaturalLight() then @@ -335,8 +339,8 @@ function KoboPowerD:turnOnFrontlightHW() return end ffiUtil.runInSubProcess(function() - for i = 1,5 do - self:setIntensityHW(math.ceil(self.fl_min + ((self.fl_intensity * (1/5)) * i))) + for i = 1, 5 do + self:setIntensityHW(math.ceil(self.fl_min + (self.fl_intensity / 5 * i))) --- @note: Newer devices appear to block slightly longer on FL ioctls/sysfs, so only sleep on older devices, --- otherwise we get a jump and not a ramp ;). if not self.device:hasNaturalLight() then