blob: c8721fe9762a0ec395c8948aa75695d8d1b38743 [file] [log] [blame]
ThuBaNguyen7b452822021-06-06 14:36:07 +07001#!/bin/bash
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +00002
3# shellcheck source=/dev/null
ThuBaNguyen33109172021-07-02 15:39:57 +07004source /usr/sbin/gpio-defs.sh
ThuBaNguyen7b452822021-06-06 14:36:07 +07005
6function gpio_number() {
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +00007 GPIO_BASE=$(cat /sys/class/gpio/gpiochip"$GPIO_CHIP0_BASE"/base)
8 echo $((GPIO_BASE + $1))
ThuBaNguyen7b452822021-06-06 14:36:07 +07009}
10
11# Configure GPIO as output and set its value
12function gpio_configure_output() {
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000013 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
ThuBaNguyen7b452822021-06-06 14:36:07 +070018}
19
20function gpio_get_val() {
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000021 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
ThuBaNguyen7b452822021-06-06 14:36:07 +070025}
26
27# Configure GPIO as input
28function gpio_configure_input() {
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000029 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
ThuBaNguyen7b452822021-06-06 14:36:07 +070033}