From 596e20e588852501e163cb7f2ed205e1a36873aa Mon Sep 17 00:00:00 2001 From: David Engster Date: Sat, 31 Mar 2018 15:46:41 +0200 Subject: [PATCH] [feat] kobo/powerd: Support for automatic warmth This is the first step to support "automatic warmth", meaning that warmth will be set according to the current time. The user can set a "bedtime" at which warmth should be maximal. Warmth will increase towards approaching bedtime and decrease afterwards. Add new members 'auto_warmth' and 'max_warmth_hour' which tell if this feature is enabled and the "bedtime", resp. Add a method 'calculateAutoWarmth' which will set the current warmth according to the current time. The progression is linear but not symmetrical: we start 5h before "bedtime", but turn back warmth to '0' 2h after it, to make sure that warmth is '0' in the morning. For automatically setting warmth in the background we use the backgroundrunner plugin, because not only is it more comfortable to use, but we also cannot require 'uimanager' during device initialization. --- frontend/device/kobo/powerd.lua | 43 +++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/frontend/device/kobo/powerd.lua b/frontend/device/kobo/powerd.lua index a4acaa536..c951aa393 100644 --- a/frontend/device/kobo/powerd.lua +++ b/frontend/device/kobo/powerd.lua @@ -1,5 +1,6 @@ local BasePowerD = require("device/generic/powerd") local NickelConf = require("device/kobo/nickel_conf") +local PluginShare = require("pluginshare") local SysfsLight = require ("device/kobo/sysfs_light") local batt_state_folder = @@ -17,6 +18,8 @@ local KoboPowerD = BasePowerD:new{ batt_capacity_file = batt_state_folder .. "capacity", is_charging_file = batt_state_folder .. "status", fl_warmth = nil, + auto_warmth = false, + max_warmth_hour = 23, } -- TODO: Remove KOBO_LIGHT_ON_START @@ -92,6 +95,7 @@ function KoboPowerD:init() -- not be called) self.hw_intensity = 20 self.initial_is_fl_on = true + self.autowarmth_job_running = false if self.device.hasFrontlight() then -- If this device has natural light (currently only KA1) @@ -190,8 +194,43 @@ end function KoboPowerD:setWarmth(warmth) if self.fl == nil then return end - self.fl_warmth = warmth - self.fl:setWarmth(warmth) + if not warmth and self.auto_warmth then + self:calculateAutoWarmth() + end + self.fl_warmth = warmth or self.fl_warmth + self.fl:setWarmth(self.fl_warmth) +end + +-- Sets fl_warmth according to current hour and max_warmth_hour +-- and starts background job if necessary. +function KoboPowerD:calculateAutoWarmth() + local current_time = os.date("%H") + os.date("%M")/60 + local max_hour = self.max_warmth_hour + local diff_time = max_hour - current_time + if diff_time < 0 then + diff_time = diff_time + 24 + end + if diff_time < 12 then + -- We are before bedtime. Use a slower progression over 5h. + self.fl_warmth = math.max(20 * (5 - diff_time), 0) + else + -- After bedtime, it only takes 2h to reach zero warmth. + self.fl_warmth = math.max(100 - 50 * (24 - diff_time), 0) + end + self.fl_warmth = math.floor(self.fl_warmth + 0.5) + -- Enable background job for setting Warmth, if not already done. + if not self.autowarmth_job_running then + table.insert(PluginShare.backgroundJobs, { + when = 180, + repeated = true, + executable = function() + if self.auto_warmth then + self:setWarmth() + end + end, + }) + self.autowarmth_job_running = true + end end function KoboPowerD:getCapacityHW()