#!/bin/bash

FSV=23.9883

FSR=40
Rsense=10
let FSC=FSR/Rsense

function pvi_init() {
cssc=`i2cget -y 8 0x4c 0x51 b` 
#echo "cssc51:${cssc}"

i2cset -y 8 0x4c 0x51  0x02 b

cssc=`i2cget -y 8 0x4c 0x51 b` 
#echo "cssc51:${cssc}"
}


function pvi_temp_get() {
temp0=`i2cget -y 8 0x4c 0x0 b` 
temp=`awk -n -v T=$temp0 'BEGIN{printf("%d", T)}'`
eval $1=$temp
}

function pvi_current_get() {
VsenseH=`i2cget -y 8 0x4c 0x54 b` 
VsenseL=`i2cget -y 8 0x4c 0x55 b`  
Vsense=`awk -n -v H=$VsenseH -v L=$VsenseL 'BEGIN{printf("%d", H * 16 + rshift(L,4))}'`

#echo "Vsense: ${VsenseH} ${VsenseL} ${Vsense}"

Isource=`awk  -v V=$Vsense -v FSC=$FSC 'BEGIN{printf("%.2f", FSC * V / 2047)}'`
#echo "Isource ${Isource}A"
eval $1=$Isource
}

function pvi_volt_get() {
VsourceH=`i2cget -y 8 0x4c 0x58 b` 
VsourceL=`i2cget -y 8 0x4c 0x59 b`
Vsource=`awk -n -v H=$VsourceH -v L=$VsourceL 'BEGIN{printf("%d", H * 16 + rshift(L,4))}'`
#echo "Vsource: ${VsourceH} ${VsourceL} ${Vsource}"

Vdd=`awk -v V=$Vsource -v FSV=$FSV 'BEGIN{printf("%.2f", FSV * V / 4094)}'`
#echo "Vdd ${Vdd}V"
eval $1=$Vdd
}


function pvi_pratio_get() {
PratioH=`i2cget -y 8 0x4c 0x5b b` 
PratioL=`i2cget -y 8 0x4c 0x5c b`
Pratio=`awk -n -v H=$PratioH -v L=$PratioL 'BEGIN{printf("%d", H * 256 + L)}'`
#echo "Pratio: ${PratioH} ${PratioL} ${Pratio}"

Psource=`awk -v P=$Pratio -v FSV=$FSV  -v FSC=$FSC 'BEGIN{printf("%.2f", FSV * FSC * P / 65536)}'`
#echo "Psource ${Psource}W"
eval $1=$Psource
}


#pvi_init

#pvi_temp_get temp
#pvi_volt_get Vdd
#pvi_pratio_get Psource
#echo *******************
#echo -e "I: ${Isource}     V: ${Vdd}     P: ${Psource}     T: ${temp}\n"

#echo *******************



 
