105 lines
3.0 KiB
Bash
105 lines
3.0 KiB
Bash
#!/bin/sh
|
|
|
|
mode=$1
|
|
i2c_bus=$2
|
|
chip_slave=$3
|
|
server_platform=$4
|
|
server_type=$5
|
|
|
|
test_data1=$2
|
|
test_data2=$3
|
|
|
|
path=`pwd`
|
|
fmt_print="${path}/format_print.sh"
|
|
i2c_script="${path}/i2c_${server_platform}.sh"
|
|
server_script="${path}/platform_${server_type}.sh"
|
|
|
|
sensor_name="TMP112"
|
|
|
|
print_help(){
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> read"
|
|
echo " Format : ./plugin_tmp112.sh read [i2c-bus] [slave] [platform]"
|
|
echo " Example: ./plugin_tmp112.sh read 13 0x48 m7"
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> test"
|
|
echo " Format : ./plugin_tmp112.sh test [data1] [data2]"
|
|
echo " Example: ./plugin_tmp112.sh test 0x24 0x00"
|
|
echo "---------------------------------------------------------"
|
|
echo "<option> help"
|
|
echo " Format : ./plugin_tmp112.sh help"
|
|
echo " Example: ./plugin_tmp112.sh help"
|
|
echo "---------------------------------------------------------"
|
|
}
|
|
|
|
# @Param1 tmp112 data high 8bit
|
|
# @Param2 tmp112 data low 8bit
|
|
# @Param3 customization sensor name
|
|
convert_tmp112_data(){
|
|
if [ "$3" = "disable" ];then
|
|
return
|
|
fi
|
|
|
|
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 ""
|
|
}')
|
|
|
|
# change data from hex to dec
|
|
dec_val=$(echo "ibase=16; $upper_hex_value" | bc)
|
|
|
|
binary_number=$(echo "ibase=16;obase=2;$upper_hex_value" | bc)
|
|
bin_length=$(echo $binary_number | awk '{print length($0)}')
|
|
last_digit=$(echo $binary_number | cut -c $bin_length)
|
|
if [ $last_digit -eq 0 ];then
|
|
temp=$(echo "scale=4; $dec_val / 16 * 0.0625" | bc)
|
|
elif [ $last_digit -eq 1 ];then
|
|
temp=$(echo "scale=4; ( $dec_val - 1 ) / 8 * 0.0625" | bc)
|
|
fi
|
|
|
|
echo "$3 : $temp"
|
|
$fmt_print "log" "Info" "[plugin_tmp112] hex value : ${merge_value}"
|
|
$fmt_print "log" "Info" "[plugin_tmp112] binary_number : ${binary_number}"
|
|
$fmt_print "log" "Info" "[plugin_tmp112] bin_length : ${bin_length}"
|
|
$fmt_print "log" "Info" "[plugin_tmp112] last_digit : ${last_digit}"
|
|
}
|
|
|
|
read_tmp112_data(){
|
|
res=`$i2c_script $i2c_bus "1" "$chip_slave" "0x00" "2"`
|
|
|
|
# After read option , reset pca9548
|
|
$server_script reset
|
|
|
|
convert_tmp112_data $res "$sensor_name"
|
|
}
|
|
|
|
test_tmp112_data(){
|
|
convert_tmp112_data $test_data1 $test_data2 "Temp"
|
|
}
|
|
|
|
case "${mode}" in
|
|
"read")
|
|
read_tmp112_data
|
|
;;
|
|
"test")
|
|
test_tmp112_data
|
|
;;
|
|
"help")
|
|
print_help
|
|
;;
|
|
*)
|
|
$fmt_print "console" "Error" "[plugin_tmp112] Unexpected Input Param : $mode"
|
|
print_help
|
|
;;
|
|
esac
|