#!/bin/sh
# Changing directory, to make bq27x00 calls working in all conditions
cd /sys/class/power_supply/bq27200-0/

# Collecting data from bq27x00_battery module

VLT=$(cat voltage_now)                   # Voltage in μV
CSOC=$(cat capacity)                     # State of charge in percents
CCT=$(cat charge_now)                    # Current capacity in μAh
LAMD=$(cat charge_full)                  # Maximum capacity in μAh
TMP=$(cat temp)                          # Temperature in teenths of °C
STS=$(cat status)                        # Charging/discharging status
CURN=$(cat current_now)                  # Current now in μA
FL=$(grep 0x0a= registers | cut -d= -f2)

WCH=$(cat /sys/devices/platform/musb_hdrc/charger) # wallcharger absent/present

# Calculating values

VOLT=$(($VLT / 1000))      # Voltage in mV
CACT=$(($CCT / 1000))      # Currently available capacity in mAh
LMD=$(($LAMD / 1000))      # Full capacity in mAh
TEMP=$(($TMP / 10))        # Temperature in °C
AI=$(($CURN / 1000))       # Actual current in mA

# Calculating flags (flag explanation in comments based on bq27200.sh script by ShadowJK)

FLAGS_VDQ="$(( ( $FL  & 4 ) / 4 ))"         # Valid discharge quality. All requirements met for learning the battery's capacity, upon reaching EDV1 (<3248 mV for 15 seconds).
FLAGS_IMIN="$(($(($FL-$FL/64*64))/32))"     # Charge current has tapered off (battery charge is near/at completion).
FLAGS_CI="$(($(($FL-$FL/32*32))/16))"       # Capacity inaccurate. >32 cycles since last learning cycle, or learning cycle never performed.


############## Preparing data for displaying ##############

# Initialize variable
CH="Absent"
RT=""

# Hidden line - will contain wall charger and IMIN data, when charging = 1, or average time to empty, otherwise.
HL=""

# Setting script to print correct wall charger status
   if [ $WCH == 1 ] ; then
      CH="Present"
   fi

# Setting script to print correct data depending on charging status
   if [ $STS == Charging  ] ; then
      # Collecting and calculating data from bq27x00_battery module
      STTF=$(cat time_to_full_now)          # Time to full in seconds
      TTF=$(($STTF / 60))                   # Time to full in minutes
      RT=$TTF

      # Setting Hidden line, to show previously hidden data, only when charging
      HL="IMIN: $FLAGS_IMIN        Wall Charger: $CH"

      # Multiplying current by -1, otherwise, it would be negative number
      AI=$((AI * -1))
   else
      # Collecting and calculating data from bq27x00_battery module
      STTE=$(cat time_to_empty_now)         # Time to empty in seconds
      STTEA=$(cat time_to_empty_avg)        # Average time to empty in seconds
      TTE=$(($STTE / 60))                   # Time to empty in minutes
      TTEA=$(($STTEA / 60))                 # Average time to empty in minutes
      RT=$TTE
      # Setting Hidden line, to show average time to empty, only when discharging
      HL="Average Remaining Time: $TTEA minutes"
   fi

############## Printing output ##############
o=org
f=freedesktop
n=Notifications
run-standalone.sh dbus-send --type=method_call --dest=$o.$f.$n /$o/$f/$n $o.$f.$n.SystemNoteDialog string:"Voltage: $VOLT mV
SoC: $CSOC%
Charge: $CACT mAh
Full Charge: $LMD mAh
Temperature: $TEMP°C
Status: $STS
Current: $AI mA
Remaining Time @$AI mA: $RT minutes
$HL
Calibration needed: $FLAGS_CI        VDQ: $FLAGS_VDQ" uint32:0 string:""

# thanks to Nicolai, for teaching me about displaying output as notification.
# huge thanks ShadowJK, I would not be able to get even close to starting this, without Your bq27200.sh state-of-art script.
# enormous thanks for MAG and peterleinchen for resolving 'sudo' issue.
# thanks to merlin1991 for teching me how to package things for debhelper.
# thanks to Pali and freemangordon for bringing us true FOSS BME replacement in kernel and userland - you guys rox!
