From 4945654ed33555faeea11cbb7da01c2df01c80ae Mon Sep 17 00:00:00 2001 From: Steve Magnuson Date: Thu, 29 Jul 2021 12:46:38 -0700 Subject: [PATCH] Added option to print lever state and will work outside of GUI --- check-piano.sh | 49 +++++++++++++++++++++++++++++++++++-------------- test-piano.sh | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 30 deletions(-) diff --git a/check-piano.sh b/check-piano.sh index 8eec221..7075ad2 100755 --- a/check-piano.sh +++ b/check-piano.sh @@ -13,10 +13,11 @@ #% OPTIONS #% -h, --help Print this help #% -v, --version Print script information +#% -s, --state Print current switch state #% #================================================================ #- IMPLEMENTATION -#- version ${SCRIPT_NAME} 1.2.0 +#- version ${SCRIPT_NAME} 1.2.1 #- author Steve Magnuson, AG7GN #- license CC-BY-SA Creative Commons License #- script_id 0 @@ -28,6 +29,8 @@ # 20200525 : Steve Magnuson : Now using raspi-gpio instead of # gpio to be compatible with all # versions of Pis +# 20210719 : Steve Magnuson : Add -s option to print switch +# state # #================================================================ # DEBUG OPTION @@ -81,6 +84,22 @@ function Die () { SafeExit } +function GetSwitchState () { + # Array P: Array index is the ID of each individual switch in the piano switch. + # Array element value is the GPIO BCM number. + P[1]=25 + P[2]=13 + P[3]=6 + P[4]=5 + local LEVERS="" + for I in 1 2 3 4 + do + J=$($GPIO get ${P[$I]} | cut -d' ' -f3 | cut -d'=' -f2) # State of a switch in the piano (0 or 1) + (( $J == 0 )) && LEVERS="$LEVERS$I" + done + echo "$LEVERS" +} + #============================ # FILES AND VARIABLES #============================ @@ -95,24 +114,19 @@ VERSION="$(ScriptInfo version | grep version | tr -s ' ' | cut -d' ' -f 4)" GPIO="$(command -v raspi-gpio)" -# Array P: Array index is the ID of each individual switch in the piano switch. -# Array element value is the GPIO BCM number. -P[1]=25 -P[2]=13 -P[3]=6 -P[4]=5 #============================ # PARSE OPTIONS WITH GETOPTS #============================ #== set short options ==# -SCRIPT_OPTS=':hv-:' +SCRIPT_OPTS=':hsv-:' #== set long options associated with short one ==# typeset -A ARRAY_OPTS ARRAY_OPTS=( [help]=h + [state]=s [version]=v ) @@ -161,6 +175,18 @@ do ScriptInfo version exit 0 ;; + s) PIANO="$(GetSwitchState)" + [[ $PIANO == "" ]] && MESSAGE="No levers are down." || MESSAGE="Levers $PIANO are down." + if xset q &>/dev/null + then + yad --center --title="Test calling pianoX.sh script - version $VERSION" \ + --info --borders=30 --no-wrap \ + --text="$MESSAGE $HOME/piano$PIANO.sh will run." \ + --buttons-layout=center --button=Close:0 + else + echo "$MESSAGE" + fi + ;; :) Die "${SCRIPT_NAME}: -$OPTARG: option requires an argument" ;; @@ -187,12 +213,7 @@ $SYNTAX && set -n $DEBUG && set -x # String $PIANO will identify which levers are in the DOWN position -PIANO="" -for I in 1 2 3 4 -do - J=$($GPIO get ${P[$I]} | cut -d' ' -f3 | cut -d'=' -f2) # State of a switch in the piano (0 or 1) - (( $J == 0 )) && PIANO="$PIANO$I" -done +PIANO="$(GetSwitchState)" # Check if the script corresponding to the piano switch setting exists and is not empty. # diff --git a/test-piano.sh b/test-piano.sh index 806c0ae..9a2b7af 100755 --- a/test-piano.sh +++ b/test-piano.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION="1.1.0" +VERSION="1.1.1" # This script checks the status of 4 GPIO pins and runs a script corresponding # to those settings as described below. This script is called by initialize-pi.sh, @@ -8,20 +8,24 @@ VERSION="1.1.0" GPIO="$(command -v raspi-gpio)" -# Array P: Array index is the ID of each individual switch in the piano switch. -# Array element value is the GPIO BCM number. -P[1]=25 -P[2]=13 -P[3]=6 -P[4]=5 +function GetSwitchState () { + # Array P: Array index is the ID of each individual switch in the piano switch. + # Array element value is the GPIO BCM number. + P[1]=25 + P[2]=13 + P[3]=6 + P[4]=5 + local LEVERS="" + for I in 1 2 3 4 + do + J=$($GPIO get ${P[$I]} | cut -d' ' -f3 | cut -d'=' -f2) # State of a switch in the piano (0 or 1) + (( $J == 0 )) && LEVERS="$LEVERS$I" + done + echo "$LEVERS" +} # String $PIANO will identify which levers are in the DOWN position -PIANO="" -for I in 1 2 3 4 -do - J=$($GPIO get ${P[$I]} | cut -d' ' -f3 | cut -d'=' -f2) # State of a switch in the piano (0 or 1) - (( $J == 0 )) && PIANO="$PIANO$I" -done +PIANO="$(GetSwitchState)" # Check if the script corresponding to the piano switch setting exists and is not empty. # @@ -34,7 +38,13 @@ done [[ $PIANO == "" ]] && MESSAGE="No levers are down." || MESSAGE="Levers $PIANO are down." -yad --center --title="Test calling pianoX.sh script - version $VERSION" --info --borders=30 \ - --no-wrap --text="$MESSAGE $HOME/piano$PIANO.sh will run." --buttons-layout=center \ ---button=Close:0 +if xset q &>/dev/null +then + yad --center --title="Test calling pianoX.sh script - version $VERSION" \ + --info --borders=30 --no-wrap \ + --text="$MESSAGE $HOME/piano$PIANO.sh will run." \ + --buttons-layout=center --button=Close:0 +else + echo "$MESSAGE" +fi