Skip to content

Instantly share code, notes, and snippets.

@mmoollllee
Last active June 17, 2024 13:47
Show Gist options
  • Select an option

  • Save mmoollllee/46aa20417576cd3eebb4d3fba16bc4e0 to your computer and use it in GitHub Desktop.

Select an option

Save mmoollllee/46aa20417576cd3eebb4d3fba16bc4e0 to your computer and use it in GitHub Desktop.

Revisions

  1. mmoollllee revised this gist Mar 9, 2024. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions witty_voltage.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # Read voltage from i2c if Wittypi is mounted
    # run as `bash witty_voltage.sh`
    # 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=$(i2cget -y $1 $2 $3)
    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 failed (result=$result), retrying $retry ..."
    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
    exit 0
  2. mmoollllee revised this gist Mar 9, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion witty_voltage.sh
    Original 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
    calc $(($i))+$(($d))/100
    exit
  3. mmoollllee revised this gist Feb 28, 2024. No changes.
  4. mmoollllee created this gist Feb 28, 2024.
    35 changes: 35 additions & 0 deletions witty_voltage.sh
    Original 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