refactoring frontlight device to more generic power device

pull/433/head
chrox 10 years ago
parent b6b3207a79
commit 92b62c907a

@ -65,7 +65,7 @@ function FileManagerMenu:setUpdateItemTable()
end
})
if Device:getFrontlight() ~= nil then
if Device:hasFrontlight() then
ReaderFrontLight:addToMainMenu(self.tab_item_table)
end

@ -1,6 +1,6 @@
local KindleFrontLight = require("ui/device/kindlefrontlight")
local KoboFrontLight = require("ui/device/kobofrontlight")
local BaseFrontLight = require("ui/device/basefrontlight")
local KindlePowerD = require("ui/device/kindlepowerd")
local KoboPowerD = require("ui/device/kobopowerd")
local BasePowerD = require("ui/device/basepowerd")
local Screen = require("ui/device/screen")
-- util
-- lfs
@ -12,7 +12,7 @@ local Device = {
touch_dev = nil,
model = nil,
firmware_rev = nil,
frontlight = nil,
powerd = nil,
has_no_keyboard = nil,
is_touch_device = nil,
has_front_light = nil,
@ -166,9 +166,9 @@ function Device:outofScreenSaver()
end
function Device:prepareSuspend() -- currently only used for kobo devices
local fl = self:getFrontlight()
if fl ~= nil then
fl.fl:sleep()
local powerd = self:getPowerDevice()
if powerd ~= nil then
powerd.fl:sleep()
end
self.screen:refresh(0)
self.screen_saver_mode = true
@ -181,9 +181,9 @@ end
function Device:Resume() -- currently only used for kobo devices
os.execute("echo 0 > /sys/power/state-extended")
self.screen:refresh(0)
local fl = self:getFrontlight()
if fl ~= nil then
fl.fl:restore()
local powerd = self:getPowerDevice()
if powerd ~= nil then
powerd.fl:restore()
end
self.screen_saver_mode = false
end
@ -215,20 +215,20 @@ function Device:usbPlugOut()
self.charging_mode = false
end
function Device:getFrontlight()
if self.frontlight ~= nil then
return self.frontlight
elseif self:hasFrontlight() then
function Device:getPowerDevice()
if self.powerd ~= nil then
return self.powerd
else
local model = self:getModel()
if model == "KindlePaperWhite" or model == "KindlePaperWhite2" then
self.frontlight = KindleFrontLight:new()
if model == "KindleTouch" or model == "KindlePaperWhite" or model == "KindlePaperWhite2" then
self.powerd = KindlePowerD:new{model = model}
elseif self:isKobo() then
self.frontlight = KoboFrontLight:new()
self.powerd = KoboPowerD:new()
else -- emulated FrontLight
self.frontlight = BaseFrontLight:new()
self.powerd = BasePowerD:new()
end
end
return self.frontlight
return self.powerd
end
return Device

@ -1,25 +0,0 @@
local BaseFrontLight = {
min = 1, max = 10,
intensity = nil,
}
function BaseFrontLight:new(o)
local o = o or {}
setmetatable(o, self)
self.__index = self
if o.init then o:init() end
return o
end
function BaseFrontLight:init() end
function BaseFrontLight:toggle() end
function BaseFrontLight:setIntensityHW() end
function BaseFrontLight:setIntensity(intensity)
intensity = intensity < self.min and self.min or intensity
intensity = intensity > self.max and self.max or intensity
self.intensity = intensity
self:setIntensityHW()
end
return BaseFrontLight

@ -0,0 +1,55 @@
local BasePowerD = {
fl_min = 0, -- min frontlight intensity
fl_max = 10, -- max frontlight intensity
flIntensity = nil, -- frontlight intensity
battCapacity = nil, -- battery capacity
model = nil -- device model
}
function BasePowerD:new(o)
local o = o or {}
setmetatable(o, self)
self.__index = self
if o.init then o:init() end
return o
end
function BasePowerD:init() end
function BasePowerD:toggleFrontlight() end
function BasePowerD:setIntensityHW() end
function BasePowerD:getCapacityHW() end
function BasePowerD:isChargingHW() end
function BasePowerD:suspendHW() end
function BasePowerD:wakeUpHW() end
function BasePowerD:read_int_file(file)
local f = io.open(file, "r")
local sysint = tonumber(f:read("*all"):match("%d+"))
f:close()
return sysint
end
function BasePowerD:setIntensity(intensity)
intensity = intensity < self.fl_min and self.fl_min or intensity
intensity = intensity > self.fl_max and self.fl_max or intensity
self.flIntensity = intensity
self:setIntensityHW()
end
function BasePowerD:getCapacity()
return self:getCapacityHW()
end
function BasePowerD:isCharging()
return self:isChargingHW()
end
function BasePowerD:suspend()
return self:suspendHW()
end
function BasePowerD:wakeUp()
return self:wakeUpHW()
end
return BasePowerD

@ -1,37 +0,0 @@
local BaseFrontLight = require("ui/device/basefrontlight")
-- liblipclua, see require below
local KindleFrontLight = BaseFrontLight:new{
min = 0, max = 24,
-- FIXME: Check how to handle this on the PW2, initial reports on IRC suggest that this isn't possible anymore
kpw_fl = "/sys/devices/system/fl_tps6116x/fl_tps6116x0/fl_intensity",
intensity = nil,
lipc_handle = nil,
}
function KindleFrontLight:init()
require "liblipclua"
self.lipc_handle = lipc.init("com.github.koreader")
if self.lipc_handle then
self.intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
end
end
function KindleFrontLight:toggle()
local f = io.open(self.kpw_fl, "r")
local sysint = tonumber(f:read("*all"):match("%d+"))
f:close()
if sysint == 0 then
self:setIntensity(self.intensity)
else
os.execute("echo -n 0 > " .. self.kpw_fl)
end
end
function KindleFrontLight:setIntensityHW()
if self.lipc_handle ~= nil then
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", self.intensity)
end
end
return KindleFrontLight

@ -0,0 +1,82 @@
local BasePowerD = require("ui/device/basepowerd")
-- liblipclua, see require below
local KindlePowerD = BasePowerD:new{
fl_min = 0, fl_max = 24,
-- FIXME: Check how to handle this on the PW2, initial reports on IRC suggest that this isn't possible anymore
kpw_frontlight = "/sys/devices/system/fl_tps6116x/fl_tps6116x0/fl_intensity",
kt_kpw_capacity = "/sys/devices/system/yoshi_battery/yoshi_battery0/battery_capacity",
kpw_charging = "/sys/devices/platform/aplite_charger.0/charging",
kt_charging = "/sys/devices/platform/fsl-usb2-udc/charging",
flIntensity = nil,
battCapacity = nil,
isCharging = nil,
lipc_handle = nil,
}
function KindlePowerD:new(o)
local o = o or {}
setmetatable(o, self)
self.__index = self
if o.init then o:init(o.model) end
return o
end
function KindlePowerD:init(model)
local lipc = require("liblipclua")
if lipc then
self.lipc_handle = lipc.init("com.github.koreader")
end
if model == "KindleTouch" then
self.batt_capacity_file = self.kt_kpw_capacity
self.is_charging_file = self.kt_charging
elseif model == "KindlePaperWhite" or model == "KindlePaperWhite2" then
self.fl_intensity_file = self.kpw_frontlight
self.batt_capacity_file = self.kt_kpw_capacity
self.is_charging_file = self.kpw_charging
end
if self.lipc_handle then
self.flIntensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
else
self.flIntensity = self:read_int_file(self.fl_intensity_file)
end
end
function KindlePowerD:toggleFrontlight()
local sysint = self:read_int_file(self.fl_intensity_file)
if sysint == 0 then
self:setIntensity(self.flIntensity)
else
os.execute("echo -n 0 > " .. self.fl_intensity_file)
end
end
function KindlePowerD:setIntensityHW()
if self.lipc_handle ~= nil then
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", self.flIntensity)
else
os.execute("echo -n ".. self.flIntensity .." > " .. self.fl_intensity_file)
end
end
function KindlePowerD:getCapacityHW()
if self.lipc_handle ~= nil then
self.battCapacity = self.lipc_handle:get_int_property("com.lab126.powerd", "battLevel")
else
self.battCapacity = self:read_int_file(self.batt_capacity_file)
end
return self.battCapacity
end
function KindlePowerD:isChargingHW()
if self.lipc_handle ~= nil then
self.isCharging = self.lipc_handle:get_int_property("com.lab126.powerd", "isCharging")
else
self.isCharging = self:read_int_file(self.is_charging_file)
end
return self.isCharging
end
return KindlePowerD

@ -1,26 +0,0 @@
local BaseFrontLight = require("ui/device/basefrontlight")
local KoboFrontLight = BaseFrontLight:new{
min = 1, max = 100,
intensity = 20,
restore_settings = true,
fl = nil,
}
function KoboFrontLight:init()
self.fl = kobolight.open()
end
function KoboFrontLight:toggle()
if self.fl ~= nil then
self.fl:toggle()
end
end
function KoboFrontLight:setIntensityHW()
if self.fl ~= nil then
self.fl:setBrightness(self.intensity)
end
end
return KoboFrontLight

@ -0,0 +1,26 @@
local BasePowerD = require("ui/device/basepowerd")
local KoboPowerD = BasePowerD:new{
fl_min = 1, fl_max = 100,
flIntensity = 20,
restore_settings = true,
fl = nil,
}
function KoboPowerD:init()
self.fl = kobolight.open()
end
function KoboPowerD:toggleFrontlight()
if self.fl ~= nil then
self.fl:toggle()
end
end
function KoboPowerD:setIntensityHW()
if self.fl ~= nil then
self.fl:setBrightness(self.flIntensity)
end
end
return KoboPowerD

@ -13,7 +13,7 @@ local ReaderFrontLight = InputContainer:new{
}
function ReaderFrontLight:init()
if Device:getFrontlight() ~= nil then
if Device:isTouchDevice() then
self.ges_events = {
Adjust = {
GestureRange:new{
@ -32,21 +32,21 @@ function ReaderFrontLight:init()
end
function ReaderFrontLight:onAdjust(arg, ges)
local fl = Device:getFrontlight()
if fl.intensity ~= nil then
local powerd = Device:getPowerDevice()
if powerd.intensity ~= nil then
local rel_proportion = ges.distance / Screen:getWidth()
local delta_int = self.steps[math.ceil(#self.steps*rel_proportion)] or self.steps[#self.steps]
local msg = nil
if ges.direction == "north" then
msg = _("Increase front light intensity to ")
fl:setIntensity(fl.intensity + delta_int)
powerd:setIntensity(powerd.intensity + delta_int)
elseif ges.direction == "south" then
msg = _("Decrease front light intensity to ")
fl:setIntensity(fl.intensity - delta_int)
powerd:setIntensity(powerd.intensity - delta_int)
end
if msg ~= nil then
UIManager:show(Notification:new{
text = msg..fl.intensity,
text = msg..powerd.intensity,
timeout = 1
})
end
@ -65,10 +65,10 @@ function ReaderFrontLight:addToMainMenu(tab_item_table)
end
function ReaderFrontLight:onShowFlDialog()
local fl = Device:getFrontlight()
local powerd = Device:getPowerDevice()
self.fl_dialog = InputDialog:new{
title = _("Frontlight Level"),
input_hint = ("(%d - %d)"):format(fl.min, fl.max),
input_hint = ("(%d - %d)"):format(powerd.fl_min, powerd.fl_max),
buttons = {
{
{
@ -76,7 +76,7 @@ function ReaderFrontLight:onShowFlDialog()
enabled = true,
callback = function()
self.fl_dialog.input:setText("")
fl:toggle()
powerd:toggleFrontlight()
end,
},
{
@ -107,14 +107,14 @@ end
function ReaderFrontLight:close()
self.fl_dialog:onClose()
G_reader_settings:saveSetting("frontlight_intensity", Device:getFrontlight().intensity)
G_reader_settings:saveSetting("frontlight_intensity", Device:getPowerDevice().flIntensity)
UIManager:close(self.fl_dialog)
end
function ReaderFrontLight:fldialIntensity()
local number = tonumber(self.fl_dialog:getInputText())
if number ~= nil then
Device:getFrontlight():setIntensity(number)
Device:getPowerDevice():setIntensity(number)
end
end

@ -139,7 +139,7 @@ function ReaderUI:init()
}
table.insert(self.active_widgets, reader_ss)
-- frontlight controller
if Device:getFrontlight() then
if Device:hasFrontlight() then
table.insert(self, ReaderFrontLight:new{
dialog = self.dialog,
view = self[1],

@ -288,7 +288,7 @@ function UIManager:run()
elseif input_event == "NotCharging" then
Device:usbPlugOut()
elseif input_event == "Light" then
Device:getFrontlight():toggle()
Device:getPowerDevice():toggleFrontlight()
elseif (input_event == "Power" and not Device.screen_saver_mode) or
input_event == "Suspend" then
local InfoMessage = require("ui/widget/infomessage")

@ -161,11 +161,11 @@ local last_file = G_reader_settings:readSetting("lastfile")
--87712cf0e43fed624f8a9f610be42b1fe174b9fe
do
local fl = Device:getFrontlight()
if fl and fl.restore_settings then
local powerd = Device:getPowerDevice()
if powerd and powerd.restore_settings then
local intensity = G_reader_settings:readSetting("frontlight_intensity")
intensity = intensity or fl.intensity
fl:setIntensity(intensity)
intensity = intensity or powerd.flIntensity
powerd:setIntensity(intensity)
end
end

Loading…
Cancel
Save