ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 1 | #!/bin/bash |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 2 | |
| 3 | # shellcheck source=/dev/null |
ThuBaNguyen | 3310917 | 2021-07-02 15:39:57 +0700 | [diff] [blame] | 4 | source /usr/sbin/gpio-defs.sh |
ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 5 | |
| 6 | function gpio_number() { |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 7 | GPIO_BASE=$(cat /sys/class/gpio/gpiochip"$GPIO_CHIP0_BASE"/base) |
| 8 | echo $((GPIO_BASE + $1)) |
ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | # Configure GPIO as output and set its value |
| 12 | function gpio_configure_output() { |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 13 | gpioId=$(gpio_number "$1") |
| 14 | echo "$gpioId" > /sys/class/gpio/export |
| 15 | echo out > /sys/class/gpio/gpio"${gpioId}"/direction |
| 16 | echo "$2" > /sys/class/gpio/gpio"${gpioId}"/value |
| 17 | echo "$gpioId" > /sys/class/gpio/unexport |
ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | function gpio_get_val() { |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 21 | gpioId=$(gpio_number "$1") |
| 22 | echo "$gpioId" > /sys/class/gpio/export |
| 23 | cat /sys/class/gpio/gpio"$gpioId"/value |
| 24 | echo "$gpioId" > /sys/class/gpio/unexport |
ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | # Configure GPIO as input |
| 28 | function gpio_configure_input() { |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 29 | gpioId=$(gpio_number "$1") |
| 30 | echo "$gpioId" > /sys/class/gpio/export |
| 31 | echo "in" > /sys/class/gpio/gpio"${gpioId}"/direction |
| 32 | echo "$gpioId" > /sys/class/gpio/unexport |
ThuBaNguyen | 7b45282 | 2021-06-06 14:36:07 +0700 | [diff] [blame] | 33 | } |