Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #!/bin/bash
  2. off_time=3
  3. polling_time=.1
  4. startup_time=20:00
  5. end_time=08:00
  6.  
  7. on_value=10
  8. off_value=0
  9.  
  10. # check for root user
  11. if [[ $EUID -ne 0 ]]; then
  12. echo "This script must be run as root"
  13. exit 1
  14. fi
  15.  
  16. # INIT VARIABLES
  17.  
  18. counter=1
  19. light_status=off
  20. # how many counter to wait for the correct(ish) off_time
  21. off_time_counter=$(bc <<< "scale=0; (1 / $polling_time) * $off_time")
  22. keyboard="/dev/input/event0"
  23. # getting the backlight class (hopefully)
  24. backlight=`find /sys/class | grep kbd_backlight`
  25. backlight="${backlight}/brightness"
  26.  
  27. if [ ! -f "$backlight" ]; then
  28. echo "Sorry I couln't find a backlight switch"
  29. exit 1
  30. fi
  31.  
  32.  
  33. while true; do
  34.  
  35. current_time=$(date +%H:%M)
  36.  
  37. if [[ "$current_time" > "$startup_time" ]] || [[ "$current_time" < "$end_time" ]]; then
  38. # get input from keyboard
  39. POLL=`timeout $polling_time cat $keyboard | tr -d '\0'`
  40. # keyboard is active, switch lights on
  41. if [ "$POLL" != "" ]; then
  42. counter=1
  43. if [ "$light_status" == "off" ]; then
  44. echo $on_value > $backlight
  45. light_status="on"
  46. fi
  47.  
  48. # keyboard is inactive... if it stays so for off_time, switch lights off
  49. else
  50. if [ "$counter" -lt "$off_time_counter" ]; then
  51. counter=$((counter + 1))
  52. elif [ "$counter" -eq "$off_time_counter" ]; then
  53. echo $off_value > $backlight
  54. light_status="off"
  55. fi
  56. fi
  57. else
  58. # outside active hours, wait a minute and check again
  59. sleep 60
  60. fi
  61. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement