frontlight on kobo: a few fixes (#3163)

* frontlight on kobo: a few fixes

Rewritten to not update NickelConf on every change, and never
if KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = false.
Reintroduce global settings frontlight_intensity and
is_frontlight_on to keep level and state across koreader
sessions.
Fix a few of the remaining issues on kobo light.
Ensure settings are saved when rebooting/powering off from
File browser.

* Ensure untoggle works when starting with light off
pull/3173/head
poire-z 7 years ago committed by Frans de Jonge
parent dbaea8c1c8
commit d4fd0b9428

@ -170,9 +170,10 @@ SEARCH_SERIES = true
SEARCH_PATH = true SEARCH_PATH = true
-- Light parameter for Kobo -- Light parameter for Kobo
KOBO_LIGHT_ON_START = -2 -- -1, -2 or 0-100. -1 leaves light as it KOBO_LIGHT_ON_START = -2 -- -1, -2 or 0-100.
-- is, -2 uses 'Kobo eReader.conf', other -- -1 uses previous koreader session saved brightness
-- sets light on start/wake up -- -2 uses 'Kobo eReader.conf' brighness,
-- other sets light on start to a fix brighness
KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = true -- Save brightness set in KOreader KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = true -- Save brightness set in KOreader
-- with nickel's 'Kobo eReader.conf' -- with nickel's 'Kobo eReader.conf'

@ -362,6 +362,7 @@ end
function FileManager:onClose() function FileManager:onClose()
logger.dbg("close filemanager") logger.dbg("close filemanager")
G_reader_settings:flush()
UIManager:close(self) UIManager:close(self)
if self.onExit then if self.onExit then
self:onExit() self:onExit()

@ -166,6 +166,9 @@ function Device:initNetworkManager() end
function Device:supportsScreensaver() return false end function Device:supportsScreensaver() return false end
-- Device specific method if any setting needs being saved
function Device:saveSettings() end
--[[ --[[
prepare for application shutdown prepare for application shutdown
--]] --]]

@ -491,6 +491,11 @@ function Kobo:resume()
end end
end end
function Kobo:saveSettings()
-- save frontlight state to G_reader_settings (and NickelConf if needed)
self.powerd:saveSettings()
end
function Kobo:powerOff() function Kobo:powerOff()
os.execute("poweroff") os.execute("poweroff")
end end

@ -4,6 +4,11 @@ local NickelConf = require("device/kobo/nickel_conf")
local batt_state_folder = local batt_state_folder =
"/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/" "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/"
-- Here, we only deal with the real hw intensity.
-- Dealing with toggling and remembering/restoring
-- previous intensity when toggling/untoggling is done
-- by BasePowerD.
local KoboPowerD = BasePowerD:new{ local KoboPowerD = BasePowerD:new{
fl_min = 0, fl_max = 100, fl_min = 0, fl_max = 100,
fl = nil, fl = nil,
@ -14,28 +19,63 @@ local KoboPowerD = BasePowerD:new{
-- TODO: Remove KOBO_LIGHT_ON_START -- TODO: Remove KOBO_LIGHT_ON_START
function KoboPowerD:_syncKoboLightOnStart() function KoboPowerD:_syncKoboLightOnStart()
local new_intensity = nil
local is_frontlight_on = nil
local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START) local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START)
if kobo_light_on_start then if kobo_light_on_start then
local new_intensity
local is_frontlight_on
if kobo_light_on_start > 0 then if kobo_light_on_start > 0 then
new_intensity = math.min(kobo_light_on_start, 100) new_intensity = math.min(kobo_light_on_start, 100)
is_frontlight_on = true is_frontlight_on = true
elseif kobo_light_on_start == 0 then elseif kobo_light_on_start == 0 then
new_intensity = 0 new_intensity = 0
is_frontlight_on = false is_frontlight_on = false
elseif kobo_light_on_start == -2 then elseif kobo_light_on_start == -2 then -- get values from NickelConf
return new_intensity = NickelConf.frontLightLevel.get()
is_frontlight_on = NickelConf.frontLightState:get()
if is_frontlight_on == nil then
-- this device does not support frontlight toggle,
-- we set the value based on frontlight intensity.
if new_intensity > 0 then
is_frontlight_on = true
else
is_frontlight_on = false
end
end
if is_frontlight_on == false and new_intensity == 0 then
-- frontlight was toggled off in nickel, and we have no
-- level-before-toggle-off (firmware without "FrontLightState"):
-- use the one from koreader settings
new_intensity = G_reader_settings:readSetting("frontlight_intensity")
end
else -- if kobo_light_on_start == -1 or other unexpected value then else -- if kobo_light_on_start == -1 or other unexpected value then
-- TODO(Hzj-jie): Read current frontlight states from OS. -- As we can't read value from the OS or hardware, use last values
return -- stored in koreader settings
new_intensity = G_reader_settings:readSetting("frontlight_intensity")
is_frontlight_on = G_reader_settings:readSetting("is_frontlight_on")
end end
NickelConf.frontLightLevel.set(new_intensity) end
NickelConf.frontLightState.set(is_frontlight_on)
if new_intensity ~= nil then
self.hw_intensity = new_intensity
end
if is_frontlight_on ~= nil then
-- will only be used to give initial state to BasePowerD:_decideFrontlightState()
self.initial_is_fl_on = is_frontlight_on
end
-- In any case frontlight is off, ensure intensity is non-zero so untoggle works
if self.initial_is_fl_on == false and self.hw_intensity == 0 then
self.hw_intensity = 1
end end
end end
function KoboPowerD:init() function KoboPowerD:init()
-- Default values in case self:_syncKoboLightOnStart() 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
if self.device.hasFrontlight() then if self.device.hasFrontlight() then
local kobolight = require("ffi/kobolight") local kobolight = require("ffi/kobolight")
local ok, light = pcall(kobolight.open) local ok, light = pcall(kobolight.open)
@ -46,42 +86,59 @@ function KoboPowerD:init()
end end
end end
function KoboPowerD:_syncIntensity(intensity) function KoboPowerD:saveSettings()
if NickelConf.frontLightLevel.get() ~= intensity then -- Store BasePowerD values into settings (and not our hw_intensity, so
NickelConf.frontLightLevel.set(intensity) -- that if frontlight was toggled off, we save and restore the previous
end -- untoggled intensity and toggle state at next startup)
end local cur_intensity = self.fl_intensity
local cur_is_fl_on = self.is_fl_on
function KoboPowerD:_syncNickelConf() -- Save intensity to koreader settings
if not KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then return end G_reader_settings:saveSetting("frontlight_intensity", cur_intensity)
if NickelConf.frontLightState.get() == nil then G_reader_settings:saveSetting("is_frontlight_on", cur_is_fl_on)
self:_syncIntensity(self:frontlightIntensity()) -- And to "Kobo eReader.conf" if needed
else if KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then
if NickelConf.frontLightState.get() ~= self:isFrontlightOn() then if NickelConf.frontLightState.get() ~= nil then
NickelConf.frontLightState.set(self:isFrontlightOn()) if NickelConf.frontLightState.get() ~= cur_is_fl_on then
NickelConf.frontLightState.set(cur_is_fl_on)
end
else -- no support for frontlight state
if not cur_is_fl_on then -- if toggled off, save intensity as 0
cur_intensity = self.fl_min
end
end
if NickelConf.frontLightLevel.get() ~= cur_intensity then
NickelConf.frontLightLevel.set(cur_intensity)
end end
self:_syncIntensity(self.fl_intensity)
end end
end end
function KoboPowerD:frontlightIntensityHW() function KoboPowerD:frontlightIntensityHW()
return NickelConf.frontLightLevel.get() return self.hw_intensity
end end
function KoboPowerD:isFrontlightOnHW() function KoboPowerD:isFrontlightOnHW()
local result = NickelConf.frontLightState.get() if self.initial_is_fl_on ~= nil then -- happens only once after init()
if result == nil then -- give initial state to BasePowerD, which will
return self.fl_intensity > 0 -- 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 end
return result return self.hw_intensity > 0
end end
function KoboPowerD:turnOffFrontlightHW() self:_setIntensity(0) end function KoboPowerD:turnOffFrontlightHW()
self:_setIntensity(0) -- will call setIntensityHW(0)
end
function KoboPowerD:setIntensityHW(intensity) function KoboPowerD:setIntensityHW(intensity)
if self.fl == nil then return end if self.fl == nil then return end
self.fl:setBrightness(intensity) self.fl:setBrightness(intensity)
self:_syncNickelConf() 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 end
function KoboPowerD:getCapacityHW() function KoboPowerD:getCapacityHW()
@ -94,16 +151,16 @@ end
-- Turn off front light before suspend. -- Turn off front light before suspend.
function KoboPowerD:beforeSuspend() function KoboPowerD:beforeSuspend()
self:turnOffFrontlightHW() if self.fl == nil then return end
-- just turn off frontlight without remembering its state
self.fl:setBrightness(0)
end end
-- Restore front light state after resume. -- Restore front light state after resume.
function KoboPowerD:afterResume() function KoboPowerD:afterResume()
if KOBO_LIGHT_ON_START and tonumber(KOBO_LIGHT_ON_START) > -1 then if self.fl == nil then return end
self:setIntensity(math.min(KOBO_LIGHT_ON_START, 100)) -- just re-set it to self.hw_intensity that we haven't change on Suspend
elseif self:isFrontlightOn() then self.fl:setBrightness(self.hw_intensity)
self:turnOnFrontlightHW()
end
end end
return KoboPowerD return KoboPowerD

@ -55,6 +55,7 @@ function UIManager:init()
require("ui/screensaver"):show("poweroff", _("Powered off")) require("ui/screensaver"):show("poweroff", _("Powered off"))
Screen:refreshFull() Screen:refreshFull()
UIManager:nextTick(function() UIManager:nextTick(function()
Device:saveSettings()
self:broadcastEvent(Event:new("Close")) self:broadcastEvent(Event:new("Close"))
Device:powerOff() Device:powerOff()
end) end)
@ -65,6 +66,7 @@ function UIManager:init()
require("ui/screensaver"):show("reboot", _("Rebooting...")) require("ui/screensaver"):show("reboot", _("Rebooting..."))
Screen:refreshFull() Screen:refreshFull()
UIManager:nextTick(function() UIManager:nextTick(function()
Device:saveSettings()
self:broadcastEvent(Event:new("Close")) self:broadcastEvent(Event:new("Close"))
Device:reboot() Device:reboot()
end) end)

@ -190,6 +190,9 @@ local function exitReader()
local ReaderActivityIndicator = local ReaderActivityIndicator =
require("apps/reader/modules/readeractivityindicator") require("apps/reader/modules/readeractivityindicator")
-- Save any device settings before closing G_reader_settings
Device:saveSettings()
G_reader_settings:close() G_reader_settings:close()
-- Close lipc handles -- Close lipc handles

Loading…
Cancel
Save