Last active
June 17, 2024 13:47
-
-
Save mmoollllee/46aa20417576cd3eebb4d3fba16bc4e0 to your computer and use it in GitHub Desktop.
Revisions
-
mmoollllee revised this gist
Mar 9, 2024 . 1 changed file with 11 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ #!/bin/bash # Read voltage from i2c if Wittypi is mounted # run with `bash witty_voltage.sh` readonly I2C_MC_ADDRESS=0x08 readonly I2C_VOLTAGE_IN_I=1 @@ -12,15 +12,17 @@ i2c_read() if [ $# -gt 3 ]; then retry=$4 fi local result=$(/usr/sbin/i2cget -y $1 $2 $3) if [[ "$result" =~ ^0x[0-9a-fA-F]{2}$ ]]; then echo $result; else retry=$(( $retry + 1 )) if [ $retry -ne 4 ]; then sleep 1 i2c_read $1 $2 $3 $retry else echo "Error reading I2C value" >&2 return 1 fi fi } @@ -32,5 +34,10 @@ calc() i=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_I) d=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_D) if [[ -z $i ]] || [[ -z $d ]]; then echo "Failed to read voltage values." exit 1 fi calc $(($i))+$(($d))/100 exit 0 -
mmoollllee revised this gist
Mar 9, 2024 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,4 +32,5 @@ calc() i=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_I) d=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_D) calc $(($i))+$(($d))/100 exit -
mmoollllee revised this gist
Feb 28, 2024 . No changes.There are no files selected for viewing
-
mmoollllee created this gist
Feb 28, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ #!/bin/bash # Read voltage from i2c if Wittypi is mounted # run as `bash witty_voltage.sh` readonly I2C_MC_ADDRESS=0x08 readonly I2C_VOLTAGE_IN_I=1 readonly I2C_VOLTAGE_IN_D=2 i2c_read() { local retry=0 if [ $# -gt 3 ]; then retry=$4 fi local result=$(i2cget -y $1 $2 $3) if [[ "$result" =~ ^0x[0-9a-fA-F]{2}$ ]]; then echo $result; else retry=$(( $retry + 1 )) if [ $retry -ne 4 ]; then sleep 1 #"I2C read $1 $2 $3 failed (result=$result), retrying $retry ..." i2c_read $1 $2 $3 $retry fi fi } calc() { awk "BEGIN { print $*}"; } i=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_I) d=$(i2c_read 0x01 $I2C_MC_ADDRESS $I2C_VOLTAGE_IN_D) calc $(($i))+$(($d))/100