Skip to content

Instantly share code, notes, and snippets.

@xtacocorex
Last active May 22, 2018 09:04
Show Gist options
  • Save xtacocorex/5bf765781b27c2c262cc to your computer and use it in GitHub Desktop.
Save xtacocorex/5bf765781b27c2c262cc to your computer and use it in GitHub Desktop.
CHIP Auto Shutdown based upon battery level
#!/bin/bash
# 2016 ROBERT WOLTERMAN (xtacocorex)
# WITH HELP FROM CHIP-hwtest/battery.sh
# THIS NEEDS TO BE RUN AS ROOT
# PROBABLY SET AS A CRON JOB EVERY 5 OR 10 MINUTES
# SIMPLE SCRIPT TO POWER DOWN THE CHIP BASED UPON BATTERY VOLTAGE
# CHANGE THESE TO CUSTOMIZE THE SCRIPT
MINVOLTAGELEVEL=2.0
MINCHARGECURRENT=10
# TALK TO THE POWER MANAGEMENT
i2cset -y -f 0 0x34 0x82 0xC3
# GET POWER OP MODE
POWER_OP_MODE=$(i2cget -y -f 0 0x34 0x01)
# SEE IF BATTERY EXISTS
BAT_EXIST=$(($(($POWER_OP_MODE&0x20))/32))
if [ $BAT_EXIST == 1 ]; then
BAT_VOLT_MSB=$(i2cget -y -f 0 0x34 0x78)
BAT_VOLT_LSB=$(i2cget -y -f 0 0x34 0x79)
BAT_BIN=$(( $(($BAT_VOLT_MSB << 4)) | $(($(($BAT_VOLT_LSB & 0x0F)) )) ))
BAT_VOLT=$(echo "($BAT_BIN*1.1)"|bc)
# CHECK BATTERY LEVEL AGAINST MINVOLTAGELEVEL
if [ $BAT_VOLT <= $MINVOLTAGELEVEL ]; then
# GET THE CHARGE CURRENT
BAT_ICHG_MSB=$(i2cget -y -f 0 0x34 0x7A)
BAT_ICHG_LSB=$(i2cget -y -f 0 0x34 0x7B)
BAT_ICHG_BIN=$(( $(($BAT_ICHG_MSB << 4)) | $(($(($BAT_ICHG_LSB & 0x0F)) )) ))
BAT_ICHG=$(echo "($BAT_ICHG_BIN*0.5)"|bc)
# IF CHARGE CURRENT IS LESS THAN MINCHARGECURRENT, WE NEED TO SHUTDOWN
if [ $BAT_ICHG <= $MINCHARGECURRENT ]; then
shutdown -h now
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment