Rockbox Technical Forums

Rockbox Development => Feature Ideas => Topic started by: cereal_killer on August 16, 2018, 03:46:15 PM

Title: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 16, 2018, 03:46:15 PM
Li-ion batteries (and I think this then applies to DAPs as well) should be stored at a 40 percent state-of-charge. The idea is to have a setting for power off, like the sleep timer, but not after a given time, but when the battery reaches 40% and then put the Player away. Is a plugin a better option?
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 16, 2018, 10:39:00 PM
if you set your idle Poweroff you could use a lua script to hold it on till the battery level gets low enough

Code: [Select]
require("actions")   -- Contains rb.actions & rb.contexts
local battery

if (not rb.battery_level) or (not rb.audio_stop) then
rb.splash(1000, "missing functions")
os.exit()
end

if rb.backlight_force_on then rb.backlight_force_on() end

while rb.get_plugin_action(0) ~= rb.actions.PLA_CANCEL do
local battery = rb.battery_level()
rb.splash(1000, battery .. "%")

if battery < 45 then
    rb.audio_stop()
    rb.lcd_clear_display()
    rb.lcd_update()
os.exit()
end
end

if you really wanted to be slick you could change the poweroff time out as well
http://forums.rockbox.org/index.php/topic,52389.msg242523.html#msg242523
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 17, 2018, 05:21:51 AM
Hello Bilgus, thanks for the reply and that you even offered a solution. So does this mean to create a lua plugin, start it and it will power off? What if the level is already below 40 percent? By now this is over my head and I really need to dig into this.
Title: Re: Power off at 40% state-of-charge for storing
Post by: gomezz on August 17, 2018, 05:59:24 AM
Never heard of this before.  Do you have a reference for this claim?
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 17, 2018, 09:46:09 AM
Never heard of this before.  Do you have a reference for this claim?

https://batteryuniversity.com/index.php/learn/article/how_to_store_batteries

I hope this is reliable.
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 17, 2018, 11:37:55 AM
yes you would start it up and it would turn off once the battery was low enough if the battery was already low it would do the same
Title: Re: Power off at 40% state-of-charge for storing
Post by: TAC109 on August 17, 2018, 07:27:05 PM
It depends how long you want to store the device. I have a Fuze+ that I'm not using at present and it self-discharges from full to completely empty in about 3 months without any use. I currently charge it to full about every 2 months to avoid having the battery going completely flat.
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 18, 2018, 04:59:13 AM
According to the page I linked, storing a battery fully charged leads to a non-recoverable capacity loss, especially when the temperature is elevated. Thus the 40 percent charge, when storing.
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 18, 2018, 07:09:45 AM
here you go..

Code: [Select]
local batteryoff = 45 --percentage of battery when script will quit

require("actions")   -- Contains rb.actions & rb.contexts

if not pcall(require, "settings") then
    -- check if settings.lua is available otherwise put in dummy tables
    rb.system = rb.system or {}
    rb.system.global_settings = rb.system.global_settings or {}
end

if (not rb.battery_level) or (not rb.audio_stop) then
    rb.splash(1000, "missing functions")
    os.exit()
end

local batterylevel = rb.battery_level()
local charging = false
local poweroff = rb.system.global_settings.poweroff or {}

if batterylevel < batteryoff then
    rb.splash(1000, "battery needs charged " .. batterylevel .. "% < " .. batteryoff .. "%")
    if rb.charging_state then charging = rb.charging_state() end
    -- exit if not already charging
    if not charging then os.exit() end
end

function check_battery()
    local dots = ""
    if rb.backlight_force_on then rb.backlight_force_on() end

    repeat
        rb.lcd_clear_display()
        rb.lcd_update()
        if rb.charging_state then charging = rb.charging_state() end

        batterylevel = rb.battery_level()

        if charging then
            rb.splash(1, "charging " .. batterylevel .. "%" .. dots)
            dots = dots .. "."
            if string.len(dots) > 4 then dots = "" end
        else
            rb.splash(1, batterylevel .. "% > " .. batteryoff .. "%")
        end

        if batterylevel < batteryoff and (not charging) then
            rb.audio_stop()
            rb.lcd_clear_display()
            rb.lcd_update()

            if rb.global_settings then
                -- set poweroff time to 1 minute
                rb.global_settings(poweroff[1], poweroff[2], 1)
            end

            return
        end
    until rb.get_plugin_action(500) == rb.actions.PLA_CANCEL
end

check_battery()
os.exit()

EDIT..
Code: [Select]
local batteryoff = 45 --percentage of battery when script will quit

--require("actions")   -- Contains rb.actions & rb.contexts

if not pcall(require, "settings") then
    -- check if settings.lua is available otherwise put in dummy tables
    rb.system = rb.system or {}
    rb.system.global_settings = rb.system.global_settings or {}
end

if (not rb.battery_level) or (not rb.audio_stop) then
    rb.splash(1000, "missing functions")
    os.exit()
end

local batterylevel = rb.battery_level()
local charging = false
local poweroff = rb.system.global_settings.poweroff or {}

if batterylevel < batteryoff then
    rb.splash(1000, "battery needs charged " .. batterylevel .. "% < " .. batteryoff .. "%")
    if rb.charging_state then charging = rb.charging_state() end
    -- exit if not already charging
    if not charging then os.exit() end
end

function check_battery()
    local dots = ""
    if rb.backlight_force_on then rb.backlight_force_on() end

    repeat
        rb.lcd_clear_display()
        rb.lcd_update()
        if rb.charging_state then charging = rb.charging_state() end

        batterylevel = rb.battery_level()

        if charging then
            rb.splash(1, "charging " .. batterylevel .. "%" .. dots)
            dots = dots .. "."
            if string.len(dots) > 4 then dots = "" end
        else
            rb.splash(1, batterylevel .. "% > " .. batteryoff .. "%")
rb.reset_poweroff_timer()
        end

        if batterylevel < batteryoff and (not charging) then
            rb.audio_stop()
            rb.lcd_clear_display()
            rb.lcd_update()

            if rb.global_settings then
                -- set poweroff time to 1 minute
                rb.global_settings(poweroff[1], poweroff[2], 1)
            end

            return
        end
    until rb.get_plugin_action(500) > 0
end

check_battery()
os.exit()
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 21, 2018, 12:59:39 PM
Thank you Bilgus, does this need the "Lua using global_settings, global_status" patch, or can I just add it to a  rockbox build.
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 21, 2018, 10:56:19 PM
it is set up to work with either.. if you don't use the global settings patch you just need to make sure idle poweroff is set
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 27, 2018, 02:39:53 PM
it is set up to work with either.. if you don't use the global settings patch you just need to make sure idle poweroff is set

Today I compiled a build with the plugin you provided without using the global settings patch. I hope I did everything right: I put a file called 45percent.lua containing your code into /apps/plugins and added a new line with this filename to CATEGORIES and to SOURCES (and to be sure to edit the right file to SOURCES.app.build after /* plugins common to all models */) The created build loads fine on my Sansa Clipv2. Starting the plugin right away shows 65% > 45%. Exiting can only be done with a reset (long power press). After a restart, when starting playback to speed up battery drain and then starting the plugin the player freezes showing only the status bar and the disk activity icon. Interestingly the game boomshine.lua also freezes the player. Regarding the setting idle power off, this doesn't change the behaviour (this setting is limited to 60 minutes, so if the battery is not at 45% after that time, the player will also shut down, right?)
I will add your patch and try again. I'll report back.
Thanks again.

Edit: Applying the patch didn't change the situation, the plugin still freezes the player. Any suggestions?
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 27, 2018, 04:20:33 PM
Cereal_killer, what player are you using?

I've done quite a bit to the lua backend so I might have broken something on your player.

Could you try 3.14 and see if the issue is reproducible?

Also is the count going down? like 60% > 45%.. 50% > 45%...

idle power off shouldn't be active while the plugin is active though I might be mistaken in that case play music as well
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 27, 2018, 06:16:29 PM
I edited the script above to quit on any key press and to reset the power off timer this should help with the issues
Title: Re: Power off at 40% state-of-charge for storing
Post by: cereal_killer on August 29, 2018, 11:24:14 AM
I use a Sansa Clip v2. I tried your edited script, but everything behaves the same as before. I started the plugin right after booting and it showed 48% > 45% for more than 6 hours. But after resetting the player, the battery level was at 9%. No keypress quit the plugin.

Boomshine.lua also freezes the player with 3.14.
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on August 30, 2018, 10:19:19 PM
I'll look into this hopefully the sim exhibits the same behavior
Title: Re: Power off at 40% state-of-charge for storing
Post by: Bilgus on September 07, 2018, 01:09:03 AM
I'm in the process of tracking down this error as far as I can tell it might be a nvram.bin corruption and or a  code alignment issue
Could you try downloading the latest dev version rename your current .rockbox folder and copy the new version over, on disconnect
allow the rolo restart and then try boomshine and see if it works?