-
-
Save jefflarkin/b2fcec3817ea5d85288f to your computer and use it in GitHub Desktop.
#!/bin/bash | |
#FIXME Add usage() function to improve documentation | |
# Enable exposure of the specified GPIO pin (0-8) | |
gpio_enable() | |
{ | |
if [[("$1" -lt 0) || ("$1" -gt 8)]] ; then | |
echo "Valid pins are 0-8" | |
return -1; | |
fi | |
PIN=$((408 + $1)); | |
sudo sh -c "echo $PIN > /sys/class/gpio/export" | |
} | |
# Disables exposure of the specified GPIO pin (0-8) | |
gpio_disable() | |
{ | |
if [[("$1" -lt 0) || ("$1" -gt 8)]] ; then | |
echo "Valid pins are 0-8" | |
return -1; | |
fi | |
PIN=$((408 + $1)); | |
sudo sh -c "echo $PIN > /sys/class/gpio/unexport" | |
} | |
# Sets the pin to input or output mode | |
gpio_mode() | |
{ | |
if [[("$1" -lt 0) || ("$1" -gt 8)]] ; then | |
echo "Valid pins are 0-8" | |
return -1; | |
fi | |
PIN=$((408 + $1)); | |
if [[ ! -d /sys/class/gpio/gpio$PIN ]] ; then | |
echo "GPIO$1 has not been enabled yet, please call gpio_enable" | |
return -1 | |
fi | |
MODE="" | |
if [[("$2" == "in") || ("$2" == "IN")]] ; then | |
MODE="in" | |
elif [[("$2" == "out") || ("$2" == "OUT")]] ; then | |
MODE="out" | |
fi | |
if [[ "$MODE" == "" ]] ; then | |
echo 'Valid modes are "in" or "out"' | |
return -1; | |
fi | |
sudo sh -c "echo $MODE > /sys/class/gpio/gpio$PIN/direction" | |
} | |
gpio_write() | |
{ | |
if [[("$#" -lt 2)]] ; then | |
echo "Usage: gpio_write pin value" | |
return -1 | |
fi | |
if [[("$1" -lt 0) || ("$1" -gt 8)]] ; then | |
echo "Valid pins are 0-8" | |
return -1; | |
fi | |
PIN=$((408 + $1)); | |
#FIXME - Accept more than one word | |
sudo sh -c "echo $2 > /sys/class/gpio/gpio$PIN/value" | |
} | |
gpio_read() | |
{ | |
if [[("$1" -lt 0) || ("$1" -gt 8)]] ; then | |
echo "Valid pins are 0-8" | |
return -1; | |
fi | |
PIN=$((408 + $1)); | |
cat /sys/class/gpio/gpio$PIN/value | |
} | |
# The MIT License (MIT) Copyright (c) 2016 Jeff Larkin | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. |
I have successfully had this script working. I wanted to go back to it to refresh my memorys but it is no longer working for me. What am I going wrong? I am now getting the following error:
chip@chip:~$ gpio_enable 0
sh: line 0: echo: write error: Invalid argument
On CHIP kernel 4.4, GPIO expander pins start at 1016
not 408
(@cornbreadheadman, it's maybe your problem: changing all 408
by 1016
works for me).
Maybe a bash jedi can check current kernel version and set this first pin number as a variable?
kernel_version=$(uname -r | cut -b 3)
Will store in $kernel_version the third character of "uname -r" (3 or 4, depending on kernel)
Even better. You can prepare your base label for current and future kernels.
From docs.getchip.com
Here is a bash script to compute the base (courtesy of bbs user @fordsfords)
LABEL_FILE=
grep -l pcf8574a /sys/class/gpio/*/*label
BASE_FILE=dirname $LABEL_FILE
/base
BASE=cat $BASE_FILE
Edit: Decided to RTFM and figured out my problem. Thanks for the guide!
sorry.. total noob here but when I try to run the git clone I keep getting
Permission denied (publickey).
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.
what step did I miss or what am I doing wrong?
check the kernel: "the GPIO expander pins start at 408 on 4.3, 1016 on 4.4.11, and 1013 on 4.4.13-ntc-mlc"
from: https://docs.getchip.com/chip.html#how-you-see-gpio
Aren't there only 8 pins? That would be 0-7 and not 0-8.
See https://docs.getchip.com/images/chip_pinouts.jpg
@gonzalo heads up on something that should affect your copy too.
Thanks for writing this Jeff! Unfortunately Gist doesn't let you do pull requests... but just letting you know if you want to copy changes from my fork I also added functions for controlling CSI pins.