shell/gpio.sh
branchv_0.1
changeset 2 ec6e1bf8d53b
parent 0 6145d960b117
child 3 34e954f3ed1c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/shell/gpio.sh	Sat Nov 05 17:38:05 2016 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +#!/bin/bash
     1.5 +
     1.6 +# GPIO.sh
     1.7 +# Copyright © 2016 František Kučera (frantovo.cz)
     1.8 +#
     1.9 +# This program is free software: you can redistribute it and/or modify
    1.10 +# it under the terms of the GNU General Public License as published by
    1.11 +# the Free Software Foundation, either version 3 of the License, or
    1.12 +# (at your option) any later version.
    1.13 +#
    1.14 +# This program is distributed in the hope that it will be useful,
    1.15 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.17 +# GNU General Public License for more details.
    1.18 +#
    1.19 +# You should have received a copy of the GNU General Public License
    1.20 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.21 +
    1.22 +# GPIO.sh is a set of functions for controlling GPIO pins of Raspberry Pi or similar computer
    1.23 +
    1.24 +_gpio_valid_port() {
    1.25 +	case $1 in 0|1|4|7|8|9|10|11|14|15|17|18|21|22|23|24|25)
    1.26 +		return 0;;
    1.27 +	*)
    1.28 +		return 1;;
    1.29 +	esac
    1.30 +}
    1.31 +
    1.32 +# export -f _gpio_valid_port
    1.33 +# 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'
    1.34 +
    1.35 +
    1.36 +_gpio_log_error() {
    1.37 +	# TODO: $(date --rfc-3339=seconds) or ● ?
    1.38 +	echo -e "\e[1;31m$1\e[0m"; > /dev/stderr;
    1.39 +}
    1.40 +
    1.41 +_gpio_log_warning() {
    1.42 +	echo -e "\e[2;33m$1 \e[0m"; > /dev/stderr;
    1.43 +}
    1.44 +
    1.45 +
    1.46 +# open (export) the port and set its direction
    1.47 +# parameters:
    1.48 +#	port number
    1.49 +#	direction: in/out
    1.50 +gpio_open() {
    1.51 +	local port="$1";
    1.52 +	local direction="$2";
    1.53 +	if [ $# -ne 2 ] || [[ "$direction" != @("in"|"out") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <in|out>"; return 1; fi
    1.54 +	if ! _gpio_valid_port "$port";                          then _gpio_log_error "invalid port number: $port";            return 1; fi;
    1.55 +	
    1.56 +	if [ -d  "/sys/class/gpio/gpio$port" ]; then 
    1.57 +		_gpio_log_warning "port $port is already exported → will just set direction: $direction";
    1.58 +	else
    1.59 +		echo "$port" > "/sys/class/gpio/export" || return 1;
    1.60 +	fi;
    1.61 +	
    1.62 +	echo "$direction" > "/sys/class/gpio/gpio$1/direction";
    1.63 +}
    1.64 +
    1.65 +
    1.66 +# close (unexport) the port and set its direction
    1.67 +# parameters:
    1.68 +#	port number
    1.69 +gpio_close() {
    1.70 +	local port="$1";
    1.71 +	if [ $# -ne 1 ];               then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi
    1.72 +	if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port";   return 1; fi;
    1.73 +	
    1.74 +	if [ -d  "/sys/class/gpio/gpio$port" ]; then 
    1.75 +		echo "$port" > "/sys/class/gpio/unexport";
    1.76 +	else
    1.77 +		_gpio_log_error "port $port is not open";
    1.78 +		return 1;
    1.79 +	fi;
    1.80 +}
    1.81 +
    1.82 +# reads value of given port
    1.83 +# parameters:
    1.84 +#	port number
    1.85 +gpio_read() {
    1.86 +	local port="$1";
    1.87 +	if [ $# -ne 1 ];               then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi
    1.88 +	if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port";   return 1; fi;
    1.89 +	
    1.90 +	if ! cat "/sys/class/gpio/gpio$port/value"; then
    1.91 +		_gpio_log_error "unable to read value of port $port";
    1.92 +		return 1;
    1.93 +	fi
    1.94 +}
    1.95 +
    1.96 +# writes the value to given port
    1.97 +# parameters:
    1.98 +#	port number
    1.99 +#	value: 0/1
   1.100 +gpio_write() {
   1.101 +	local port="$1";
   1.102 +	local value="$2";
   1.103 +	if [ $# -ne 2 ] || [[ "$value" != @("0"|"1") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <0|1>"; return 1; fi
   1.104 +	if ! _gpio_valid_port "$port";                          then _gpio_log_error "invalid port number: $port";  return 1; fi;
   1.105 +	
   1.106 +	if ! echo "$value" > "/sys/class/gpio/gpio$port/value"; then
   1.107 +		_gpio_log_error "unable to write value $value to port $port";
   1.108 +		return 1;
   1.109 +	fi
   1.110 +}
   1.111 +