2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/device/kindle/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

107 lines
3.1 KiB
Lua

local BasePowerD = require("device/generic/powerd")
-- liblipclua, see require below
local KindlePowerD = BasePowerD:new{
fl_min = 0, fl_max = 24,
fl_intensity = nil,
lipc_handle = nil,
is_fl_on = false,
}
function KindlePowerD:init()
local haslipc, lipc = pcall(require, "liblipclua")
if haslipc and lipc then
self.lipc_handle = lipc.init("com.github.koreader.kindlepowerd")
end
if self.device.hasFrontlight() then
-- Kindle stock software does not use intensity file directly, so we need to read from its
-- lipc property first.
if self.lipc_handle ~= nil then
self.fl_intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
else
self.fl_intensity = self:_readFLIntensity()
end
self:_set_fl_on()
end
end
function KindlePowerD:toggleFrontlight()
if not self.device.hasFrontlight() then
return
end
if self:_readFLIntensity() == 0 then
self:setIntensityHW()
else
self:_turnOffFL()
end
end
function KindlePowerD:setIntensityHW()
if self.lipc_handle ~= nil and self.fl_intensity > 0 then
-- NOTE: We want to bypass setIntensity's shenanigans and simply restore the light as-is
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", self.fl_intensity)
else
-- NOTE: when fl_intensity is 0, We want to really kill the light, so do it manually
-- (asking lipc to set it to 0 would in fact set it to 1)...
os.execute("echo -n ".. self.fl_intensity .." > " .. self.fl_intensity_file)
end
self:_set_fl_on()
end
function KindlePowerD:getCapacityHW()
if self.lipc_handle ~= nil then
return self.lipc_handle:get_int_property("com.lab126.powerd", "battLevel")
else
return self:read_int_file(self.batt_capacity_file)
end
end
function KindlePowerD:isChargingHW()
local is_charging
if self.lipc_handle ~= nil then
is_charging = self.lipc_handle:get_int_property("com.lab126.powerd", "isCharging")
else
is_charging = self:read_int_file(self.is_charging_file)
end
return is_charging == 1
end
function KindlePowerD:__gc()
if self.lipc_handle then
self.lipc_handle:close()
self.lipc_handle = nil
end
end
function KindlePowerD:_turnOffFL()
-- NOTE: We want to really kill the light, so do it manually (asking lipc to set it to 0 would in fact set it to 1)...
os.execute("echo -n 0 > " .. self.fl_intensity_file)
self.is_fl_on = false
end
function KindlePowerD:_readFLIntensity()
return self:read_int_file(self.fl_intensity_file)
end
function KindlePowerD:_set_fl_on()
self.is_fl_on = (self.fl_intensity > 0)
end
function KindlePowerD:afterResume()
if not self.device.hasFrontlight() then
return
end
if self.is_fl_on then
-- Kindle stock software should turn on the front light automatically. The follow statement
-- ensure the consistency of intensity.
self:setIntensityHW()
else
self:_turnOffFL()
end
end
return KindlePowerD