From f2b12c51738dde5e5f615b9038bff6a4a00a24ee Mon Sep 17 00:00:00 2001 From: nagumii Date: Mon, 13 Jan 2025 16:35:25 +0200 Subject: [PATCH] Add "shutdown" feature to button service If button is pressed for 5+ seconds, system will shutdown (halt) --- .../usr/local/bin/button.sh | 39 ++++++++++++------- .../usr/local/bin/femto-config | 13 ++++++- .../usr/local/bin/femto-network-config.sh | 18 +++++++-- ...fig.sh => femto-wifi-config.sh.deprecated} | 0 4 files changed, 51 insertions(+), 19 deletions(-) rename foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/{femto-wifi-config.sh => femto-wifi-config.sh.deprecated} (100%) mode change 100755 => 100644 diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh index a5024e1..dd8cc28 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh @@ -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 diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config index 04b24d6..cdc942c 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config @@ -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 } diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh index 5cab66c..bd4c4db 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh @@ -8,7 +8,8 @@ wpa_supplicant_conf="/etc/wpa_supplicant/wpa_supplicant.conf" help=$(cat <