From dda905271d135ada01948798764333c36b847572 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Thu, 22 Oct 2020 01:17:34 +0200 Subject: [PATCH] [Kobo, Mk. 7] Enable the power LED when charging (#6810) --- frontend/device/generic/device.lua | 4 ++ frontend/device/kobo/device.lua | 66 +++++++++++++++++++ .../elements/common_settings_menu_table.lua | 13 ++++ .../ui/elements/filemanager_menu_order.lua | 3 +- frontend/ui/elements/reader_menu_order.lua | 3 +- frontend/ui/uimanager.lua | 6 ++ 6 files changed, 93 insertions(+), 2 deletions(-) diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index 47df33c76..2469353ea 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -56,6 +56,7 @@ local Device = { canToggleGSensor = no, isGSensorLocked = no, canToggleMassStorage = no, + canToggleChargingLED = no, canUseWAL = yes, -- requires mmap'ed I/O on the target FS canRestart = yes, canSuspend = yes, @@ -412,6 +413,9 @@ function Device:lockGSensor(toggle) end end +-- Device specific method for toggling the charging LED +function Device:toggleChargingLED(toggle) end + --[[ prepare for application shutdown --]] diff --git a/frontend/device/kobo/device.lua b/frontend/device/kobo/device.lua index 78ad1900f..b8649730a 100644 --- a/frontend/device/kobo/device.lua +++ b/frontend/device/kobo/device.lua @@ -189,6 +189,7 @@ local KoboPika = Kobo:new{ -- Kobo Clara HD: local KoboNova = Kobo:new{ model = "Kobo_nova", + canToggleChargingLED = yes, hasFrontlight = yes, touch_snow_protocol = true, display_dpi = 300, @@ -213,6 +214,7 @@ local KoboNova = Kobo:new{ -- There's also a CM_ROTARY_ENABLE command, but which seems to do as much nothing as the STATUS one... local KoboFrost = Kobo:new{ model = "Kobo_frost", + canToggleChargingLED = yes, hasFrontlight = yes, hasKeys = yes, hasGSensor = yes, @@ -236,6 +238,7 @@ local KoboFrost = Kobo:new{ -- NOTE: Assume the same quirks as the Forma apply. local KoboStorm = Kobo:new{ model = "Kobo_storm", + canToggleChargingLED = yes, hasFrontlight = yes, hasKeys = yes, hasGSensor = yes, @@ -259,6 +262,7 @@ local KoboStorm = Kobo:new{ --- @fixme: Untested, assume it's Clara-ish for now. local KoboLuna = Kobo:new{ model = "Kobo_luna", + canToggleChargingLED = yes, hasFrontlight = yes, touch_snow_protocol = true, display_dpi = 212, @@ -338,6 +342,12 @@ function Kobo:init() self:initEventAdjustHooks() end end + + -- We have no way of querying the current state of the charging LED, so, our only sane choices are: + -- * Do nothing + -- * Turn it off on startup + -- I've chosen the latter, as I find it vaguely saner, more useful, and it matches Nickel's behavior (I think). + self:toggleChargingLED(false) end function Kobo:setDateTime(year, month, day, hour, min, sec) @@ -740,6 +750,62 @@ function Kobo:toggleGSensor(toggle) end end +function Kobo:toggleChargingLED(toggle) + if not self:canToggleChargingLED() then + return + end + + -- We have no way of querying the current state from the HW! + if toggle == nil then + return + end + + -- NOTE: While most/all Kobos actually have a charging LED, and it can usually be fiddled with in a similar fashion, + -- we've seen *extremely* weird behavior in the past when playing with it on older devices (c.f., #5479). + -- In fact, Nickel itself doesn't provide this feature on said older devices + -- (when it does, it's an option in the Energy saving settings), + -- which is why we also limit ourselves to "true" Mk. 7 devices. + local f = io.open("/sys/devices/platform/ntx_led/lit", "w") + if not f then + logger.err("cannot open /sys/devices/platform/ntx_led/lit for writing!") + return false + end + + -- c.f., strace -fittvyy -e trace=ioctl,file,signal,ipc,desc -s 256 -o /tmp/nickel.log -p $(pidof -s nickel) & + -- This was observed on a Forma, so I'm mildly hopeful that it's safe on other Mk. 7 devices ;). + if toggle == true then + -- NOTE: Technically, Nickel forces a toggle off before that, too. + -- But since we do that on startup, it shouldn't be necessary here... + f:write("ch 4") + f:flush() + f:write("cur 1") + f:flush() + f:write("dc 63") + f:flush() + else + f:write("ch 3") + f:flush() + f:write("cur 1") + f:flush() + f:write("dc 0") + f:flush() + f:write("ch 4") + f:flush() + f:write("cur 1") + f:flush() + f:write("dc 0") + f:flush() + f:write("ch 5") + f:flush() + f:write("cur 1") + f:flush() + f:write("dc 0") + f:flush() + end + + io.close(f) +end + -------------- device probe ------------ local codename = Kobo:getCodeName() diff --git a/frontend/ui/elements/common_settings_menu_table.lua b/frontend/ui/elements/common_settings_menu_table.lua index 1a9fd831c..04e134c9e 100644 --- a/frontend/ui/elements/common_settings_menu_table.lua +++ b/frontend/ui/elements/common_settings_menu_table.lua @@ -48,6 +48,19 @@ if Device:canToggleMassStorage() then common_settings.mass_storage_actions = MassStorage:getActionsMenuTable() end +if Device:canToggleChargingLED() then + -- Charging LED settings + common_settings.charging_led = { + text = _("Turn on the power LED when charging"), + checked_func = function() + return G_reader_settings:nilOrTrue("enable_charging_led") + end, + callback = function() + G_reader_settings:flipNilOrTrue("enable_charging_led") + end + } +end + -- Associate OS level file extensions (must be off by default, because we're not associated initially) if Device:canAssociateFileExtensions() then common_settings.file_ext_assoc = { diff --git a/frontend/ui/elements/filemanager_menu_order.lua b/frontend/ui/elements/filemanager_menu_order.lua index 3e81215f2..c47ae2a1f 100644 --- a/frontend/ui/elements/filemanager_menu_order.lua +++ b/frontend/ui/elements/filemanager_menu_order.lua @@ -41,12 +41,13 @@ local order = { "keyboard_layout", "time", "battery", + "charging_led", -- if Device:canToggleChargingLED() "autostandby", "autosuspend", "autoshutdown", "ignore_sleepcover", "ignore_open_sleepcover", - "mass_storage_settings", + "mass_storage_settings", -- if Device:canToggleMassStorage() "file_ext_assoc", "screenshot", }, diff --git a/frontend/ui/elements/reader_menu_order.lua b/frontend/ui/elements/reader_menu_order.lua index 61e89f04a..c60f8211d 100644 --- a/frontend/ui/elements/reader_menu_order.lua +++ b/frontend/ui/elements/reader_menu_order.lua @@ -62,12 +62,13 @@ local order = { "keyboard_layout", "time", "battery", + "charging_led", -- if Device:canToggleChargingLED() "autostandby", "autosuspend", "autoshutdown", "ignore_sleepcover", "ignore_open_sleepcover", - "mass_storage_settings", + "mass_storage_settings", -- if Device:canToggleMassStorage() "file_ext_assoc", "screenshot", }, diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index a6dae7888..14f601083 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -1312,10 +1312,16 @@ function UIManager:_afterResume() end function UIManager:_beforeCharging() + if G_reader_settings:nilOrTrue("enable_charging_led") then + Device:toggleChargingLED(true) + end self:broadcastEvent(Event:new("Charging")) end function UIManager:_afterNotCharging() + if G_reader_settings:nilOrTrue("enable_charging_led") then + Device:toggleChargingLED(false) + end self:broadcastEvent(Event:new("NotCharging")) end