Add "shutdown" feature to button service

If button is pressed for 5+ seconds, system will shutdown (halt)
This commit is contained in:
nagumii 2025-01-13 16:35:25 +02:00
parent 16c0f3c220
commit f2b12c5173
4 changed files with 51 additions and 19 deletions

View file

@ -4,32 +4,43 @@ logger "Listening for button press on $DEVICE..."
evtest --grab /dev/input/event0 | while read line; do
# Check for key press event
if echo "$line" | grep -q "EV_KEY.*value 1"; then
start_time=$(date +%s)
start_time=$(date +%s) #start timer
fi
# Check for key release event
if echo "$line" | grep -q "EV_KEY.*value 0"; then
duration=$(($(date +%s) - start_time))
if [ $duration -lt 2 ]; then
( for i in $(seq 1 3); do
if [ $duration -lt 2 ]; then # <2 second press: toggle wifi
(
for i in $(seq 1 3); do
echo 1 > /sys/class/gpio/gpio34/value;
sleep 0.25;
echo 0 > /sys/class/gpio/gpio34/value;
sleep 0.25;
done
) &
wifi_status=$(cat /root/.portduino/default/prefs/config.proto | protoc --decode_raw | awk '/4 {/, /}/ {if ($1 == "1:") print $2}')
if [ "$wifi_status" -eq 1 ]; then
logger "$duration second button press detected. Toggling wifi off."
femto-meshtasticd-config.sh -m "--set network.wifi_enabled false" 10 "Button toggle wifi"
else
logger "$duration second button press detected. Toggling wifi on."
femto-meshtasticd-config.sh -m "--set network.wifi_enabled true" 10 "Button toggle wifi"
fi
else
logger "$duration second button press detected. Rebooting."
echo 1 > /sys/class/gpio/gpio34/value;
msg="$duration second button press detected. Toggling wifi"
echo $msg | sudo tee /dev/console
logger $msg
(femto-network-config.sh -T) & # toggle wifi in background so button can be repressed immediately
elif [ $duration -lt 5 ]; then # 2-5 second press: reboot
msg="$duration second button press detected. Rebooting"
echo $msg | sudo tee /dev/console
logger $msg
for i in $(seq 1 20); do
echo 1 > /sys/class/gpio/gpio34/value;
sleep 0.0625;
echo 0 > /sys/class/gpio/gpio34/value;
sleep 0.0625;
done
reboot
else # 5+ second press: halt
msg="$duration second button press detected. Halting system"
echo $msg | sudo tee /dev/console
logger $msg
echo 1 > /sys/class/gpio/gpio34/value;
halt
fi
fi
done

View file

@ -172,8 +172,9 @@ utilities() {
4 "Run USB configuration tool" \
5 "Run OEM luckfox-config" \
6 "Reboot" \
7 "Shutdown" \
"" "" \
7 "Back to Main Menu" 3>&1 1>&2 2>&3)
8 "Back to Main Menu" 3>&1 1>&2 2>&3)
exit_status=$? # This line checks the exit status of the dialog command
if [ $exit_status -ne 0 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog
@ -202,7 +203,15 @@ utilities() {
reboot
fi
;;
7) return ;;
7)
dialog --title "$title" --yesno "Shut down now?\n\nTo power system back on, disconnect/reconnect power.\n\nThe system shutdown command prevents damage to the filesystem from power interrupts, but does not stop power consumption.\n\nTip: You can also shut down the Femtofox by pressing the BOOT button for 5 seconds or by running \`sudo halt\`." 14 60
if [ $? -eq 0 ]; then #unless cancel/no
dialog --infobox "Shutting down...\n\nPower consumption will not stop." 5 45
logger "User requested system halt"
halt
fi
;;
8) return ;;
esac
done
}

View file

@ -8,7 +8,8 @@ wpa_supplicant_conf="/etc/wpa_supplicant/wpa_supplicant.conf"
help=$(cat <<EOF
Options are:
-h This message
-x "up" Set wifi status (options are "up" or "down")
-x "up" Set wifi state (options are "up" or "down")
-T Toggle wifi state
-s "SSID" Set Wi-Fi SSID
-p "PSK" Set Wi-Fi PSK (password)
-c "COUNTRY" Set Wi-Fi 2-letter country code (such as US, DE)
@ -36,18 +37,29 @@ if [ $# -eq 0 ]; then
fi
# Parse options
while getopts ":hx:s:p:c:ewn:tr" opt; do
while getopts ":hx:Ts:p:c:ewn:tr" opt; do
case ${opt} in
h) # Option -h (help)
echo -e "$help"
;;
x) # Option -x (set wifi status)
x) # Option -x (set wifi state)
if [ $OPTARG = "up" ]; then
logger "Setting wifi to up"
ip link set wlan0 up
elif [ $OPTARG = "down" ]; then
logger "Setting wifi to up"
ip link set wlan0 down
fi
;;
T) # Option -T (toggle wifi state)
if [ "$(cat /etc/wifi_state.txt)" == "up" ]; then
logger "Toggling wifi off"
femto-network-config.sh -x "up"
else
logger "Toggling wifi on"
femto-network-config.sh -x "down"
fi
;;
s) # Option -s (ssid)
sed -i "/ssid=/s/\".*\"/\"$OPTARG\"/" "$wpa_supplicant_conf"
echo "Setting SSID to $OPTARG."