From b750f06f29f94a445abc71c2f353af7173201f20 Mon Sep 17 00:00:00 2001 From: jayfan0 <> Date: Sun, 2 Apr 2023 15:02:26 +0100 Subject: [PATCH] Added search feature to GPIO command --- general/overlay/usr/sbin/gpio | 55 +++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/general/overlay/usr/sbin/gpio b/general/overlay/usr/sbin/gpio index 567c6293..ffcab319 100644 --- a/general/overlay/usr/sbin/gpio +++ b/general/overlay/usr/sbin/gpio @@ -1,18 +1,26 @@ #!/bin/sh help() { - echo "gpio " - echo "gpio " - echo "gpio read " + echo -e "\n=== GPIO usage ===\n" + + echo -e ">\t gpio \n" + echo -e "set\t\t = Output HI\nclear\t\t = Output LOW\ntoggle\t\t = Swap HI <> LOW\nunexport\t = Release GPIO pin back to the kernel space\nread\t\t = Read GPIO pin output state\n" + + echo -e "---\n" + + echo -e ">\t gpio search \n" + echo -e "Toggle a range of GPIO pins from HI to LOW" + echo -e "gpio search logs to syslog\n" + + echo -e "==================\n" } exp() { if [ ! -d /sys/class/gpio/gpio$1 ]; then + [ ! -f /sys/class/gpio/export ] && echo "Error: No export file!" && exit echo $1 > /sys/class/gpio/export + [ ! -f /sys/class/gpio/gpio$1/direction ] && echo "Error: No direction file!" && exit echo out > /sys/class/gpio/gpio$1/direction - elif [ $(cat /sys/class/gpio/gpio$1/direction) = "in" ]; then - echo Pin $1 is currently configured as input. Be careful! - exit 0 fi } @@ -21,37 +29,57 @@ write(){ } read() { + [ ! -f /sys/class/gpio/gpio$1/value ] && echo "Error: GPIO-$1 is not set" && exit echo $(cat /sys/class/gpio/gpio$1/value) } unexport() { if [ -d /sys/class/gpio/gpio$1 ]; then echo $1 > /sys/class/gpio/unexport + else + echo "GPIO-${1} is not exported." fi } +search() { + echo -e "Start blink (output) on GPIO range $1 to $2\n" + for pin in $(seq $1 $2); do + echo "=================================" + exp ${pin} && echo " Exported GPIO-${pin} from kernel space" && echo " Set GPIO-${pin} to OUTPUT mode" + write 0 ${pin} && echo " Set GPIO-${pin} to LO level" ; sleep 1 + write 1 ${pin} && echo " Set GPIO-${pin} to HI level" ; sleep 1 + echo in > /sys/class/gpio/gpio${pin}/direction && echo " Set GPIO-${pin} to INPUT mode" + unexport ${pin} && echo " Unexported GPIO-${pin}" + logger HELLO - ${pin} + done +} + if [ -n $1 ] && [ -n $2 ] && [ $2 -eq $2 ]; then case $1 in set) exp $2 + echo "Setting GPIO-$2 to HI" write 1 $2 ;; clear) exp $2 + echo "Setting GPIO-$2 to LOW" write 0 $2 ;; toggle) exp $2 - if [ read -eq 0 ]; then + if [ $(read $2) -eq 0 ]; then + echo "Setting GPIO-$2 to HI" write 1 $2 else + echo "Setting GPIO-$2 to LOW" write 0 $2 fi ;; - unexport|release) + unexport) unexport $2 ;; @@ -59,10 +87,19 @@ if [ -n $1 ] && [ -n $2 ] && [ $2 -eq $2 ]; then echo $(read $2) ;; + search) + if [ ! -z $3 ] && [ $3 -eq $3 ]; then + search $2 $3 + else + echo -e "\n Caution: Toggling single pin. Use to search a range.\n" + search $2 $2 + fi + ;; + *) help ;; esac else help -fi \ No newline at end of file +fi