Skip to content

Instantly share code, notes, and snippets.

@cornbreadheadman
Forked from nhorvath/gpio.sh
Last active July 3, 2016 04:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cornbreadheadman/cda48dfb6000b9eadd2d21261698264a to your computer and use it in GitHub Desktop.
Save cornbreadheadman/cda48dfb6000b9eadd2d21261698264a to your computer and use it in GitHub Desktop.
BASH functions for using the C.H.I.P. GPIO pins.
#!/bin/bash
GPIO_OFFSET=1016
CSI_OFFSET=132
LCD_OFFSET=96
#FIXME Add usage() function to improve documentation
# Enable exposure of the specified GPIO pin (0-7)
gpio_enable()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($GPIO_OFFSET + $1));
sudo sh -c "echo $PIN > /sys/class/gpio/export"
}
# Disables exposure of the specified GPIO pin (0-7)
gpio_disable()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($GPIO_OFFSET + $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 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($GPIO_OFFSET + $1));
if [[ ! -d /sys/class/gpio/gpio$PIN ]] ; then
echo "GPIO$1 has not been enabled yet, please call gpio_enable"
return -1
fi
if [ $# -lt 2 ] ; then
cat /sys/class/gpio/gpio$PIN/direction
else
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"
fi
}
gpio_write()
{
if [[("$#" -lt 2)]] ; then
echo "Usage: gpio_write pin value"
return -1
fi
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($GPIO_OFFSET + $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 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($GPIO_OFFSET + $1));
cat /sys/class/gpio/gpio$PIN/value
}
# Enable exposure of the specified CSI pin (0-7)
csi_enable()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($CSI_OFFSET + $1));
sudo sh -c "echo $PIN > /sys/class/gpio/export"
}
# Disables exposure of the specified CSI pin (0-7)
csi_disable()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($CSI_OFFSET + $1));
sudo sh -c "echo $PIN > /sys/class/gpio/unexport"
}
# Sets the pin to input or output mode
csi_mode()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($CSI_OFFSET + $1));
if [[ ! -d /sys/class/gpio/gpio$PIN ]] ; then
echo "CSI$1 has not been enabled yet, please call csi_enable"
return -1
fi
if [ $# -lt 2 ] ; then
cat /sys/class/gpio/gpio$PIN/direction
else
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"
fi
}
csi_write()
{
if [[("$#" -lt 2)]] ; then
echo "Usage: csi_write pin value"
return -1
fi
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($CSI_OFFSET + $1));
#FIXME - Accept more than one word
sudo sh -c "echo $2 > /sys/class/gpio/gpio$PIN/value"
}
csi_read()
{
if [[("$1" -lt 0) || ("$1" -gt 7)]] ; then
echo "Valid pins are 0-7"
return -1;
fi
PIN=$(($CSI_OFFSET + $1));
cat /sys/class/gpio/gpio$PIN/value
}
# Enable exposure of the specified LCD pin (2-7, 10-15 & 18-23)
lcd_enable()
{
if [[("$1" -lt 2) || ("$1" -gt 23) && ("$1" -ne 8,9,16,17)]] ; then
echo "Valid pins are 2-7, 10-15 & 18-23"
return -1;
fi
PIN=$(($LCD_OFFSET + $1));
sudo sh -c "echo $PIN > /sys/class/gpio/export"
}
# Disables exposure of the specified LCD pin (2-7, 10-15 & 18-23)
lcd_disable()
{
if [[("$1" -lt 2) || ("$1" -gt 23) && ("$1" -ne 8,9,16,17)]] ; then
echo "Valid pins are 2-7, 10-15 & 18-23"
return -1;
fi
PIN=$(($LCD_OFFSET + $1));
sudo sh -c "echo $PIN > /sys/class/gpio/unexport"
}
# Sets the pin to input or output mode
lcd_mode()
{
if [[("$1" -lt 2) || ("$1" -gt 23) && ("$1" -ne 8,9,16,17)]] ; then
echo "Valid pins are 2-7, 10-15 & 18-23"
return -1;
fi
PIN=$(($LCD_OFFSET + $1));
if [[ ! -d /sys/class/gpio/gpio$PIN ]] ; then
echo "LCD-$1 has not been enabled yet, please call lcd_enable"
return -1
fi
if [ $# -lt 2 ] ; then
cat /sys/class/gpio/gpio$PIN/direction
else
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"
fi
}
lcd_write()
{
if [[("$#" -lt 2)]] ; then
echo "Usage: lcd_write pin value"
return -1
fi
if [[("$1" -lt 2) || ("$1" -gt 23) && ("$1" -ne 8,9,16,17)]] ; then
echo "Valid pins are 2-7, 10-15 & 18-23"
return -1;
fi
PIN=$(($LCD_OFFSET + $1));
#FIXME - Accept more than one word
sudo sh -c "echo $2 > /sys/class/gpio/gpio$PIN/value"
}
lcd_read()
{
if [[("$1" -lt 2) || ("$1" -gt 23) && ("$1" -ne 8,9,16,17)]] ; then
echo "Valid pins are 2-7, 10-15 & 18-23"
return -1;
fi
PIN=$(($LCD_OFFSET + $1));
cat /sys/class/gpio/gpio$PIN/value
}
# The MIT License (MIT) Copyright (c) 2016 Jeff Larkin
# modified by Nick Horvath
#
# 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.
@cornbreadheadman
Copy link
Author

Made all the 7s into 8s for obvious reasons.

@cornbreadheadman
Copy link
Author

added some LCDs

@cornbreadheadman
Copy link
Author

doesn't all work yet, wife is calling me to bed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment