Thursday, January 21, 2010

OpenBSD NC4200: Howto get battery status & put it on wmii's status bar.

Update: My Updated wmiirc Status Bar Script is the recent version, there's a bit of error in the script below so you guys better take a look at the most recent one. I'm keeping this post as an archive.

Hola. After searching for an answer, I finally found out how to get the battery status for my NC4200 laptop. In case you're wondering where to look for more information, this is what I look at:

man acpi
man sysctl


This is the command and output of my primary laptop's battery. If you're using an extended battery pack like me, you'll see hw.sensors.acpibat1 got listed too. Here I just list the primary battery only:

$ sysctl hw.sensors.acpibat0
hw.sensors.acpibat0.volt0=11.10 VDC (voltage)
hw.sensors.acpibat0.volt1=12.40 VDC (current voltage)
hw.sensors.acpibat0.amphour0=4.04 Ah (last full capacity)
hw.sensors.acpibat0.amphour1=0.20 Ah (warning capacity)
hw.sensors.acpibat0.amphour2=0.04 Ah (low capacity)
hw.sensors.acpibat0.amphour3=3.95 Ah (remaining capacity), OK
hw.sensors.acpibat0.raw0=0 (battery idle), OK
hw.sensors.acpibat0.raw1=0 (rate)


Not forgetting that I also need to take a look at the AC Adapter. So I just use:

$ sysctl hw.sensors.acpiac0
hw.sensors.acpiac0.indicator0=On (power supply)


Update: Here's my simple script to display the battery percentage. I shamelessly take bits from here, here (FreeBSD) and a few others. Also, man sed, man bash is also usefull.

Update: I finally got most of the status for the battery that I wanted.

Update: I now includes the cpu usage & free memory using top. Bear in mind this is my first try so I need to clean the script more. Here's the update script:

Update: I tested the script using my extended battery and I saw garbage being display if the extended battery was taken off. So now I have tweak the script and now hopefully it fixed the issue. I done quite some testing and it seems to be ok now.

Here's some information regarding what the status bar will display from left to right:

- Display the CPU usage by system
- Display the Free Physical Memory capacity
- Display the percentage of the primary battery remaining, behind it display "+" if it's charging, "-" if it's discharging, "!" if the battery is critical and nothing if the battery is idle. As I'm using the extended battery, this status bar will also display the extended battery remaining charges the same as the primary one.
- Display the AC Adapter status. "On" if it's on AC or "Off" if not.
- Display the full date.

Ok so I vim ~/.wmii-3.5/wmiirc and insert the script below just before the # Event processing entry.

######### Script starts

# My wmii Status Bar script
status() {
 # This is for the main battery, if there's none, just display nothing.
 bat0=$(sysctl -n hw.sensors.acpibat0)
 if [ "${bat0} != "" ]; then
  bat0full=$(sysctl -n hw.sensors.acpibat0.amphour0 | sed s/.A.*//)
  bat0warn=$(sysctl -n hw.sensors.acpibat0.amphour1 | sed s/.A.*//)
  bat0low=$(sysctl -n hw.sensors.acpibat0.amphour2 | sed s/.A.*//)
  bat0now=$(sysctl -n hw.sensors.acpibat0.amphour3 | sed s/.A.*//)

  # Get the battery capacity in percentage
  bat0cap=`echo "100 * ${bat0now} / ${bat0full}" | bc`
  bat0p="$bat0cap%"

  # Get the charging status of the battery, replace with symbols
  bat0s=$(sysctl -n hw.sensors.acpibat0.raw0 | sed 's/'0'.*//; s/'1'.*/-/; s/'2'.*/+/; s/'4'.*/!/')
 else
  bat0p=`echo ""`
  bat0s=`echo ""`
 fi

 # This is for extended battery, if it's installed, display the status. Else just display nothing.
 bat1=$(sysctl -n hw.sensors.acpibat1)
 if [ "${bat1} != "" ]; then
  bat1full=$(sysctl -n hw.sensors.acpibat1.amphour0 | sed s/.A.*//)
  bat1warn=$(sysctl -n hw.sensors.acpibat1.amphour1 | sed s/.A.*//)
  bat1low=$(sysctl -n hw.sensors.acpibat1.amphour2 | sed s/.A.*//)
  bat1now=$(sysctl -n hw.sensors.acpibat1.amphour3 | sed s/.A.*//)

  # Get the battery capacity in percentage
  bat1cap=`echo "100 * ${bat1now} / ${bat1full}" | bc`
  bat1p="$bat1cap%"

  # Get the charging status of the battery, replace with symbols
  bat1s=$(sysctl -n hw.sensors.acpibat1.raw0 | sed 's/'0'.*//; s/'1'.*/-/; s/'2'.*/+/; s/'4'.*/!/')
 else
  bat1p=`echo ""`
  bat1s=`echo ""`
 fi

 # Get the AC Adapter status
 ac0=$(sysctl -n hw.sensors.acpiac0.indicator0 | sed s/.'('.*//)

 # Get the free memory data using top
 memfree=$(top | grep Memory | sed 's/.*Free:.//; s/.Swap.*//')

 # Get the CPU usage via top
 cpuuse=$(top | grep 'CPU states:' | grep 's/.*nice..//; s/.sys.*//')

 # Display the status bar
 echo CPU: $cpuuse '|' Mem: $memfree '|' Bat: ${bat0p}${bat0s} ${bat1p}${bat1s} '|' AC: $ac0 '|' $(date)
}


######### Scripts ended

So after I refresh the wmiirc ([MOD]+[A], wmiirc), the status bar displays something like:

CPU: 6.5% | Mem: 847M | Bat: 98% 97%+ | AC: On | Fri Jan 22 12:18:28 MYT 2010

The 1st battery doesn't have any "+" or "-" symbol behind it because it's currently idle. The 2nd battery is currently charging as you can see by the "+" symbol behind it. With this script there's no longer junks being displayed if the battery was taken off. Oh yeah I still didn't use the $bat0warn and $bat0low yet. I'll figure out how to manipulate it so low battery will shutdown the machine. Do use the script if you find it interesting. If you has improvement, please share.

- Display the battery percentage with "+" is the battery is charging.
- Display "AC" text if the machine is on AC adapter.
- Auto shutdown the machine if the "low capacity" indicator is reached.
- Display the wireless status (iwi0, still having headache on getting it to work with dstumbler).
- Display the CPU status. (clean this up).
- Display the RAM status (free memory, size).
- Simplifies the script (yeah well I'm no programmer).

No comments:

6.5 amd64: Modify existing certbot certificates.

Hi, It's been quite some time eh. As you can see, I still upgrade my OpenBSD system regularly but currently I do not have the time to ...