2018-10-27 20:49:18 +00:00
|
|
|
-- Generic frontlight SysFS interface.
|
2018-02-04 11:24:24 +00:00
|
|
|
-- This also supports the natural light, which consists of additional
|
|
|
|
-- red and green light LEDs.
|
|
|
|
|
|
|
|
local dbg = require("dbg")
|
2023-05-03 14:26:35 +00:00
|
|
|
local util = require("util")
|
2022-10-13 23:50:03 +00:00
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
local SysfsLight = {
|
|
|
|
frontlight_white = nil,
|
|
|
|
frontlight_red = nil,
|
|
|
|
frontlight_green = nil,
|
2018-12-28 03:32:42 +00:00
|
|
|
frontlight_mixer = nil,
|
2019-09-22 00:11:18 +00:00
|
|
|
frontlight_ioctl = nil,
|
2018-12-28 03:32:42 +00:00
|
|
|
nl_min = nil,
|
|
|
|
nl_max = nil,
|
|
|
|
nl_inverted = nil,
|
2018-02-04 11:24:24 +00:00
|
|
|
current_brightness = 0,
|
|
|
|
current_warmth = 0,
|
|
|
|
white_gain = 25,
|
|
|
|
red_gain = 24,
|
|
|
|
green_gain = 24,
|
|
|
|
white_offset = -25,
|
|
|
|
red_offset = 0,
|
|
|
|
green_offset = -65,
|
2018-03-09 22:15:32 +00:00
|
|
|
exponent = 0.25,
|
2018-02-04 11:24:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
function SysfsLight:new(o)
|
2018-03-02 22:03:00 +00:00
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
if o.init then o:init() end
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
function SysfsLight:setBrightness(brightness)
|
2019-04-08 21:05:08 +00:00
|
|
|
self:setNaturalBrightness(brightness, nil)
|
2018-02-04 11:24:24 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
dbg:guard(SysfsLight, 'setBrightness',
|
2018-02-04 11:24:24 +00:00
|
|
|
function(self, brightness)
|
|
|
|
assert(brightness >= 0 and brightness <= 100,
|
|
|
|
"Wrong brightness value given!")
|
|
|
|
end)
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
function SysfsLight:setWarmth(warmth)
|
2019-04-08 21:05:08 +00:00
|
|
|
self:setNaturalBrightness(nil, warmth)
|
2018-02-04 11:24:24 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
dbg:guard(SysfsLight, 'setWarmth',
|
2018-02-04 11:24:24 +00:00
|
|
|
function(self, warmth)
|
|
|
|
assert(warmth >= 0 and warmth <= 100,
|
|
|
|
"Wrong warmth value given!")
|
|
|
|
end)
|
|
|
|
|
2022-03-14 18:56:18 +00:00
|
|
|
--- @note: warmth is already in the *native* scale!
|
2018-10-27 20:49:18 +00:00
|
|
|
function SysfsLight:setNaturalBrightness(brightness, warmth)
|
2019-04-08 21:05:08 +00:00
|
|
|
local set_brightness = true
|
|
|
|
local set_warmth = true
|
2018-03-11 16:35:09 +00:00
|
|
|
if not brightness then
|
2019-04-08 21:05:08 +00:00
|
|
|
set_brightness = false
|
2018-03-11 16:35:09 +00:00
|
|
|
brightness = self.current_brightness
|
|
|
|
end
|
|
|
|
if not warmth then
|
2019-04-08 21:05:08 +00:00
|
|
|
set_warmth = false
|
2018-03-11 16:35:09 +00:00
|
|
|
warmth = self.current_warmth
|
|
|
|
end
|
|
|
|
|
2018-12-28 03:32:42 +00:00
|
|
|
-- Newer devices use a mixer instead of writting values per color.
|
|
|
|
if self.frontlight_mixer then
|
2019-04-08 21:05:08 +00:00
|
|
|
if set_brightness then
|
2019-09-22 00:11:18 +00:00
|
|
|
-- Prefer the ioctl, as it's much lower latency.
|
|
|
|
if self.frontlight_ioctl then
|
|
|
|
self.frontlight_ioctl:setBrightness(brightness)
|
|
|
|
else
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(brightness, self.frontlight_white)
|
2019-09-22 00:11:18 +00:00
|
|
|
end
|
2019-04-08 21:05:08 +00:00
|
|
|
end
|
2022-03-14 18:56:18 +00:00
|
|
|
-- The mixer might be using inverted values... (cold is nl_max, warm is nl_min)
|
2019-04-08 21:05:08 +00:00
|
|
|
if set_warmth then
|
|
|
|
if self.nl_inverted then
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(self.nl_max - warmth, self.frontlight_mixer)
|
2019-04-08 21:05:08 +00:00
|
|
|
else
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(warmth, self.frontlight_mixer)
|
2019-04-08 21:05:08 +00:00
|
|
|
end
|
2018-12-28 03:32:42 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
local red = 0
|
|
|
|
local green = 0
|
|
|
|
local white = 0
|
|
|
|
if brightness > 0 then
|
|
|
|
-- On Nickel, the values for white/red/green are roughly linearly dependent
|
|
|
|
-- on the 4th root of brightness and warmth.
|
2022-09-27 23:11:34 +00:00
|
|
|
white = math.min(self.white_gain * (brightness * (100 - warmth))^self.exponent + self.white_offset, 255)
|
2018-12-28 03:32:42 +00:00
|
|
|
end
|
|
|
|
if warmth > 0 then
|
2022-09-27 23:11:34 +00:00
|
|
|
local brightness_warmth_exp = (brightness * warmth)^self.exponent
|
|
|
|
red = math.min(self.red_gain * brightness_warmth_exp + self.red_offset, 255)
|
|
|
|
green = math.min(self.green_gain * brightness_warmth_exp + self.green_offset, 255)
|
2018-12-28 03:32:42 +00:00
|
|
|
end
|
2018-02-04 11:24:24 +00:00
|
|
|
|
2018-12-28 03:32:42 +00:00
|
|
|
white = math.max(white, 0)
|
|
|
|
red = math.max(red, 0)
|
|
|
|
green = math.max(green, 0)
|
2018-02-04 11:24:24 +00:00
|
|
|
|
2018-12-28 03:32:42 +00:00
|
|
|
self:_set_light_value(self.frontlight_white, math.floor(white))
|
|
|
|
self:_set_light_value(self.frontlight_green, math.floor(green))
|
|
|
|
self:_set_light_value(self.frontlight_red, math.floor(red))
|
|
|
|
end
|
2018-02-04 11:24:24 +00:00
|
|
|
|
|
|
|
self.current_brightness = brightness
|
|
|
|
self.current_warmth = warmth
|
|
|
|
end
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
dbg:guard(SysfsLight, 'setNaturalBrightness',
|
2018-02-04 11:24:24 +00:00
|
|
|
function(self, brightness, warmth)
|
Enable HW dithering in a few key places (#4541)
* Enable HW dithering on supported devices (Clara HD, Forma; Oasis 2, PW4)
* FileManager and co. (where appropriate, i.e., when covers are shown)
* Book Status
* Reader, where appropriate:
* CRe: on pages whith image content (for over 7.5% of the screen area, should hopefully leave stuff like bullet points or small scene breaks alone).
* Other engines: on user-request (in the gear tab of the bottom menu), via the new "Dithering" knob (will only appear on supported devices).
* ScreenSaver
* ImageViewer
* Minimize repaints when flash_ui is enabled (by, almost everywhere, only repainting the flashing element, and not the toplevel window which hosts it).
(The first pass of this involved fixing a few Button instances whose show_parent was wrong, in particular, chevrons in the FM & TopMenu).
* Hunted down a few redundant repaints (unneeded setDirty("all") calls),
either by switching the widget to nil when only a refresh was needed, and not a repaint,
or by passing the appropritate widget to setDirty.
(Note to self: Enable *verbose* debugging to catch broken setDirty calls via its post guard).
There were also a few instances of 'em right behind a widget close.
* Don't repaint the underlying widget when initially showing TopMenu & ConfigDialog.
We unfortunately do need to do it when switching tabs, because of their variable heights.
* On Kobo, disabled the extra and completely useless full refresh before suspend/reboot/poweroff, as well as on resume. No more double refreshes!
* Fix another debug guard in Kobo sysfs_light
* Switch ImageWidget & ImageViewer mostly to "ui" updates, which will be better suited to image content pretty much everywhere, REAGL or not.
PS: (Almost :100: commits! :D)
2019-02-07 00:14:37 +00:00
|
|
|
assert(brightness == nil or (brightness >= 0 and brightness <= 100),
|
2018-02-04 11:24:24 +00:00
|
|
|
"Wrong brightness value given!")
|
Enable HW dithering in a few key places (#4541)
* Enable HW dithering on supported devices (Clara HD, Forma; Oasis 2, PW4)
* FileManager and co. (where appropriate, i.e., when covers are shown)
* Book Status
* Reader, where appropriate:
* CRe: on pages whith image content (for over 7.5% of the screen area, should hopefully leave stuff like bullet points or small scene breaks alone).
* Other engines: on user-request (in the gear tab of the bottom menu), via the new "Dithering" knob (will only appear on supported devices).
* ScreenSaver
* ImageViewer
* Minimize repaints when flash_ui is enabled (by, almost everywhere, only repainting the flashing element, and not the toplevel window which hosts it).
(The first pass of this involved fixing a few Button instances whose show_parent was wrong, in particular, chevrons in the FM & TopMenu).
* Hunted down a few redundant repaints (unneeded setDirty("all") calls),
either by switching the widget to nil when only a refresh was needed, and not a repaint,
or by passing the appropritate widget to setDirty.
(Note to self: Enable *verbose* debugging to catch broken setDirty calls via its post guard).
There were also a few instances of 'em right behind a widget close.
* Don't repaint the underlying widget when initially showing TopMenu & ConfigDialog.
We unfortunately do need to do it when switching tabs, because of their variable heights.
* On Kobo, disabled the extra and completely useless full refresh before suspend/reboot/poweroff, as well as on resume. No more double refreshes!
* Fix another debug guard in Kobo sysfs_light
* Switch ImageWidget & ImageViewer mostly to "ui" updates, which will be better suited to image content pretty much everywhere, REAGL or not.
PS: (Almost :100: commits! :D)
2019-02-07 00:14:37 +00:00
|
|
|
assert(warmth == nil or (warmth >= 0 and warmth <= 100),
|
2018-02-04 11:24:24 +00:00
|
|
|
"Wrong warmth value given!")
|
|
|
|
end)
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
function SysfsLight:_set_light_value(sysfs_directory, value)
|
|
|
|
if not sysfs_directory then return end
|
2018-02-04 11:24:24 +00:00
|
|
|
-- bl_power is '31' when the light is turned on, '0' otherwise.
|
|
|
|
if (value > 0) then
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(31, sysfs_directory .. "/bl_power")
|
2018-02-04 11:24:24 +00:00
|
|
|
else
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(0, sysfs_directory .. "/bl_power")
|
2018-02-04 11:24:24 +00:00
|
|
|
end
|
2023-05-03 14:26:35 +00:00
|
|
|
util.writeToSysfs(value, sysfs_directory .. "/brightness")
|
2018-02-04 11:24:24 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 20:49:18 +00:00
|
|
|
return SysfsLight
|