Kobo: Handle a few PowerCover quirks...

* The first few capacity reads after connecting to the cover may fail

* The PowerCover may trigger spurious move/add usb_plug uevent,
which translate into Charging events for us.
Instead of blindly lighting up the charging LED on those,
check if the device is actually charging, first.
This commit is contained in:
NiLuJe
2022-01-16 19:08:42 +01:00
committed by poire-z
parent 7018853940
commit eb0c2bfc93
5 changed files with 31 additions and 13 deletions

View File

@@ -467,6 +467,9 @@ end
-- Device specific method for toggling the charging LED
function Device:toggleChargingLED(toggle) end
-- Device specific method for setting the charging LED to the right state
function Device:setupChargingLED() end
-- Device specific method for enabling a specific amount of CPU cores
-- (Should only be implemented on embedded platforms where we can afford to control that without screwing with the system).
function Device:enableCPUCores(amount) end

View File

@@ -111,6 +111,17 @@ function BasePowerD:read_int_file(file)
end
end
function BasePowerD:unchecked_read_int_file(file)
local fd = io.open(file, "r")
if fd then
local int = fd:read("*number")
fd:close()
return int
else
return
end
end
function BasePowerD:read_str_file(file)
local fd = io.open(file, "r")
if fd then
@@ -160,8 +171,12 @@ function BasePowerD:getAuxCapacity()
local now_ts = UIManager:getTime()
if (now_ts - self.last_aux_capacity_pull_time):tonumber() >= 60 then
self.aux_batt_capacity = self:getAuxCapacityHW()
self.last_aux_capacity_pull_time = now_ts
local aux_batt_capa = self:getAuxCapacityHW()
-- If the read failed, don't update our cache, and retry next time.
if aux_batt_capa then
self.aux_batt_capacity = aux_batt_capa
self.last_aux_capacity_pull_time = now_ts
end
end
return self.aux_batt_capacity
end

View File

@@ -387,7 +387,7 @@ local KoboIo = Kobo:new{
},
}
function Kobo:_refreshChargingLED()
function Kobo:setupChargingLED()
if G_reader_settings:nilOrTrue("enable_charging_led") then
if self:hasAuxBattery() and self.powerd:isAuxBatteryConnected() then
self:toggleChargingLED(self.powerd:isAuxCharging())
@@ -512,7 +512,7 @@ function Kobo:init()
-- We have no way of querying the current state of the charging LED, so, start from scratch.
-- Much like Nickel, start by turning it off.
self:toggleChargingLED(false)
self:_refreshChargingLED()
self:setupChargingLED()
-- Only enable a single core on startup
self:enableCPUCores(1)
@@ -888,7 +888,7 @@ function Kobo:resume()
end
-- A full suspend may have toggled the LED off.
self:_refreshChargingLED()
self:setupChargingLED()
end
function Kobo:saveSettings()

View File

@@ -100,7 +100,9 @@ function KoboPowerD:init()
self.aux_batt_charging_file = self.aux_battery_sysfs .. "/charge_status" -- "usb_conn" would not allow us to detect the "Full" state
self.getAuxCapacityHW = function(this)
return this:read_int_file(this.aux_batt_capacity_file)
-- NOTE: The first few reads after connecting to the PowerCover may fail, in which case,
-- we pass that detail along to PowerD so that it may retry the call sooner.
return this:unchecked_read_int_file(this.aux_batt_capacity_file)
end
self.isAuxBatteryConnectedHW = function(this)
@@ -110,7 +112,7 @@ function KoboPowerD:init()
self.isAuxChargingHW = function(this)
-- 0 when not charging
-- 3 when full
-- 2 when charging via DCP and/or when battery is high (> 70%)
-- 2 when charging via DCP
local charge_status = this:read_int_file(this.aux_batt_charging_file)
return charge_status ~= 0 and charge_status ~= 3
end

View File

@@ -1759,17 +1759,15 @@ end
-- The common operations that should be performed when the device is plugged to a power source.
function UIManager:_beforeCharging()
if G_reader_settings:nilOrTrue("enable_charging_led") then
Device:toggleChargingLED(true)
end
-- Leave the kernel some time to figure it out ;o).
self:scheduleIn(0.5, function() Device:setupChargingLED() end)
self:broadcastEvent(Event:new("Charging"))
end
-- The common operations that should be performed when the device is unplugged from a power source.
function UIManager:_afterNotCharging()
if G_reader_settings:nilOrTrue("enable_charging_led") then
Device:toggleChargingLED(false)
end
-- Leave the kernel some time to figure it out ;o).
self:scheduleIn(0.5, function() Device:setupChargingLED() end)
self:broadcastEvent(Event:new("NotCharging"))
end