Files
compulab/led_status.sh
T
2026-05-06 15:07:54 +02:00

46 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# green: gpiochip3 line 13 / LED1_GREEN (0 = on, 1 = off, active-low)
# red: gpiochip3 line 14 / LED2_RED (0 = on, 1 = off, active-low)
#
# gpioset v2 holds the line until the process exits, so each LED is driven
# by a background process. We kill and replace it to change state.
CHIP=/dev/gpiochip3
GREEN_PIN=13
RED_PIN=14
green_pid=""
red_pid=""
set_led() {
local pin=$1 val=$2 pid_var=$3
kill "${!pid_var}" 2>/dev/null || true
gpioset --chip "$CHIP" "${pin}=${val}" &
printf -v "$pid_var" '%s' "$!"
}
cleanup() {
kill "$green_pid" "$red_pid" 2>/dev/null || true
}
trap 'cleanup; exit 0' INT TERM
trap cleanup EXIT
# release any lines left held by a previous run of this script
pkill -f "gpioset.*$CHIP" 2>/dev/null || true
green_state=0 # start ON
while true; do
green_state=$(( 1 - green_state ))
set_led "$GREEN_PIN" "$green_state" green_pid
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then
set_led "$RED_PIN" 1 red_pid
else
set_led "$RED_PIN" 0 red_pid
fi
sleep 2
done