2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00

Merge pull request #433 from chrox/master

show battery icon in crereader page header
This commit is contained in:
Qingping Hou 2014-01-04 09:07:22 -08:00
commit e70e4a542d
15 changed files with 243 additions and 126 deletions

View File

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

View File

@ -297,6 +297,11 @@ function CreDocument:setVisiblePageCount(new_count)
self._document:setVisiblePageCount(new_count)
end
function CreDocument:setBatteryState(state)
DEBUG("CreDocument: set battery state", state)
self._document:setBatteryState(state)
end
function CreDocument:register(registry)
registry:addProvider("txt", "application/txt", self)
registry:addProvider("epub", "application/epub", self)

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,64 @@
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
capacity_pulled_count = 0,
capacity_cached_count = 10,
}
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()
if capacity_pulled_count == capacity_cached_count then
capacity_pulled_count = 0
return self:getCapacityHW()
else
capacity_pulled_count = capacity_pulled_count + 1
return self.battCapacity or self:getCapacityHW()
end
end
function BasePowerD:isCharging()
return self:isChargingHW()
end
function BasePowerD:suspend()
return self:suspendHW()
end
function BasePowerD:wakeUp()
return self:wakeUpHW()
end
return BasePowerD

View File

@ -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

View File

@ -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,
is_charging = 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.is_charging = self.lipc_handle:get_int_property("com.lab126.powerd", "isCharging")
else
self.is_charging = self:read_int_file(self.is_charging_file)
end
return self.is_charging == 1
end
return KindlePowerD

View File

@ -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

View File

@ -0,0 +1,41 @@
local BasePowerD = require("ui/device/basepowerd")
local KoboPowerD = BasePowerD:new{
fl_min = 1, fl_max = 100,
flIntensity = 20,
restore_settings = true,
fl = nil,
batt_capacity_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity",
is_charging_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/charge_now",
battCapacity = nil,
is_charging = 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
function KoboPowerD:getCapacityHW()
self.battCapacity = self:read_int_file(self.batt_capacity_file)
return self.battCapacity
end
function KoboPowerD:isChargingHW()
self.is_charging = self:read_int_file(self.is_charging_file)
return self.is_charging == 1
end
return KoboPowerD

View File

@ -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

View File

@ -204,10 +204,12 @@ end
function ReaderRolling:onPosUpdate(new_pos)
self.current_pos = new_pos
self:updateBatteryState()
end
function ReaderRolling:onPageUpdate(new_page)
self.current_page = new_page
self:updateBatteryState()
end
function ReaderRolling:onGotoPercent(percent)
@ -345,4 +347,15 @@ function ReaderRolling:onGotoPage(number)
return true
end
function ReaderRolling:updateBatteryState()
DEBUG("update battery state")
if self.view.view_mode == "page" then
local powerd = Device:getPowerDevice()
local state = powerd:isCharging() and -1 or powerd:getCapacity()
if state then
self.ui.document:setBatteryState(state)
end
end
end
return ReaderRolling

View File

@ -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],

View File

@ -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")

@ -1 +1 @@
Subproject commit bf3ef2a6c9002a665b8c224632eba744e6147fe0
Subproject commit 5c2afc4f9f40b51ebe0ee86c9ba200859559e5f9

View File

@ -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