# simbamon.conf
#
# Configuration variables for the SimBaMon simple battery monitor daemon.
# This code is copyright Hamish Cunningham and the University of Sheffield
# and is licenced under GPL 3 or any later version.

# how often should we check battery state? (in seconds, approximately)
MONITOR_FREQUENCY=2

# how long to pause between (non-critical) warnings? (in seconds, approximately)
WARNING_INTERVAL=60

# how often should we log a routine message? (in multiples of
# MONITOR_FREQUENCY)
LOG_INTERVAL=1000

# battery levels; the MoPi board uses 3 bits by default
# we avoid the 0 state as this is indistinguishable from unplugging the unit!
#
# quoting the MoPi system docs:
#7 battery full [LED blue];  level above 7.5V
#6 battery good [LED green]; level 7.5V-7.0V
#5 battery good [LED green]; level 7.0V-6.5V
#4 battery low: warning message [LED red];                      level 6.5V-6.2V
#3 battery critical: "urgent" warning message [LED red];        level 6.2V-6.0V
#2 lowest battery level: shutdown, no delay [LED flashing red]; level <= 6.0V
#1 power off button pressed: shut down with no delay [LED flashing red]
#0 unused to avoid confusion with non-functional gpio
#
BAT_FULL=7
BAT_WARNING=4
BAT_CRITICAL=3
BAT_SHUTDOWN=2
POWER_OFF=1

# how long to wait before starting work? (in seconds)
BOOT_DELAY=30

# file used as indicator that we are running for the first time after boot
PREVIOUSLY_RUN_INDICATOR=/tmp/${NAME}-previously-run.txt

# how long to wait before shutting down at critical level? (in seconds)
SHUT_DELAY=30

# which GPIO pins we're using to get levels from the board
IO_A=6
IO_B=5
IO_C=4

# debug and simulation modes: 
# don't actually call shutdown;
# redefine gpio to take input from a simulation file
# (which is expected to have a single line of the form nnn, in binary);
# also sets logging and delay levels short
if [ "$DEBUG" = on ]
then
  SHUT_DELAY=5
  LOG_INTERVAL=3
  WARNING_INTERVAL=10
  SHUTDOWN='echo "shutdown -h now"; sleep 10'
fi
if [ "$SIMUL" = on ]
then
  SIMULATION_DATA=/tmp/simbamon-simulation.txt
  gpio() {
    [ "$1" = "read" ] || return 0
    if [ -r "$SIMULATION_DATA" ] 
    then
      CUT_INDEX=`expr $2 + 1` # cut indexes from 1, gpio from 0
      cut -c $CUT_INDEX $SIMULATION_DATA
    else
      echo 0 # default gpio return value
    fi
  }
fi
