rename bash → shell v_0.1
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 05 Nov 2016 17:38:05 +0100
branchv_0.1
changeset 2ec6e1bf8d53b
parent 1 4950117e9c11
child 3 34e954f3ed1c
rename bash → shell
bash/gpio.sh
shell/gpio.sh
     1.1 --- a/bash/gpio.sh	Sat Nov 05 17:34:34 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,108 +0,0 @@
     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 -
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/shell/gpio.sh	Sat Nov 05 17:38:05 2016 +0100
     2.3 @@ -0,0 +1,108 @@
     2.4 +#!/bin/bash
     2.5 +
     2.6 +# GPIO.sh
     2.7 +# Copyright © 2016 František Kučera (frantovo.cz)
     2.8 +#
     2.9 +# This program is free software: you can redistribute it and/or modify
    2.10 +# it under the terms of the GNU General Public License as published by
    2.11 +# the Free Software Foundation, either version 3 of the License, or
    2.12 +# (at your option) any later version.
    2.13 +#
    2.14 +# This program is distributed in the hope that it will be useful,
    2.15 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.16 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.17 +# GNU General Public License for more details.
    2.18 +#
    2.19 +# You should have received a copy of the GNU General Public License
    2.20 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.21 +
    2.22 +# GPIO.sh is a set of functions for controlling GPIO pins of Raspberry Pi or similar computer
    2.23 +
    2.24 +_gpio_valid_port() {
    2.25 +	case $1 in 0|1|4|7|8|9|10|11|14|15|17|18|21|22|23|24|25)
    2.26 +		return 0;;
    2.27 +	*)
    2.28 +		return 1;;
    2.29 +	esac
    2.30 +}
    2.31 +
    2.32 +# export -f _gpio_valid_port
    2.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'
    2.34 +
    2.35 +
    2.36 +_gpio_log_error() {
    2.37 +	# TODO: $(date --rfc-3339=seconds) or ● ?
    2.38 +	echo -e "\e[1;31m$1\e[0m"; > /dev/stderr;
    2.39 +}
    2.40 +
    2.41 +_gpio_log_warning() {
    2.42 +	echo -e "\e[2;33m$1 \e[0m"; > /dev/stderr;
    2.43 +}
    2.44 +
    2.45 +
    2.46 +# open (export) the port and set its direction
    2.47 +# parameters:
    2.48 +#	port number
    2.49 +#	direction: in/out
    2.50 +gpio_open() {
    2.51 +	local port="$1";
    2.52 +	local direction="$2";
    2.53 +	if [ $# -ne 2 ] || [[ "$direction" != @("in"|"out") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <in|out>"; return 1; fi
    2.54 +	if ! _gpio_valid_port "$port";                          then _gpio_log_error "invalid port number: $port";            return 1; fi;
    2.55 +	
    2.56 +	if [ -d  "/sys/class/gpio/gpio$port" ]; then 
    2.57 +		_gpio_log_warning "port $port is already exported → will just set direction: $direction";
    2.58 +	else
    2.59 +		echo "$port" > "/sys/class/gpio/export" || return 1;
    2.60 +	fi;
    2.61 +	
    2.62 +	echo "$direction" > "/sys/class/gpio/gpio$1/direction";
    2.63 +}
    2.64 +
    2.65 +
    2.66 +# close (unexport) the port and set its direction
    2.67 +# parameters:
    2.68 +#	port number
    2.69 +gpio_close() {
    2.70 +	local port="$1";
    2.71 +	if [ $# -ne 1 ];               then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi
    2.72 +	if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port";   return 1; fi;
    2.73 +	
    2.74 +	if [ -d  "/sys/class/gpio/gpio$port" ]; then 
    2.75 +		echo "$port" > "/sys/class/gpio/unexport";
    2.76 +	else
    2.77 +		_gpio_log_error "port $port is not open";
    2.78 +		return 1;
    2.79 +	fi;
    2.80 +}
    2.81 +
    2.82 +# reads value of given port
    2.83 +# parameters:
    2.84 +#	port number
    2.85 +gpio_read() {
    2.86 +	local port="$1";
    2.87 +	if [ $# -ne 1 ];               then _gpio_log_error "usage: ${FUNCNAME[0]} <port>"; return 1; fi
    2.88 +	if ! _gpio_valid_port "$port"; then _gpio_log_error "invalid port number: $port";   return 1; fi;
    2.89 +	
    2.90 +	if ! cat "/sys/class/gpio/gpio$port/value"; then
    2.91 +		_gpio_log_error "unable to read value of port $port";
    2.92 +		return 1;
    2.93 +	fi
    2.94 +}
    2.95 +
    2.96 +# writes the value to given port
    2.97 +# parameters:
    2.98 +#	port number
    2.99 +#	value: 0/1
   2.100 +gpio_write() {
   2.101 +	local port="$1";
   2.102 +	local value="$2";
   2.103 +	if [ $# -ne 2 ] || [[ "$value" != @("0"|"1") ]]; then _gpio_log_error "usage: ${FUNCNAME[0]} <port> <0|1>"; return 1; fi
   2.104 +	if ! _gpio_valid_port "$port";                          then _gpio_log_error "invalid port number: $port";  return 1; fi;
   2.105 +	
   2.106 +	if ! echo "$value" > "/sys/class/gpio/gpio$port/value"; then
   2.107 +		_gpio_log_error "unable to write value $value to port $port";
   2.108 +		return 1;
   2.109 +	fi
   2.110 +}
   2.111 +