nicsensor/nicsensor_v2/plugin_adc128.sh

177 lines
6.0 KiB
Bash
Raw Normal View History

2025-01-06 09:10:08 +08:00
#!/bin/sh
2025-03-13 16:34:05 +08:00
# ADC128 Division factor
votage_division_factor_0="1"
votage_division_factor_1="1"
votage_division_factor_2="1"
votage_division_factor_3="0.8"
votage_division_factor_4="0.6"
votage_division_factor_5="0.6"
votage_division_factor_6="0.2326"
votage_division_factor_7="1"
# ADC128 channel name
ADC128_Channel0_name="Channel0"
ADC128_Channel1_name="Channel1"
ADC128_Channel2_name="Channel2"
ADC128_Channel3_name="Channel3"
ADC128_Channel4_name="Channel4"
ADC128_Channel5_name="Channel5"
ADC128_Channel6_name="Channel6"
ADC128_Channel7_name="Channel7"
2025-02-21 11:10:37 +08:00
mode=$1
i2c_bus=$2
chip_slave=$3
server_platform=$4
server_type=$5
2025-02-21 11:10:37 +08:00
2025-03-13 16:34:05 +08:00
# Only for test purpose
test_data1=$2
test_data2=$3
test_factor=$4
2025-02-21 11:10:37 +08:00
path=`pwd`
fmt_print="${path}/format_print.sh"
i2c_script="${path}/i2c_${server_platform}.sh"
server_script="${path}/platform_${server_type}.sh"
2025-02-21 11:10:37 +08:00
2025-03-13 16:34:05 +08:00
REG_adc128_config="0x00"
REG_adc128_advance="0x0b"
REG_adc128_status="0x0c"
REG_adc128_ch0="0x20"
REG_adc128_ch1="0x21"
REG_adc128_ch2="0x22"
REG_adc128_ch3="0x23"
REG_adc128_ch4="0x24"
REG_adc128_ch5="0x25"
REG_adc128_ch6="0x26"
REG_adc128_ch7="0x27"
print_help(){
echo "---------------------------------------------------------"
echo "<option> read"
echo " Format : ./plugin_adc128.sh read [i2c-bus] [slave] [platform]"
echo " Example: ./plugin_adc128.sh read 13 0x1f m7"
echo "---------------------------------------------------------"
echo "<option> test"
echo " Format : ./plugin_adc128.sh test [data1] [data2] [factor]"
echo " Example: ./plugin_adc128.sh test 0xc7 0x00 0.6"
echo "---------------------------------------------------------"
echo "<option> help"
echo " Format : ./plugin_adc128.sh help"
echo " Example: ./plugin_adc128.sh help"
echo "---------------------------------------------------------"
}
# adc128 chip init if need
check_adc128_init(){
# Get Reg 0x00 status
res_adc128_status=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_config 1`
# format_log_print $INFO "REG adc128 STATUS : $res_adc128_status"
$fmt_print "log" "Info" "[plugin_adc128] REG adc128 STATUS : $res_adc128_status"
# if stauts is not 0x01 (Start Monitor) ,then do init
if [ "$res_adc128_status" != "0x01" ];then
$fmt_print "log" "Info" "[plugin_adc128] Start Init ADC128 Chip"
# Init ADC128 work as mode 1 (0x02)
res_adc128_advance=`$i2c_script $i2c_bus 2 $chip_slave "$REG_adc128_advance 0x02" 0`
# Set ADC128 on start (0x01)
res_adc128_setstart=`$i2c_script $i2c_bus 2 $chip_slave "$REG_adc128_config 0x01" 1`
$fmt_print "log" "Info" "[plugin_adc128] After Set status, the REG 0x00 value is $res_adc128_setstart "
fi
}
2025-01-06 09:10:08 +08:00
# process sensor data then print to console
# @Param 1 adc128 data high 8bit
# @Param 2 adc128 data low 8bit
# @Param 3 customization channel name
# @Param 4 division factor
convert_adc128_data(){
if [ "$3" = "disable" ];then
return
fi
2025-01-06 09:10:08 +08:00
# remove data prefix '0x'
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
merge_value="${hex_value1}${hex_value2}"
# bc calculator recognized upper case only, change data to upper case
upper_hex_value=$(echo "$merge_value" | awk '{
for(i=1; i<=length($0); i++){
if(tolower(substr($0,i,1)) ~ /^[a-f]$/)
printf toupper(substr($0,i,1));
else
printf substr($0,i,1);
}
print ""
}')
# calculate dec value then print it to console
dec_val=$(echo "ibase=16; $upper_hex_value" | bc)
volt=$(echo "scale=4; $dec_val / 16 / 4096 * 2.65 / $4" | bc)
format_volt=$(echo "$volt" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
echo "$3 : $format_volt v, hex value: $upper_hex_value"
}
2025-02-21 11:10:37 +08:00
read_adc128_data(){
2025-03-13 16:34:05 +08:00
# check if chip adc128 need init
check_adc128_init
res_ch0=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch0 2`
res_ch1=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch1 2`
res_ch2=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch2 2`
res_ch3=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch3 2`
res_ch4=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch4 2`
res_ch5=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch5 2`
res_ch6=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch6 2`
res_ch7=`$i2c_script $i2c_bus 1 $chip_slave $REG_adc128_ch7 2`
# After read option , reset pca9548
$server_script reset
2025-03-13 16:34:05 +08:00
# record i2c raw data to log
$fmt_print "log" "Info" "[plugin_adc128] channel0 : $res_ch0"
$fmt_print "log" "Info" "[plugin_adc128] channel1 : $res_ch1"
$fmt_print "log" "Info" "[plugin_adc128] channel2 : $res_ch2"
$fmt_print "log" "Info" "[plugin_adc128] channel3 : $res_ch3"
$fmt_print "log" "Info" "[plugin_adc128] channel4 : $res_ch4"
$fmt_print "log" "Info" "[plugin_adc128] channel5 : $res_ch5"
$fmt_print "log" "Info" "[plugin_adc128] channel6 : $res_ch6"
$fmt_print "log" "Info" "[plugin_adc128] channel7 : $res_ch7"
# start parse raw data
$fmt_print "console" "Info" "The ADC128 value is :"
convert_adc128_data $res_ch0 "$ADC128_Channel0_name" $votage_division_factor_0
convert_adc128_data $res_ch1 "$ADC128_Channel1_name" $votage_division_factor_1
convert_adc128_data $res_ch2 "$ADC128_Channel2_name" $votage_division_factor_2
convert_adc128_data $res_ch3 "$ADC128_Channel3_name" $votage_division_factor_3
convert_adc128_data $res_ch4 "$ADC128_Channel4_name" $votage_division_factor_4
convert_adc128_data $res_ch5 "$ADC128_Channel5_name" $votage_division_factor_5
convert_adc128_data $res_ch6 "$ADC128_Channel6_name" $votage_division_factor_6
convert_adc128_data $res_ch7 "$ADC128_Channel7_name" $votage_division_factor_7
}
test_adc128_value(){
convert_adc128_data $test_data1 $test_data2 "Volt" $test_factor
2025-02-21 11:10:37 +08:00
}
case "${mode}" in
"read")
read_adc128_data
;;
2025-03-13 16:34:05 +08:00
"test")
test_adc128_value
;;
"help")
print_help
;;
2025-02-21 11:10:37 +08:00
*)
$fmt_print "console" "Error" "[plugin_adc128]Unexpected Input Param : $mode"
2025-03-13 16:34:05 +08:00
print_help
2025-02-21 11:10:37 +08:00
;;
2025-03-13 16:34:05 +08:00
esac