-
-
Save dezren39/3dd97d322d6a409c979241fcb224a255 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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