Skip to content

Instantly share code, notes, and snippets.

@arrjay
Last active September 16, 2023 13:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arrjay/dded4a53d1c40551e25bbb6ce17d808f to your computer and use it in GitHub Desktop.
Save arrjay/dded4a53d1c40551e25bbb6ce17d808f to your computer and use it in GitHub Desktop.
# this file goes in $HOME/.config/systemd/user
[Unit]
Description=beepy backlight controller
[Service]
Type=simple
# adjust this path to where you wrote the script out
ExecStart=/home/rj/bin/noarch/beepy-bl.sh
Restart=on-failure
[Install]
WantedBy=default.target
#!/usr/bin/env bash
# I created this file in $HOME/bin/noarch/beepy-bl.sh (you will need to adjust unit paths, see above)
# beepy keyboard device to watch for events
DEVICE=/dev/input/by-path/platform-3f804000.i2c-event
# time (in seconds) before we shut the backlight off
IDLE=10
# keyboard brightness (0..255)
BRIGHT=255
DARK=0
# check for keyboard backlight
backlight=/sys/firmware/beepy/keyboard_backlight
[[ -f "${backlight}" ]] || { printf 'could not find backlight control %s\n' "${backlight}" 1>&2 ; exit 1 ; }
# this is going to loop on events from our device
# it will immediately flip the backlight to on
# and output the letter k
evemu_event() {
local event_ready=0
# output an event *before* we get going
printf '%s\n' 'k'
while read -r line ; do
case "${line}" in
################################) : ;;
*"Waiting for events"*) event_ready=1 ;;
*)
[[ "${event_ready}" -eq 1 ]] || continue
printf '%s\n' "${BRIGHT}" | sudo tee "${backlight}" >/dev/null
printf '%s\n' 'k'
;;
esac
done < <(evemu-record "${DEVICE}")
}
# start the coprocs now
coproc watch { evemu_event ; }
# main loop
ce="${IDLE}"
light="${BRIGHT}"
# initial read flags - raw, 1sec timeout
def_rflags=('-t' '1' '-r')
rflags=("${def_rflags[@]}")
while true ; do
# the read with a timeout is actually what helps us
read "${rflags[@]}" -u "${watch[0]}" event
# get event - reset idle counter
[[ "${event}" ]] && { ce="${IDLE}" ; light="${BRIGHT}" ; rflags=("${def_rflags[@]}") ; continue ; }
# waiting out the idle counter
[[ "${ce}" -gt 0 ]] && { ce=$((ce -= 1)) ; continue ; }
# okay, sleep the thing
[[ "${light}" -ne "${DARK}" ]] && {
printf '%s\n' "${DARK}" | sudo tee "${backlight}" >/dev/null
light="${DARK}"
# pause the loop by removing the read timeout
rflags=('-r')
}
done
systemctl --user enable beepy-bl
systemctl --user start beepy-bl
systemctl --user status beepy-bl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment