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