2018-10-31 22:48:36 +00:00
|
|
|
local BasePowerD = require("device/generic/powerd")
|
|
|
|
local SysfsLight = require ("device/sysfs_light")
|
|
|
|
|
|
|
|
local battery_sysfs = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/"
|
|
|
|
|
|
|
|
local CervantesPowerD = BasePowerD:new{
|
|
|
|
fl = nil,
|
|
|
|
|
|
|
|
fl_min = 0,
|
|
|
|
fl_max = 100,
|
2018-12-28 03:32:42 +00:00
|
|
|
fl_warmth_min = 0,
|
|
|
|
fl_warmth_max = 100,
|
2018-10-31 22:48:36 +00:00
|
|
|
capacity_file = battery_sysfs .. 'capacity',
|
|
|
|
status_file = battery_sysfs .. 'status'
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:56:18 +00:00
|
|
|
-- We can't read back the current state from the OS or hardware.
|
|
|
|
-- Use the last value stored in KOReader settings instead.
|
|
|
|
function CervantesPowerD:frontlightWarmthHW()
|
|
|
|
return G_reader_settings:readSetting("frontlight_warmth") or 0
|
|
|
|
end
|
|
|
|
|
2018-10-31 22:48:36 +00:00
|
|
|
function CervantesPowerD:_syncLightOnStart()
|
2022-03-14 18:56:18 +00:00
|
|
|
|
2018-10-31 22:48:36 +00:00
|
|
|
local new_intensity = G_reader_settings:readSetting("frontlight_intensity") or nil
|
|
|
|
local is_frontlight_on = G_reader_settings:readSetting("is_frontlight_on") or nil
|
|
|
|
|
|
|
|
if new_intensity ~= nil then
|
|
|
|
self.hw_intensity = new_intensity
|
|
|
|
end
|
|
|
|
|
|
|
|
if is_frontlight_on ~= nil then
|
|
|
|
self.initial_is_fl_on = is_frontlight_on
|
|
|
|
end
|
|
|
|
|
2022-03-14 18:56:18 +00:00
|
|
|
self.fl_warmth = self:frontlightWarmthHW()
|
2018-10-31 22:48:36 +00:00
|
|
|
|
|
|
|
if self.initial_is_fl_on == false and self.hw_intensity == 0 then
|
|
|
|
self.hw_intensity = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:init()
|
|
|
|
-- Default values in case self:_syncLightOnStart() does not find
|
|
|
|
-- any previously saved setting (and for unit tests where it will
|
|
|
|
-- not be called)
|
|
|
|
self.hw_intensity = 20
|
|
|
|
self.initial_is_fl_on = true
|
|
|
|
|
2019-04-08 21:05:08 +00:00
|
|
|
if self.device:hasFrontlight() then
|
|
|
|
if self.device:hasNaturalLight() then
|
2018-10-31 22:48:36 +00:00
|
|
|
local nl_config = G_reader_settings:readSetting("natural_light_config")
|
|
|
|
if nl_config then
|
2023-12-13 23:12:44 +00:00
|
|
|
for key, val in pairs(nl_config) do
|
2018-10-31 22:48:36 +00:00
|
|
|
self.device.frontlight_settings[key] = val
|
|
|
|
end
|
|
|
|
end
|
2023-12-13 23:12:44 +00:00
|
|
|
-- Does this device's NaturalLight use a custom scale?
|
|
|
|
self.fl_warmth_min = self.device.frontlight_settings.nl_min or self.fl_warmth_min
|
|
|
|
self.fl_warmth_max = self.device.frontlight_settings.nl_max or self.fl_warmth_max
|
2019-09-22 00:11:18 +00:00
|
|
|
-- If this device has a mixer, we can use the ioctl for brightness control, as it's much lower latency.
|
|
|
|
if self.device:hasNaturalLightMixer() then
|
|
|
|
local kobolight = require("ffi/kobolight")
|
|
|
|
local ok, light = pcall(kobolight.open)
|
|
|
|
if ok then
|
|
|
|
self.device.frontlight_settings.frontlight_ioctl = light
|
|
|
|
end
|
|
|
|
end
|
2018-10-31 22:48:36 +00:00
|
|
|
self.fl = SysfsLight:new(self.device.frontlight_settings)
|
|
|
|
self:_syncLightOnStart()
|
|
|
|
else
|
|
|
|
local kobolight = require("ffi/kobolight")
|
|
|
|
local ok, light = pcall(kobolight.open)
|
|
|
|
if ok then
|
|
|
|
self.fl = light
|
|
|
|
self:_syncLightOnStart()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:saveSettings()
|
2019-04-08 21:05:08 +00:00
|
|
|
if self.device:hasFrontlight() then
|
2018-10-31 22:48:36 +00:00
|
|
|
-- Store BasePowerD values into settings (and not our hw_intensity, so
|
|
|
|
-- that if frontlight was toggled off, we save and restore the previous
|
|
|
|
-- untoggled intensity and toggle state at next startup)
|
|
|
|
local cur_intensity = self.fl_intensity
|
|
|
|
local cur_is_fl_on = self.is_fl_on
|
|
|
|
local cur_warmth = self.fl_warmth
|
|
|
|
-- Save intensity to koreader settings
|
|
|
|
G_reader_settings:saveSetting("frontlight_intensity", cur_intensity)
|
|
|
|
G_reader_settings:saveSetting("is_frontlight_on", cur_is_fl_on)
|
|
|
|
if cur_warmth ~= nil then
|
|
|
|
G_reader_settings:saveSetting("frontlight_warmth", cur_warmth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:frontlightIntensityHW()
|
|
|
|
return self.hw_intensity
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:isFrontlightOnHW()
|
|
|
|
if self.initial_is_fl_on ~= nil then -- happens only once after init()
|
|
|
|
-- give initial state to BasePowerD, which will
|
|
|
|
-- reset our self.hw_intensity to 0 if self.initial_is_fl_on is false
|
|
|
|
local ret = self.initial_is_fl_on
|
|
|
|
self.initial_is_fl_on = nil
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
return self.hw_intensity > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:setIntensityHW(intensity)
|
|
|
|
if self.fl == nil then return end
|
[Kobo/Cervantes] Always use setBrightness to set the brightness (#5429)
sysfs_light handles every case sanely already, no need to be cute ;).
This prevents always setting the warmth when setting the FL on NL
devices with a mixer, because, besides being useless, it's also slow
(lua does mmap'ed io, so every handle requires an fstat + mmap).
It was subtly affecting the fluidity of the toggle ramp, and much less
subtly wreaking havoc on #5373 ;).
KA1 owners might want to investigate keeping the fds around one way or
another, like we do for the fl ioctl, to alleviate the overhead of doing
that for not one but three or four fds...
I won't be doing that, since I lack the hardware to test it, and FL+NL
stuff is pain.
* Do the same on Cervantes
* Bump base to pickup related ioctl tweaks
2019-09-26 21:59:31 +00:00
|
|
|
self.fl:setBrightness(intensity)
|
2018-10-31 22:48:36 +00:00
|
|
|
self.hw_intensity = intensity
|
|
|
|
-- Now that we have set intensity, we need to let BasePowerD
|
|
|
|
-- know about possibly changed frontlight state (if we came
|
|
|
|
-- from light toggled off to some intensity > 0).
|
|
|
|
self:_decideFrontlightState()
|
|
|
|
end
|
|
|
|
|
2022-03-14 18:56:18 +00:00
|
|
|
function CervantesPowerD:setWarmthHW(warmth)
|
2021-09-02 20:44:22 +00:00
|
|
|
if self.fl == nil then return end
|
2022-03-14 18:56:18 +00:00
|
|
|
self.fl:setWarmth(warmth)
|
2018-10-31 22:48:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:getCapacityHW()
|
|
|
|
return self:read_int_file(self.capacity_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:isChargingHW()
|
2021-01-16 03:41:46 +00:00
|
|
|
return self:read_str_file(self.status_file) == "Charging"
|
2018-10-31 22:48:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:beforeSuspend()
|
2023-05-18 21:13:43 +00:00
|
|
|
-- Inhibit user input and emit the Suspend event.
|
|
|
|
self.device:_beforeSuspend()
|
|
|
|
|
|
|
|
if self.fl then
|
|
|
|
-- just turn off frontlight without remembering its state
|
|
|
|
self.fl:setBrightness(0)
|
|
|
|
end
|
2018-10-31 22:48:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function CervantesPowerD:afterResume()
|
2023-05-18 21:13:43 +00:00
|
|
|
if self.fl then
|
|
|
|
-- just re-set it to self.hw_intensity that we haven't changed on Suspend
|
|
|
|
if not self.device:hasNaturalLight() then
|
|
|
|
self.fl:setBrightness(self.hw_intensity)
|
|
|
|
else
|
|
|
|
self.fl:setNaturalBrightness(self.hw_intensity, self.fl_warmth)
|
|
|
|
end
|
2018-10-31 22:48:36 +00:00
|
|
|
end
|
2023-05-18 21:13:43 +00:00
|
|
|
|
|
|
|
self:invalidateCapacityCache()
|
|
|
|
|
|
|
|
-- Restore user input and emit the Resume event.
|
|
|
|
self.device:_afterResume()
|
2018-10-31 22:48:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return CervantesPowerD
|