blob: 063cc55c9cce79247724595fbc3be4c9a8ea848f [file] [log] [blame]
Lancelot Kao96a7ee32021-02-22 18:50:48 -06001#!/bin/bash
2
Charles Boyerd7d34232022-02-09 08:32:22 -06003# get_gpio_num
4# Dynamically obtains GPIO number from chip base and I2C expanders through line name
5# line-name
6function 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 Kao96a7ee32021-02-22 18:50:48 -060024# set_gpio_ctrl
Charles Boyerd7d34232022-02-09 08:32:22 -060025# line-name, high(1)/low(0)
Lancelot Kao96a7ee32021-02-22 18:50:48 -060026function set_gpio_ctrl() {
Charles Boyerd7d34232022-02-09 08:32:22 -060027 #shellcheck disable=SC2046
28 gpioset $(gpiofind "$1")="$2"
Lancelot Kao96a7ee32021-02-22 18:50:48 -060029}
30
31# get_gpio_ctrl
Charles Boyerd7d34232022-02-09 08:32:22 -060032# line-name
Lancelot Kao96a7ee32021-02-22 18:50:48 -060033function get_gpio_ctrl() {
Charles Boyerd7d34232022-02-09 08:32:22 -060034 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 Kao96a7ee32021-02-22 18:50:48 -060038}