Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- off_time=3
- polling_time=.1
- startup_time=20:00
- end_time=08:00
- on_value=10
- off_value=0
- # check for root user
- if [[ $EUID -ne 0 ]]; then
- echo "This script must be run as root"
- exit 1
- fi
- # INIT VARIABLES
- counter=1
- light_status=off
- # how many counter to wait for the correct(ish) off_time
- off_time_counter=$(bc <<< "scale=0; (1 / $polling_time) * $off_time")
- keyboard="/dev/input/event0"
- # getting the backlight class (hopefully)
- backlight=`find /sys/class | grep kbd_backlight`
- backlight="${backlight}/brightness"
- if [ ! -f "$backlight" ]; then
- echo "Sorry I couln't find a backlight switch"
- exit 1
- fi
- while true; do
- current_time=$(date +%H:%M)
- if [[ "$current_time" > "$startup_time" ]] || [[ "$current_time" < "$end_time" ]]; then
- # get input from keyboard
- POLL=`timeout $polling_time cat $keyboard | tr -d '\0'`
- # keyboard is active, switch lights on
- if [ "$POLL" != "" ]; then
- counter=1
- if [ "$light_status" == "off" ]; then
- echo $on_value > $backlight
- light_status="on"
- fi
- # keyboard is inactive... if it stays so for off_time, switch lights off
- else
- if [ "$counter" -lt "$off_time_counter" ]; then
- counter=$((counter + 1))
- elif [ "$counter" -eq "$off_time_counter" ]; then
- echo $off_value > $backlight
- light_status="off"
- fi
- fi
- else
- # outside active hours, wait a minute and check again
- sleep 60
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement