xbattbar-check-sysfs

Description of the page content

Forth program that feeds xbattbar with battery data taken from sysfs.

Tags:

xbattbar uses APM by default, or optionally (in newer versions) ACPI. But none of them was active in the Linux kernel of my Debian installation in Pandora. Compiling the kernel would be a nightmare because of the special patches required by Pandora.

Finally I found a solution: The Pandora's battery information is in the sysfs system, under the the following directory:

/sys/class/power_supply/bq27500-0/

On the other hand, xbattbar uses two programs to get the battery data, one for ACPI and other for APM:

/usr/lib/xbattbar/xbattbar-check-acpi
/usr/lib/xbattbar/xbattbar-check-apm

The second one is a binary file; but the first one is a Perl program and it wasn't difficult to understand what it does: it simply prints two values in certain format. I wrote a Forth program, with Gforth, to emulate the Perl program, but using the information provided by sysfs. Then a simple symbolic link does the trick.

Source code

#! /usr/bin/env gforth 

\ xbattbar-check-sysfs.fs
\ xbattbar-check-sysfs (version A-0020121104)

\ This program, 'xbattbar-check-sysfs', prints the status of the
\ Pandora's battery with the format required by xbattbar
\ (emulating the output of the Perl program
\ /usr/lib/xbattbar/xbattbar-check-acpi) 
\
\ Its goal is to make xbattbar to work without APM or ACPI,
\ getting the battery data from sysfs instead.

\ Copyright (C) 2012 Marcos Cruz (programandala.net)

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

\ 2012-11-04 First version.
\ 2012-12-04 Simpler usage intructions, without installation.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Usage as a parameter (the simple way)

\ Simply start xbattbar as usual, but add the -s parameter and
\ the path to this program:

\ xbattbar -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs

\ Of course, consult the other parameters in the manual ('man
\ xbattbar').

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Usage with installation (the hard way)

\ This way you don't need to change the way you call xbattbar,
\ but the installation may be overriden by an upgrade of
\ xbattbar.

\ As root user:

\ 1) cd /usr/lib/xbattbar/
\ 2) mv xbattbar-check-acpi xbattbar-check-acpi.ORIGINAL
\ 3) mv xbattbar-check-apm xbattbar-check-apm.ORIGINAL
\ 4) ln -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs xbattbar-check-acpi
\ 5) ln -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs xbattbar-check-apm

\ Remark: xbattbar uses APM by default unless you start it with
\ the "-c" option to use ACPI instead. Since both original
\ programs have been redirected to this new one, xbattbar will
\ work fine in both cases.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

: sconstant  (  a1 u1 "name" -- )
  create  s,
  does>  ( pfa -- a2 u1 )  count
  ;

\ Directory that contains the Pandora's battery information:
s" /sys/class/power_supply/bq27500-0/" sconstant sys_dir

sys_dir s" status" s+ sconstant status_file
sys_dir s" capacity" s+ sconstant capacity_file

false [if]  \ use Gforth's 'slurp-file'

\ 'slurp-file' does not work with files under /sys/,
\ it causes the error "could not read whole file".

' slurp-file alias content

[else] \ alternative to 'slurp-file'

variable fid  \ file id
256 constant max_line
create line_buffer max_line 2 + allot

: open_file  ( a u -- )
  \ a u = file name
  r/o open-file throw fid !
  ;
: close_file  ( -- )
  fid @ close-file throw
  ;
: read_file  ( -- a u )
  \ Get the content of the open file (only its first line).
  \ a u = file content (first line)
  line_buffer dup max_line fid @ read-line throw and
  ;
: content  ( a1 u1 -- a2 u2 )
  \ Get file content; an alternative to 'slurp-file'.
  \ a1 u1 = file name
  \ a2 u2 = file content (first line)
  open_file read_file close_file
  ;

[then]

: .battery
  \ Print the capacity of the battery.
  s" battery=" type
  capacity_file content type cr
  ;
: .ac_line
  \ Print the status of the charger.
  s" ac_line=" type
  status_file content s" Charging" compare
  if  s" off"  else  s" on"  then  type cr
  ;

.battery .ac_line bye

Downloads

The installation instructions are in the source.

Related pages

Pandbatt
Forth program to show the status of the Pandora's battery.