Files
Openwrt_Print_Server/files-overlay/hiker_x9/full/etc/rc.button/reset
2026-02-12 18:13:56 +08:00

168 lines
3.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# Hiker Print - Reset Button Handler with LED feedback
# 按住2秒开始红蓝交替闪松开红蓝长亮2秒重启
# 按住超过5秒开始红蓝同时闪松开红蓝长亮2秒恢复出厂设置
. /lib/functions.sh
LED_R="/sys/class/leds/hiker:red:led_r/brightness"
LED_R_TRIGGER="/sys/class/leds/hiker:red:led_r/trigger"
LED_B="/sys/class/leds/hiker:blue:led_b/brightness"
LED_B_TRIGGER="/sys/class/leds/hiker:blue:led_b/trigger"
PIDFILE="/var/run/reset_button_led.pid"
# Function to check if LED script is running
stop_led_script() {
if [ -f "$PIDFILE" ]; then
local pid=$(cat "$PIDFILE")
if [ -d "/proc/$pid" ]; then
kill "$pid" 2>/dev/null
fi
rm -f "$PIDFILE"
fi
rm -f /tmp/stop_led_blink
}
# Function to turn off both LEDs
turn_off_leds() {
echo "none" > "$LED_R_TRIGGER" 2>/dev/null
echo "none" > "$LED_B_TRIGGER" 2>/dev/null
echo 0 > "$LED_R" 2>/dev/null
echo 0 > "$LED_B" 2>/dev/null
}
# Function to turn on both LEDs
turn_on_leds() {
echo "none" > "$LED_R_TRIGGER" 2>/dev/null
echo "none" > "$LED_B_TRIGGER" 2>/dev/null
echo 1 > "$LED_R" 2>/dev/null
echo 1 > "$LED_B" 2>/dev/null
}
# Function to alternate red and blue LEDs (for reboot: 2-5s)
led_alternate_blink_loop() {
local period=$1 # Blink period in seconds
echo "none" > "$LED_R_TRIGGER" 2>/dev/null
echo "none" > "$LED_B_TRIGGER" 2>/dev/null
local start_time=$(date +%s)
while [ ! -f /tmp/stop_led_blink ]; do
local now=$(date +%s)
local elapsed=$((now - start_time))
# Only start blinking after 2 seconds
if [ $elapsed -lt 2 ]; then
sleep 1
continue
fi
# Blue: on, Red: off
echo 1 > "$LED_B" 2>/dev/null
echo 0 > "$LED_R" 2>/dev/null
sleep "$period"
# Blue: off
echo 0 > "$LED_B" 2>/dev/null
# Red: on (instant switch)
echo 1 > "$LED_R" 2>/dev/null
sleep "$period"
# Red: off
echo 0 > "$LED_R" 2>/dev/null
# Blue: on (instant switch, next iteration)
done
turn_off_leds
rm -f /tmp/stop_led_blink
}
# Function to blink both LEDs together (for factory reset: 5s+)
led_both_blink_loop() {
local period=$1 # Blink period in seconds
echo "none" > "$LED_R_TRIGGER" 2>/dev/null
echo "none" > "$LED_B_TRIGGER" 2>/dev/null
local start_time=$(date +%s)
while [ ! -f /tmp/stop_led_blink ]; do
local now=$(date +%s)
local elapsed=$((now - start_time))
# Only start blinking after 2 seconds
if [ $elapsed -lt 2 ]; then
sleep 1
continue
fi
# Check if we've passed 5 seconds - switch from alternate to both blink
if [ $elapsed -lt 5 ]; then
# Still in reboot zone (2-5s): alternate
echo 1 > "$LED_B" 2>/dev/null
echo 0 > "$LED_R" 2>/dev/null
sleep "$period"
echo 0 > "$LED_B" 2>/dev/null
echo 1 > "$LED_R" 2>/dev/null
sleep "$period"
echo 0 > "$LED_R" 2>/dev/null
else
# Factory reset zone (5s+): both blink together
turn_on_leds
sleep "$period"
turn_off_leds
sleep "$period"
fi
done
turn_off_leds
rm -f /tmp/stop_led_blink
}
case "${ACTION}" in
pressed)
# Button pressed - wait 2 seconds then start LED feedback loop
stop_led_script
# Start background LED blink process (alternates 2-5s, both blink 5s+)
rm -f /tmp/stop_led_blink
led_both_blink_loop 1 &
echo $! > "$PIDFILE"
;;
released)
# Button released - stop LED and execute action
stop_led_script
# Wait for LED to turn off (no sleep - busybox doesn't support decimals)
logger "$BUTTON pressed for $SEEN seconds"
if [ "$SEEN" -lt 2 ]
then
# <2秒: 无操作
echo "No action (<2s)" > /dev/console
turn_off_leds
exit 0
elif [ "$SEEN" -ge 2 ] && [ "$SEEN" -lt 5 ]
then
# 2-5秒: 红蓝长亮2秒然后重启
echo "REBOOT (${SEEN}s pressed)" > /dev/console
turn_on_leds
sleep 2
sync
reboot
elif [ "$SEEN" -ge 5 ]
then
# >=5秒: 红蓝长亮2秒然后恢复出厂设置
echo "FACTORY RESET (${SEEN}s pressed)" > /dev/console
turn_on_leds
sleep 2
jffs2reset -y && reboot &
fi
;;
esac
return 0