author | František Kučera <franta-hg@frantovo.cz> |
Thu, 24 Oct 2019 22:03:40 +0200 | |
branch | v_0.1 |
changeset 5 | f176e4893d17 |
parent 3 | 34e954f3ed1c |
permissions | -rw-r--r-- |
franta-hg@0 | 1 |
#!/bin/bash |
franta-hg@0 | 2 |
|
franta-hg@0 | 3 |
# GPIO.sh |
franta-hg@0 | 4 |
# Copyright © 2016 František Kučera (frantovo.cz) |
franta-hg@0 | 5 |
# |
franta-hg@0 | 6 |
# This program is free software: you can redistribute it and/or modify |
franta-hg@0 | 7 |
# it under the terms of the GNU General Public License as published by |
franta-hg@5 | 8 |
# the Free Software Foundation, version 3 of the License. |
franta-hg@0 | 9 |
# |
franta-hg@0 | 10 |
# This program is distributed in the hope that it will be useful, |
franta-hg@0 | 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
franta-hg@0 | 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
franta-hg@0 | 13 |
# GNU General Public License for more details. |
franta-hg@0 | 14 |
# |
franta-hg@0 | 15 |
# You should have received a copy of the GNU General Public License |
franta-hg@0 | 16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
franta-hg@0 | 17 |
|
franta-hg@0 | 18 |
# GPIO.sh is a set of functions for controlling GPIO pins of Raspberry Pi or similar computer |
franta-hg@0 | 19 |
|
franta-hg@0 | 20 |
_gpio_valid_port() { |
franta-hg@0 | 21 |
case $1 in 0|1|4|7|8|9|10|11|14|15|17|18|21|22|23|24|25) |
franta-hg@0 | 22 |
return 0;; |
franta-hg@0 | 23 |
*) |
franta-hg@0 | 24 |
return 1;; |
franta-hg@0 | 25 |
esac |
franta-hg@0 | 26 |
} |
franta-hg@0 | 27 |
|
franta-hg@0 | 28 |
# export -f _gpio_valid_port |
franta-hg@0 | 29 |
# seq 0 30 | xargs -n1 bash -c 'printf "%4s" "$0: "; _gpio_valid_port $0 ; if [ $? -eq 0 ]; then echo "valid port number"; else echo; fi' |
franta-hg@0 | 30 |
|
franta-hg@0 | 31 |
|
franta-hg@0 | 32 |
_gpio_log_error() { |
franta-hg@0 | 33 |
# TODO: $(date --rfc-3339=seconds) or ● ? |
franta-hg@0 | 34 |
echo -e "\e[1;31m$1\e[0m"; > /dev/stderr; |
franta-hg@0 | 35 |
} |
franta-hg@0 | 36 |
|
franta-hg@0 | 37 |
_gpio_log_warning() { |
franta-hg@0 | 38 |
echo -e "\e[2;33m$1 \e[0m"; > /dev/stderr; |
franta-hg@0 | 39 |
} |
franta-hg@0 | 40 |
|
franta-hg@0 | 41 |
|
franta-hg@0 | 42 |
# open (export) the port and set its direction |
franta-hg@0 | 43 |
# parameters: |
franta-hg@0 | 44 |
# port number |
franta-hg@0 | 45 |
# direction: in/out |
franta-hg@0 | 46 |
gpio_open() { |
franta-hg@0 | 47 |
local port="$1"; |
franta-hg@0 | 48 |
local direction="$2"; |
franta-hg@0 | 49 |
if [ $# -ne 2 ] || [[ "$direction" != @("in"|"out") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <in|out>"; return 1; fi |
franta-hg@0 | 50 |
if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port"; return 1; fi; |
franta-hg@0 | 51 |
|
franta-hg@0 | 52 |
if [ -d "/sys/class/gpio/gpio$port" ]; then |
franta-hg@0 | 53 |
_gpio_log_warning "port $port is already exported → will just set direction: $direction"; |
franta-hg@0 | 54 |
else |
franta-hg@0 | 55 |
echo "$port" > "/sys/class/gpio/export" || return 1; |
franta-hg@0 | 56 |
fi; |
franta-hg@0 | 57 |
|
franta-hg@0 | 58 |
echo "$direction" > "/sys/class/gpio/gpio$1/direction"; |
franta-hg@0 | 59 |
} |
franta-hg@0 | 60 |
|
franta-hg@0 | 61 |
|
franta-hg@0 | 62 |
# close (unexport) the port and set its direction |
franta-hg@0 | 63 |
# parameters: |
franta-hg@0 | 64 |
# port number |
franta-hg@0 | 65 |
gpio_close() { |
franta-hg@0 | 66 |
local port="$1"; |
franta-hg@0 | 67 |
if [ $# -ne 1 ]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi |
franta-hg@0 | 68 |
if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port"; return 1; fi; |
franta-hg@0 | 69 |
|
franta-hg@0 | 70 |
if [ -d "/sys/class/gpio/gpio$port" ]; then |
franta-hg@0 | 71 |
echo "$port" > "/sys/class/gpio/unexport"; |
franta-hg@0 | 72 |
else |
franta-hg@0 | 73 |
_gpio_log_error "port $port is not open"; |
franta-hg@0 | 74 |
return 1; |
franta-hg@0 | 75 |
fi; |
franta-hg@0 | 76 |
} |
franta-hg@0 | 77 |
|
franta-hg@0 | 78 |
# reads value of given port |
franta-hg@0 | 79 |
# parameters: |
franta-hg@0 | 80 |
# port number |
franta-hg@0 | 81 |
gpio_read() { |
franta-hg@0 | 82 |
local port="$1"; |
franta-hg@0 | 83 |
if [ $# -ne 1 ]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi |
franta-hg@0 | 84 |
if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port"; return 1; fi; |
franta-hg@0 | 85 |
|
franta-hg@0 | 86 |
if ! cat "/sys/class/gpio/gpio$port/value"; then |
franta-hg@0 | 87 |
_gpio_log_error "unable to read value of port $port"; |
franta-hg@0 | 88 |
return 1; |
franta-hg@0 | 89 |
fi |
franta-hg@0 | 90 |
} |
franta-hg@0 | 91 |
|
franta-hg@0 | 92 |
# writes the value to given port |
franta-hg@0 | 93 |
# parameters: |
franta-hg@0 | 94 |
# port number |
franta-hg@0 | 95 |
# value: 0/1 |
franta-hg@0 | 96 |
gpio_write() { |
franta-hg@0 | 97 |
local port="$1"; |
franta-hg@0 | 98 |
local value="$2"; |
franta-hg@0 | 99 |
if [ $# -ne 2 ] || [[ "$value" != @("0"|"1") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <0|1>"; return 1; fi |
franta-hg@3 | 100 |
if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port"; return 1; fi; |
franta-hg@0 | 101 |
|
franta-hg@0 | 102 |
if ! echo "$value" > "/sys/class/gpio/gpio$port/value"; then |
franta-hg@0 | 103 |
_gpio_log_error "unable to write value $value to port $port"; |
franta-hg@0 | 104 |
return 1; |
franta-hg@0 | 105 |
fi |
franta-hg@0 | 106 |
} |
franta-hg@0 | 107 |