Skip to content

Instantly share code, notes, and snippets.

@pfeerick
Last active August 24, 2019 03:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pfeerick/05e5715733f00dcf303636c80abff598 to your computer and use it in GitHub Desktop.
Save pfeerick/05e5715733f00dcf303636c80abff598 to your computer and use it in GitHub Desktop.
Colourful Battery Status script for Pine64 SOC board
#!/bin/bash
# See https://github.com/pfeerick/pine64-scripts/blob/master/pine64-battery-colour.sh
# for updated versions of this script.
SWITCH="\033["
NORMAL="${SWITCH}0m"
RED="${SWITCH}1;31m"
GREEN="${SWITCH}1;32m"
YELLOW="${SWITCH}1;33m"
PURPLE="${SWITCH}1;35m"
BLUE="${SWITCH}1;34m"
CYAN="${SWITCH}1;36m"
BATT_PRESENT=$(</sys/class/power_supply/battery/present)
if [ "$BATT_PRESENT" = "1" ]; then
BATT_STATUS=$(</sys/class/power_supply/battery/status)
BATT_VOLTAGE=$(</sys/class/power_supply/battery/voltage_now)
BATT_VOLTAGE=$(echo " (($BATT_VOLTAGE/10000)*0.01 ) "|bc)
BATT_CURRENT=$(</sys/class/power_supply/battery/current_now)
((BATT_CURRENT = BATT_CURRENT / 1000))
BATT_CAPACITY=$(</sys/class/power_supply/battery/capacity)
BATT_HEALTH=$(</sys/class/power_supply/battery/health)
echo -e "${PURPLE}Pine64${NORMAL} reports ${GREEN}battery detected!${NORMAL}"
echo -e "${YELLOW}Status:${NORMAL}" $BATT_STATUS
echo -e "${YELLOW}Voltage:${NORMAL}" $BATT_VOLTAGE"v"
echo -e "${YELLOW}Current:${NORMAL}" $BATT_CURRENT"ma"
echo -e "${YELLOW}Capacity:${NORMAL}" $BATT_CAPACITY"%"
echo -e "${YELLOW}Health:${NORMAL}" $BATT_HEALTH
else
echo -e "${PURPLE}Pine64${NORMAL} reports ${RED}no battery connected!${NORMAL}"
fi
@pfeerick
Copy link
Author

pfeerick commented Dec 7, 2016

Currently depends on bc to convert the voltage integer to a floating point (decimal) number. Will be fixed in a later revision (maybe!). To install this dependency, run sudo apt-get install bc from your terminal or command line.

You can download this directly to your pine64 using (wget raw-view-url):

wget https://gist.github.com/pfeerick/05e5715733f00dcf303636c80abff598/raw/9bfca4be89ed219658426701b702a2663329f2df/pine64-battery.sh

Don't forget to make it executable after downloading!
chmod +x pine64-battery.sh

@gadLinux
Copy link

Great! It works perfectly. Thank you a lot

@brianredbeard
Copy link

FYI, I've got an updated fork in the tree which abstracts the battery path into a single variable (i noticed that in a 4.19.x kernel it used a different path).

@pfeerick
Copy link
Author

Thanks for that. 😄 This script is now part of a collection hosted at https://github.com/pfeerick/pine64-scripts/, and I've just added path detection based on your comment. Please let me know if there are any other changes needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment