Created
July 8, 2016 17:49
-
-
Save rvido/d7c3dde69edd2a56429de8d5c711bf71 to your computer and use it in GitHub Desktop.
A shell script for testing (iManager) GPIO-LED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################################################################ | |
# test-led.sh - Shell script to test GPIOs through sysfs | |
# | |
# Copyright (c) 2016 Richard Vidal-Dorsch | |
# | |
# This program is free software; you can redistribute it and/or modify it | |
# under the terms of the GNU General Public License as published by the | |
# Free Software Foundation; either version 2 of the License, or (at your | |
# option) any later version. | |
# | |
################################################################################ | |
VERSION=v1.0.0.20160613 | |
DRVNAME=gpio-imanager | |
GPIO_SYSFS_PATH=/sys/class/gpio | |
print_banner() | |
{ | |
echo "GPIO LED Test Script ${VERSION}" | |
echo "Richard Vidal-Dorsch <[email protected]>" | |
echo "Copyright (c) 2016 Richard Vidal-Dorsch" | |
echo | |
} | |
# Returns path to gpiochip<base number> of the matching GPIO chip | |
find_gpiochip() | |
{ | |
local drvname=${1} | |
for i in ${GPIO_SYSFS_PATH}/gpiochip*; do | |
[[ ! -e ${i} ]] && return | |
if [ "$(cat ${i}/label)" == "${drvname}" ]; then | |
echo ${i} | |
break | |
fi | |
done | |
} | |
check_requirements() | |
{ | |
[ -d "$GPIO_SYSFS_PATH/" ] || { \ | |
echo "Error: Directory '$GPIO_SYSFS_PATH/' does not exist.";\ | |
echo "Please enable GPIO sysfs support in your kernel"; | |
return 1; } | |
if [ -z "$(find_gpiochip ${DRVNAME})" ]; then | |
echo "Error: GPIO driver '${DRVNAME}' does not seem to be loaded." | |
return 1 | |
fi | |
} | |
create_gpio_files() | |
{ | |
local arr=(${1}) | |
for i in ${arr[*]}; do | |
[ ! -e $GPIO_SYSFS_PATH/gpio${i} ] && \ | |
echo "$i" > $GPIO_SYSFS_PATH/export | |
done | |
sleep 0.5; # Give enough time to create those nodes | |
} | |
remove_gpio_files() | |
{ | |
local arr=(${1}) | |
for i in ${arr[*]};do | |
echo "$i" > $GPIO_SYSFS_PATH/unexport || return 1 | |
done | |
} | |
set_gpio_direction() | |
{ | |
local num=$1 | |
local dir=$2 # "in" or "out" | |
echo $dir > $GPIO_SYSFS_PATH/gpio${num}/direction || return 1 | |
} | |
set_gpio_state() | |
{ | |
local num=$1 | |
local state=$2 # "0" or "1" | |
echo $state > $GPIO_SYSFS_PATH/gpio${num}/value || return 1 | |
} | |
gpio_set_dirs2out() | |
{ | |
local arr=(${1}) | |
for i in ${arr[*]}; do | |
set_gpio_direction $i "out" | |
done | |
} | |
gpio_restore_direction() | |
{ | |
local arr=(${1}) | |
# Set all GPIO direction to input | |
for i in ${arr[*]}; do | |
set_gpio_direction $i "in" | |
done | |
} | |
# --- main --- | |
print_banner | |
check_requirements || exit 1 | |
sysfs_path_chip=$(find_gpiochip "${DRVNAME}") | |
base=$(cat ${sysfs_path_chip}/base) | |
ngpio=$(cat ${sysfs_path_chip}/ngpio) | |
max=$(echo $[${base} + ${ngpio} - 1]) | |
range=($(seq $base $max)) | |
# Export GPIO to user space | |
create_gpio_files "${range[*]}" | |
gpio_set_dirs2out "${range[*]}" | |
for i in ${range[*]}; do set_gpio_state $i 0;done | |
for i in ${range[*]}; do | |
for j in $(seq 1 5); do | |
printf "\rSet GPO %d to '1'" $(echo $[${i} - ${base}]) | |
set_gpio_state $i 1 | |
sleep 0.2 | |
printf "\rSet GPO %d to '0'" $(echo $[${i} - ${base}]) | |
set_gpio_state $i 0 | |
sleep 0.2 | |
done | |
printf "\r" | |
done | |
# Remove GPIO from user space | |
remove_gpio_files "${range[*]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment