Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Charles Boyer | d7d3423 | 2022-02-09 08:32:22 -0600 | [diff] [blame^] | 3 | # get_gpio_num |
| 4 | # Dynamically obtains GPIO number from chip base and I2C expanders through line name |
| 5 | # line-name |
| 6 | function get_gpio_num() { |
| 7 | #shellcheck disable=SC2207 |
| 8 | CHIP_PIN=($(gpiofind "$1" | awk '{print substr ($1, 9 ), $2 }')) |
| 9 | #shellcheck disable=SC2128 |
| 10 | if [ -z "$CHIP_PIN" ]; then |
| 11 | echo "Could not find GPIO with name: $1" |
| 12 | return 1 |
| 13 | fi |
| 14 | |
| 15 | if [ "${CHIP_PIN[0]}" -gt 7 ]; then |
| 16 | BUS_ADDR=$(gpiodetect | grep gpiochip"${CHIP_PIN[0]}" | awk '{print substr($2, 2, length($2) - 2)}') |
| 17 | GPIO_BASE=$(cat /sys/bus/i2c/devices/"$BUS_ADDR"/gpio/*/base) |
| 18 | echo "$((GPIO_BASE+CHIP_PIN[1]))" |
| 19 | else |
| 20 | echo "$((CHIP_PIN[0]*32+CHIP_PIN[1]))" |
| 21 | fi |
| 22 | } |
| 23 | |
Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 24 | # set_gpio_ctrl |
Charles Boyer | d7d3423 | 2022-02-09 08:32:22 -0600 | [diff] [blame^] | 25 | # line-name, high(1)/low(0) |
Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 26 | function set_gpio_ctrl() { |
Charles Boyer | d7d3423 | 2022-02-09 08:32:22 -0600 | [diff] [blame^] | 27 | #shellcheck disable=SC2046 |
| 28 | gpioset $(gpiofind "$1")="$2" |
Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | # get_gpio_ctrl |
Charles Boyer | d7d3423 | 2022-02-09 08:32:22 -0600 | [diff] [blame^] | 32 | # line-name |
Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 33 | function get_gpio_ctrl() { |
Charles Boyer | d7d3423 | 2022-02-09 08:32:22 -0600 | [diff] [blame^] | 34 | GPIO_NUM=$(get_gpio_num "$1") |
| 35 | echo "$GPIO_NUM" > /sys/class/gpio/export |
| 36 | cat /sys/class/gpio/gpio"$GPIO_NUM"/value |
| 37 | echo "$GPIO_NUM" > /sys/class/gpio/unexport |
Lancelot Kao | 96a7ee3 | 2021-02-22 18:50:48 -0600 | [diff] [blame] | 38 | } |