2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/device/kobo/powerd.lua
Hzj_jie 0b29e73e2e BatteryStat plugin and instruction of KoboLight plugin (#2643)
* Start battery stat plugin

* BatteryStat & kobolight

* Several minor improvements

* Remove a useless function

* flush settings

* Some review feedbacks

* Resolve review comments

* Remaining Minutes -> Remaining Hours

* Add dump_file

* typo

* realpath

* realpath on folder

* Remove useless os.time()

* Resolve review comments

* warning

* Add BatteryStat.debugging flag

* treat log as txt

* Minor improvement

* Charging hour should be positive

* Use warn instead of info

* onSuspend in Kobo

* Charging events for kobo and kindle

* More events

* dumpOrLog

* Warnings

* Typo

* More space

* Singleton

* slightly format change

* BatteryStat singleton

* Init

* Remove debugging flag

* sleeping percentage is still negative

* Read settings

* Do not need to change was_suspending and was_charging

* Typo

* Remove debugging flag

* Not charging should happen before suspend

* Resolve review comments

* was_suspend and was_charging should be updated each time in onCallback()
2017-03-23 23:36:15 -07:00

104 lines
3.1 KiB
Lua

local BasePowerD = require("device/generic/powerd")
local NickelConf = require("device/kobo/nickel_conf")
local batt_state_folder =
"/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/"
local KoboPowerD = BasePowerD:new{
-- Do not actively set front light to 0, it may confuse users -- pressing
-- hardware button won't take any effect.
fl_min = 1, fl_max = 100,
fl_intensity = 20,
restore_settings = true,
fl = nil,
-- We will set this value for all kobo models. but it will only be synced
-- with nickel's FrontLightState config if the current nickel firmware
-- supports this config.
is_fl_on = false,
batt_capacity_file = batt_state_folder .. "capacity",
is_charging_file = batt_state_folder .. "status",
}
function KoboPowerD:init()
if self.device.hasFrontlight() then
local kobolight = require("ffi/kobolight")
local ok, light = pcall(kobolight.open)
if ok then
self.fl = light
if NickelConf.frontLightState.get() ~= nil then
self.has_fl_state_cfg = true
else
self.has_fl_state_cfg = false
end
end
end
end
function KoboPowerD:toggleFrontlight()
if self.fl ~= nil then
if self.is_fl_on then
self.fl:setBrightness(0)
else
self.fl:setBrightness(self.fl_intensity)
end
self.is_fl_on = not self.is_fl_on
if self.has_fl_state_cfg and KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then
NickelConf.frontLightState.set(self.is_fl_on)
end
end
end
function KoboPowerD:setIntensityHW()
if self.fl ~= nil then
self.fl:setBrightness(self.fl_intensity)
-- Make sure we persist intensity config in reader setting
G_reader_settings:saveSetting("frontlight_intensity", self.fl_intensity)
if KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then
NickelConf.frontLightLevel.set(self.fl_intensity)
end
-- also keep self.is_fl_on in sync with intensity if needed
local is_fl_on
if self.fl_intensity > 0 then
is_fl_on = true
else
is_fl_on = false
end
if self.is_fl_on ~= is_fl_on then
self.is_fl_on = is_fl_on
if self.has_fl_state_cfg and KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then
NickelConf.frontLightState.set(self.is_fl_on)
end
end
end
end
function KoboPowerD:getCapacityHW()
return self:read_int_file(self.batt_capacity_file)
end
function KoboPowerD:isChargingHW()
return self:read_str_file(self.is_charging_file) == "Charging\n"
end
-- Turn off front light before suspend.
function KoboPowerD:beforeSuspend()
if self.fl ~= nil then
self.fl:setBrightness(0)
end
end
-- Restore front light state after resume.
function KoboPowerD:afterResume()
if self.fl ~= nil then
if KOBO_LIGHT_ON_START and tonumber(KOBO_LIGHT_ON_START) > -1 then
self:setIntensity(math.min(KOBO_LIGHT_ON_START, 100))
elseif self.is_fl_on then
self.fl:setBrightness(self.fl_intensity)
end
end
end
return KoboPowerD