Skip to content

Commit

Permalink
hardware: add GPIO test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RadxaYuntian committed Oct 25, 2023
1 parent 559b3cd commit 4501fcb
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/usr/lib/rsetup/tui/hardware/gpio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# shellcheck shell=bash

__hardware_gpio_fmt() {
local i="$1"
if (( i < 4 ))
then
# shellcheck disable=SC2028
echo "%s %s %s %s\\n"
elif (( i == 4 ))
then
# shellcheck disable=SC2028
echo "%s %s %s %s\\n"
else
# shellcheck disable=SC2028
echo "%s %s %s %s\\n"
fi
}

__hardware_gpio_set() {
local i level="$1" gpio=() states=()

for i in {1..40}
do
if gpiofind "PIN_$i" >/dev/null
then
read -ra gpio < <(gpiofind "PIN_$i")
if gpioset "${gpio[0]}" "${gpio[1]}=$level"
then
gpioset -m signal "${gpio[0]}" "${gpio[1]}=$level" &
states+=("$level")
else
states+=("E")
fi
else
states+=(" ")
fi
done

msgbox "Following GPIO pins have their state changed temporarily:
0: Low | 1: High | E: Error
State Pin State
$(for i in {0..19}
do
# shellcheck disable=SC2059
printf "$(__hardware_gpio_fmt "$i")" \
"${states[i * 2]}" "$(( i * 2 + 1 ))" \
"$(( i * 2 + 2 ))" "${states[i * 2 + 1]}"
done)" #" # Workaround VS Code incorrect code highlighting

jobs -r -p | xargs -I{} kill -- {}
}

__hardware_gpio_set_high() {
__hardware_gpio_set 1
}

__hardware_gpio_set_low() {
__hardware_gpio_set 0
}

__hardware_gpio_get() {
local i level gpio=() states=()

for i in {1..40}
do
if gpiofind "PIN_$i" >/dev/null
then
read -ra gpio < <(gpiofind "PIN_$i")
if level="$(gpioget "${gpio[@]}" 2>/dev/null)"
then
states+=("$level")
else
states+=("E")
fi
else
states+=(" ")
fi
done

msgbox "Following is the current reading of all supported GPIO pins:
0: Low | 1: High | E: Error
State Pin State
$(for i in {0..19}
do
# shellcheck disable=SC2059
printf "$(__hardware_gpio_fmt "$i")" \
"${states[i * 2]}" "$(( i * 2 + 1 ))" \
"$(( i * 2 + 2 ))" "${states[i * 2 + 1]}"
done)" #" # Workaround VS Code incorrect code highlighting
}

__hardware_gpio() {
if loginctl | grep -e ttyS -e ttyAML -e ttyFIQ >/dev/null && \
! yesno "A serial session has been detected.
Testing GPIO with provided functions may cause the serial console to malfunction.
It will only return to normal working condition after a system reboot.
Do you want to continue?
"
then
return
fi

menu_init
menu_add __hardware_gpio_set_high "Set all GPIO to High"
menu_add __hardware_gpio_set_low "Set all GPIO to Low"
menu_add __hardware_gpio_get "Get all GPIO state"
menu_show "Please select the test case:"
}
4 changes: 4 additions & 0 deletions src/usr/lib/rsetup/tui/hardware/hardware.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# shellcheck source=src/usr/lib/rsetup/mod/config.sh
source "/usr/lib/rsetup/mod/config.sh"

# shellcheck source=src/usr/lib/rsetup/tui/hardware/gpio.sh
source "/usr/lib/rsetup/tui/hardware/gpio.sh"

__hardware_gstreamer_test_picture() {
local temp
temp="$(mktemp tmp.XXXXXXXXXX.jpg)"
Expand Down Expand Up @@ -240,6 +243,7 @@ __hardware() {
menu_add __hardware_gpio_leds "GPIO LEDs"
menu_add __hardware_thermal "Thermal governor"
menu_add __hardware_dsi_mirror "Configure DSI display mirroring"
menu_add __hardware_gpio "40-pin GPIO"
if $DEBUG
then
menu_add __hardware_rgb_leds "RGB LEDs"
Expand Down

0 comments on commit 4501fcb

Please sign in to comment.