public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] Battery charge status script for term or text console
@ 2015-11-04 23:53 Walter Dnes
  2015-11-05 11:53 ` J. Roeleveld
  0 siblings, 1 reply; 2+ messages in thread
From: Walter Dnes @ 2015-11-04 23:53 UTC (permalink / raw
  To: Gentoo Users List

[-- Attachment #1: Type: text/plain, Size: 1106 bytes --]

  The attached script requires "make menuconfig" to have

Power management and ACPI options  --->
[*] ACPI (Advanced Configuration and Power Interface) Support  --->
<*>   Battery

or CONFIG_ACPI_BATTERY enabled if you do .config manually.

  The script reads /sys/class/power_supply/BAT1/uevent on a laptop and
calculates the percentage of battery charge.  Because it only reads from
/sys, and does not write, it can be run by a regular user.  The script
is primitive, and has to be stopped with {CTRL}{C}.

  The script runs in text mode, and can run in xterm or even a pure text
console.  I've removed the #!/bin/bash at the top of the attached file
and chmod'ed it non-executable.  The mailing list software seems to
reject any such attachments.  You can source the file as is, or chmod it
executable, and insert #!/bin/bash at the top.  Note that there is an
ANSI escape sequence in the script, to keep the output in one location.
Regular cut-and-paste can fail if you try to copy from the screen.

-- 
Walter Dnes <waltdnes@waltdnes.org>
I don't run "desktop environments"; I run useful applications

[-- Attachment #2: battpct --]
[-- Type: text/plain, Size: 1535 bytes --]

#
# If no battery found, bail out
if [ ! -d /sys/class/power_supply/BAT1 ]; then
   echo "***ERROR***; Battery not detected"
   exit
fi
#
# Get line containing full design charge value
full=`grep POWER_SUPPLY_CHARGE_FULL_DESIGN /sys/class/power_supply/BAT1/uevent`
#
# Strip out everything except the actual number itself
full1="${full#POWER*=}"
#
# Get half of the full value, for use in emulating "round offs" in integer
# arithmetic.
half=$(( ${full} / 2 ))
while true
do
#
# Get line containing current charge value and strip out everything
# except the actual number itself
   now=`grep POWER_SUPPLY_CHARGE_NOW /sys/class/power_supply/BAT1/uevent`
   now1="${now#POWER*=}"
#
# Multiply by 1000, so that the following division is actually 10 times
# the percentage.
   now2=$(( ${now1} * 1000 ))
   percent=$(( ${now2} / ${full1} ))
#
# Get the remainder of the division.  If it's greater than or equal to
# half the divisor (as determined above) add 1 to the percent x 10 value.
   remainder=$(( ${now2} % ${full1} ))
   if [ ${remainder} -ge ${half} ]; then
      percent=$(( ${percent} + 1 ))
   fi
#
# Treating ${percent} as a string, figure out where to place a decimal
# point to fake 10ths of a percent.
   pointer=$(( ${#percent} - 1 ))
   percent1="${percent:0:${pointer}}.${percent:${pointer}}"
#
# Backtrack the cursor up to 7 columns, and display the new value
   echo -n "^[[7D${percent1} %"
#
# Wait 5 seconds and update again.  This is an infinite loop.
   sleep 5
done

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [gentoo-user] Battery charge status script for term or text console
  2015-11-04 23:53 [gentoo-user] Battery charge status script for term or text console Walter Dnes
@ 2015-11-05 11:53 ` J. Roeleveld
  0 siblings, 0 replies; 2+ messages in thread
From: J. Roeleveld @ 2015-11-05 11:53 UTC (permalink / raw
  To: gentoo-user

On 5 November 2015 00:53:25 CET, Walter Dnes <waltdnes@waltdnes.org> wrote:
>  The attached script requires "make menuconfig" to have
>
>Power management and ACPI options  --->
>[*] ACPI (Advanced Configuration and Power Interface) Support  --->
><*>   Battery
>
>or CONFIG_ACPI_BATTERY enabled if you do .config manually.
>
>  The script reads /sys/class/power_supply/BAT1/uevent on a laptop and
>calculates the percentage of battery charge.  Because it only reads
>from
>/sys, and does not write, it can be run by a regular user.  The script
>is primitive, and has to be stopped with {CTRL}{C}.
>
> The script runs in text mode, and can run in xterm or even a pure text
>console.  I've removed the #!/bin/bash at the top of the attached file
>and chmod'ed it non-executable.  The mailing list software seems to
>reject any such attachments.  You can source the file as is, or chmod
>it
>executable, and insert #!/bin/bash at the top.  Note that there is an
>ANSI escape sequence in the script, to keep the output in one location.
>Regular cut-and-paste can fail if you try to copy from the screen.

FYI, on my laptop it's BAT0, not BAT1.

--
Joost
-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-11-05 11:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-04 23:53 [gentoo-user] Battery charge status script for term or text console Walter Dnes
2015-11-05 11:53 ` J. Roeleveld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox