#!/bin/bash
# How to run this script
# sudo curl https://www.bi7jta.org/files/MMDVM_Firmware/repeater_HAT/reboot_flash/2.2.exit_flash_mode-not-need-poweroff.sh | sudo sh

sudo mount -o remount,rw / ;

# suit bookworm OS
# gpio front/back compat. for pi5 and older Pi SBCs; since Pi5 has its own GPIO chip (gpiochip4 [pinctrl-rp1])
# also fixes new kernels on the RPi family (Thank you WPSD team)
#
getGPIOpinMaps=$(ls -la /sys/class/gpio/gpiochip* | grep "0000.gpio" | sed -n "s/.*gpiochip\([0-9]*\).*/\1/p" | tail -n 1)
 
#getGPIOpinMaps=${getGPIOpinMaps/504/0}

pin20=$((getGPIOpinMaps + 20))
pin21=$((getGPIOpinMaps + 21))
 
echo "pin20: ${pin20}"
echo "pin21: ${pin21}"

########以下是GPIO复位，避免重启才生效##########
echo "0" > /sys/class/gpio/gpio${pin21}/value  #229对应树莓派21

echo "0" > /sys/class/gpio/gpio${pin20}/value #230对应树莓派20
#whait enough to discharge C8
#sleep 1
##这里gpio21高电平就重置，所有灯全部亮了 ，要复位，需要将GPIO21 20都设置为0，再将GPIO21置为1
echo "1" > /sys/class/gpio/gpio${pin21}/value #230对应树莓派20
#whait enough to charge C8
#sleep 1

echo "in" > /sys/class/gpio/gpio${pin20}/direction  #230对应树莓派20
echo "in" > /sys/class/gpio/gpio${pin21}/direction  #229对应树莓派21
echo "${pin20}" > /sys/class/gpio/unexport #230对应树莓派20
echo "${pin21}" > /sys/class/gpio/unexport #229对应树莓派21
 




