diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index 20c08e5c2..467b8dc55 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -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 diff --git a/frontend/device/generic/powerd.lua b/frontend/device/generic/powerd.lua index 5367f0e4d..6850de776 100644 --- a/frontend/device/generic/powerd.lua +++ b/frontend/device/generic/powerd.lua @@ -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 diff --git a/frontend/device/kobo/device.lua b/frontend/device/kobo/device.lua index e95925960..695f5702c 100644 --- a/frontend/device/kobo/device.lua +++ b/frontend/device/kobo/device.lua @@ -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() diff --git a/frontend/device/kobo/powerd.lua b/frontend/device/kobo/powerd.lua index f5ef3c031..2a63d4c57 100644 --- a/frontend/device/kobo/powerd.lua +++ b/frontend/device/kobo/powerd.lua @@ -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 diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index da8e229a5..941d3c6e4 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -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