bin/g910-lights

185 lines
3.8 KiB
Plaintext
Raw Permalink Normal View History

2019-06-21 22:56:05 -05:00
#! /bin/bash
#
# g910 coloring service
# Copyright (C) 2019 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
colorbase="002b36"
colorblue="268bd2"
colorgreen="859900"
colormagenta="d33682"
colorred="dc322f"
coloryellow="b58900"
# Basic functions
log() {
[ -z "$1" ] && return 1
printf "g910: %s\\n" \
"$1"
}
rsleep() {
[ -z "$1" ] && return 1
exec 1023<> <(:)
read -t "$1" -u 1023
return 0
}
# Main loop
loop-stats() {
while :; do
# CPU stats on F1 and G6
g910-led -kn f1 $colorblue
if (( $(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)/($2+$4+$5)} END {print (usage > 0.75)}') )); then
2019-11-27 23:49:44 -06:00
if [ $(($(date +%s) % 2)) -eq 0 ]; then
g910-led -kn g6 $colorred
else
g910-led -kn g6 $colorbase
fi
2019-06-21 22:56:05 -05:00
else
g910-led -kn g6 $colorgreen
fi
# Memory stats on F2 and G7
g910-led -kn f2 $colormagenta
if (( $(free | grep Mem | awk '{print ($7/$2 < 0.25)}') )); then
2019-11-27 23:49:44 -06:00
if [ $(($(date +%s) % 2)) -eq 0 ]; then
g910-led -kn g7 $colorred
else
g910-led -kn g7 $colorbase
fi
2019-06-21 22:56:05 -05:00
else
g910-led -kn g7 $colorgreen
fi
# Root disk stats on F3 and G8
g910-led -kn f3 $coloryellow
if (( $(df --output=pcent / | tr -dc '0-9') > 75 )); then
g910-led -kn g8 $colorred
else
g910-led -kn g8 $colorgreen
fi
# Home disk stats on F4 and G9
g910-led -kn f4 $coloryellow
if (( $(df --output=pcent /home | tr -dc '0-9') > 75 )); then
g910-led -kn g9 $colorred
else
g910-led -kn g9 $colorgreen
fi
# Commit
g910-led -c
# Wait
rsleep 1
done
}
initloops() {
# i3wm integration
if pgrep i3 > /dev/null 2>&1; then
log "Coloring i3 related keys"
g910-led -k logo 000000
rsleep 0.025
g910-led -kn logo $colorred
g910-led -kn alt_left $colorred
g910-led -kn alt_right $colorred
g910-led -kn win_left $colorblue
g910-led -kn win_right $colorblue
g910-led -c
fi
# General loops
loop-stats &
log "Starting general stat display with PID $!"
wait
}
main() {
if ! command -v g910-led > /dev/null 2>&1; then
log "g910-led is not installed. Install it with the g810-led package"
exit 50
fi
if ! g910-led -a 000000 > /dev/null 2>&1; then
log "Could not find keyboard"
log "Ensure it is plugged in and that you have the proper udev rules set up"
log "See https://raw.githubusercontent.com/MatMoul/g810-led/master/udev/g810-led.rules"
exit 51
fi
log "Playing init animation"
log "Dimming all keys"
g910-led -a 000000
log "Flickering logo on"
rsleep 0.25
g910-led -k logo ffffff
rsleep 0.05
g910-led -k logo 000000
rsleep 0.025
g910-led -k logo ffffff
rsleep 0.025
g910-led -k logo 000000
rsleep 0.025
g910-led -k logo ffffff
rsleep 1
log "Flickering second logo on"
rsleep 0.025
g910-led -k logo2 $colorblue
rsleep 0.025
g910-led -k logo2 000000
rsleep 0.025
g910-led -k logo2 $colorred
log "Flickering G-keys on"
g910-led -g gkeys ffffff
rsleep 0.05
g910-led -g gkeys 000000
rsleep 0.025
g910-led -g gkeys 444444
rsleep 0.25
log "Flickering F-keys on"
g910-led -g fkeys 222222
rsleep 0.05
g910-led -g fkeys 333333
rsleep 0.025
g910-led -g fkeys 444444
log "Flickering keys on"
g910-led -g keys aaaaaa
rsleep 0.05
g910-led -g keys 888888
rsleep 0.15
log "Flickering numpad on"
g910-led -g numeric 888888
log "Flickering arrow keys on"
g910-led -g arrows 222222
rsleep 0.025
g910-led -g arrows 444444
rsleep 0.10
log "Flickering modifier keys on"
g910-led -g modifiers 444444
rsleep 0.025
g910-led -gn modifiers 888888
g910-led -kn menu 000000
g910-led -c
rsleep 0.05
log "Flickering multimedia keys on"
g910-led -g multimedia ffffff
rsleep 0.025
g910-led -g multimedia 888888
log "Flickering functions on"
g910-led -g functions aaaaaa
rsleep 0.025
g910-led -g functions 444444
g910-led -k logo2 $colorgreen
# Set up listeners as background processes
initloops
}
main "$@"