#!/bin/sh
# Changing directory, to make i2cget call working in all conditions
cd /usr/sbin/

# Assuming 30 mOhm sense resistance
RS=20

# Collecting data  - from bq27200.sh by shadowjk, with few things added by me

AR=$(i2cget -y 2 0x55 0x02 w)
ARTTE=$(i2cget -y 2 0x55 0x04 w)
VOLT=$(i2cget -y 2 0x55 0x08 w)
TEMP=$(i2cget -y 2 0x55 0x06 w)
CSOC=$(i2cget -y 2 0x55 0x2c)
RSOC=$(i2cget -y 2 0x55 0x0b)
NAC=$(i2cget -y 2 0x55 0x0c w)
CACD=$(i2cget -y 2 0x55 0x0e w)
CACT=$(i2cget -y 2 0x55 0x10 w)
AI=$(i2cget -y 2 0x55 0x14 w)
TTF=$(i2cget -y 2 0x55 0x18 w)
TTE=$(i2cget -y 2 0x55 0x16 w)
FLAGS=$(i2cget -y 2 0x55 0x0A)
CYCL=$(i2cget -y 2 0x55 0x28 w)
CYCLTL=$(i2cget -y 2 0x55 0x2a w)
SI=$(i2cget -y 2 0x55 0x1A w)
STTE=$(i2cget -y 2 0x55 0x1C w)
MLI=$(i2cget -y 2 0x55 0x1e w)
MLTTE=$(i2cget -y 2 0x55 0x20 w)
LMD=$(i2cget -y 2 0x55 0x12 w)
ILMD=$(i2cget -y 2 0x55 0x76)
EDVF=$(i2cget -y 2 0x55 0x77)
EDV1=$(i2cget -y 2 0x55 0x78)
ISLC=$(i2cget -y 2 0x55 0x79)
DMFSD=$(i2cget -y 2 0x55 0x7A)
TAPER=$(i2cget -y 2 0x55 0x7B)
IMLC=$(i2cget -y 2 0x55 0x7D)

WCH=$(cat /sys/devices/platform/musb_hdrc/charger)

############## Calculate ############## - from bq27200.sh by shadowjk

CSOC=$(($CSOC))                      # CSOC Compensated state of charge %. CACT/LMD * 100
RSOC=$(($RSOC))                      # RSOC Relative state of charge %. NAC/LMD * 100
NAC=$(($NAC * 3570 / $RS / 1000))    # NAC Nominal available capaciy, mAh.
CACD=$(($CACD * 3570 / $RS / 1000))  # CACD Discharge rate compensated available capacity, mAh.
CACT=$(($CACT * 3570 / $RS / 1000))  # CACT Temperature compensated CACD, mAh.
AI=$(($AI * 3570 / $RS / 1000))      # AI Average (last 5.12 seconds) current, mA.
VOLT=$(($VOLT))                      # VOLT Battery voltage, mV.
TTF=$(($TTF))                        # TTF Time to Full minutes. 65535 if no charging.
TTE=$(($TTE))                        # TTE Time to Empty minutes. 65535 if charging.

F=$(($FLAGS))
FLAGS_CHARGE=$(($F/128))             # There is charging activity. AI is measuring charge current.
F=$(($F-$F/128*128))
FLAGS_NOACT=$(($F/64))               # No charge/discharge activity detected.
F=$(($F-$F/64*64))
FLAGS_IMIN=$(($F/32))                # Charge current has tapered off (battery charge is near/at completion)
F=$(($F-$F/32*32))
FLAGS_CI=$(($F/16))                  # Capacity inaccurate. >32 cycles since last learning cycle.
F=$(($F-$F/16*16))
FLAGS_CALIP=$(($F/8))                # External offset calibration in progress.
F=$(($F-$F/8*8))
FLAGS_VDQ=$(($F/4))                  # Valid discharge. All requirements met for learning the battery's capacity when reaching EDV1
F=$(($F-$F/4*4))
FLAGS_EDV1=$(($F/2))                 # First end of discharge-voltage flag. Battery voltage is at or below preprogrammed EDV1 threshold. If VDQ is 1, LMD is updated and VDQ set to 0.
F=$(($F-$F/2*2))
FLAGS_EDVF=$F                        # Final end of discharge-voltage flag. The battery has discharged to 0% threshold.
AR=$(($AR * 3570 / $RS / 1000))      # AR At-rate
ARTTE=$(($ARTTE))                    # At-rate time to empty
LMD=$(($LMD * 3570 / $RS / 1000))    # LM Last measured discharge, mAh.
SI=$(($SI * 3570/ $RS / 1000))       # SI Standby Current, mA.
STTE=$(($STTE))                      # STTE Time to empty at standby, minutes.
MLI=$(($MLI * 3570 / $RS / 1000))    # MLI Maximum Load Current, mA.
MLTTE=$(($MLTTE))                    # MLTTE Time to empty at maximum load, minutes.
CYCL=$(($CYCL))                      # CYCL Cycles since last learning cycle (last time LMD was updated)
CYCLTL=$(($CYCLTL))                  # CYCLTL Cycles since last full reset.

ILMD=$(($ILMD * 913920 / $RS / 1000)) # eeprom Initial Last Measured Discharge. LMD = ILMD if no valid learning cycle has been completed.
EDVF=$(($EDVF * 8 + 8*256))           # eeprom End of discharge voltage threshold.
EDV1=$(($EDV1 * 8 + 8*256))           # eeprom 6.25% Capacity voltage threshold.
ISLC=$(($ISLC * 7140 / $RS / 1000))   # eeprom Initial standby load current.

# DMF in bits 4:7
DMF=$(($DMFSD/16))
# SD in bits 0:3
SD=$(($DMFSD-$DMF*16))
DMF=$(($DMF * 4900))                 # eeprom Digital Magnitude Filter, nanoVolts
SD=$((1610 / $SD))                   # eeprom Self Discharge rate, thousandth of percent (1/1000 %) per day at 25 degrees celcius

AGELMD=$(($TAPER/128))               # eeprom Battery capacity aging estimation on/off
TAPER=$(($TAPER-$AGELMD*128))
TAPER=$(($TAPER * 228000 / $RS / 1000))  # eeprom Taper current mA

IMLC=$(($IMLC * 457000 / $RS / 1000))    # eeprom Initial Max Load Current


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

# Initialize variable
CH="Absent"
STS="Discharging"
RT=$(($TTE))

# Hidden line - empty line, will contain wall charger and IMIN data, when charging = 1
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 [ $FLAGS_CHARGE == 1 ] ; then
      STS="Charging"
      RT=$(($TTF))

# Setting Hidden line, to show previously hidden data, only when charging
      HL="
IMIN: $FLAGS_IMIN        Wall Charger: $CH"
   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 * 250 / 1000 - 273))°C
Status: $STS
Current: $AI mA
Remaining Time: $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.