View Single Post
Old 12-15-2024, 09:42 AM   #1439
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 80,077
Karma: 147983159
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Quote:
Originally Posted by Aleron Ives View Post
I've made another update to my battery statistics calculation script. Version 1.2 brings a few improvements:
  • The v_pct divisor is now calculated automatically from v_max and v_min, rather than using a hardcoded value.
  • v_now no longer displays nonsensical values like 4.8 V.

I had hoped to use bc to do proper floating-point arithmetic, but it seems that Kobo's version of BusyBox doesn't have it, so instead I've resorted to manipulating v_now, v_min, and v_max as strings, which avoids doing any actual arithmetic for the units conversion.

Remember that if you have the Libra Colour, you must replace:

/sys/class/power_supply/battery/
with
/sys/class/power_supply/bd71827_bat/

in order for the script to work. Other Kobo models may require greater changes, as some statistics may not be available.

Code:
# Battery Statistics Calculator 1.2 (2024-08-28) by Aleron Ives
#
# This script calculates battery statistics for the Kobo Libra 2.
# Check the contents of /sys/class/power_supply/ if you use a different
# model to ensure that the statistics you want to track are available.
#
# You can use NickelMenu to invoke this script like so:
# menu_item :main :Battery :cmd_output :500 :/mnt/onboard/.adds/battcalc.sh

# Gather the necessary statistics

meter=$(cat /sys/class/power_supply/battery/capacity)
v_now=$(cat /sys/class/power_supply/battery/voltage_now)
v_min=$(cat /sys/class/power_supply/battery/voltage_min)
v_max=$(cat /sys/class/power_supply/battery/voltage_max)
c_now=$(cat /sys/class/power_supply/battery/charge_now)
c_full=$(cat /sys/class/power_supply/battery/charge_full)
c_dfull=$(cat /sys/class/power_supply/battery/charge_full_design)

# Format the statistics

let c_now/=1000; let c_full/=1000; let c_dfull/=1000 # Convert to mAh
let charge=$c_now*100/$c_full     # Calculate charge percentage from mAh
let c_health=$c_full*100/$c_dfull # Calculate health percentage from mAh
let v_div=$v_max-$v_min; let v_div/=100    # Calculate charge percentage from V
let v_pct=$v_max-$v_now; let v_pct/=$v_div # "
let v_pct=100-$v_pct                       # "
v_nowr=$(echo $v_now | cut -b 2-4) # Simulate floating-point arithmetic
v_now=$(echo $v_now | cut -b 1)    # "
v_minr=$(echo $v_min | cut -b 2-3) # "
v_min=$(echo $v_min | cut -b 1)    # "
v_maxr=$(echo $v_max | cut -b 2-3) # "
v_max=$(echo $v_max | cut -b 1)    # "

# Display the results

echo Capacity: $c_now mAh / $charge% / $meter%
echo Voltage: $v_min.$v_minr V / $v_now.$v_nowr V / $v_max.$v_maxr V / $v_pct%
echo Health: $c_full mAh / $c_dfull mAh / $c_health%
If you download the attached version, remember to remove the .txt extension! The forum doesn't allow uploading text files with the .sh extension.
I decided to install the battery script on my Libra 2. I'm running 4.38.23171 and it's not working. Here's what I get.

Click image for larger version

Name:	BarryFail.png
Views:	487
Size:	603.4 KB
ID:	212485
JSWolf is offline   Reply With Quote