battery monitor script
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4KB

  1. ;;; Battery monitoring daemon script.
  2. (import shell)
  3. (import srfi-1)
  4. ;;; Constant values.
  5. ;;; Battery level control interval, in seconds.
  6. (define +check-interval+ 5)
  7. ;;; Path to the virtual file containing the battery percentage number.
  8. (define +capacity-path+ "/sys/class/power_supply/BAT1/capacity")
  9. ;;; Association list of threshold levels and formatting functions.
  10. (define +thresholds+
  11. `((20 . ,(lambda (battery-level)
  12. (string-append "Battery level low: " (number->string battery-level) "%.")))
  13. (5 . ,(lambda (battery-level)
  14. (string-append "Battery level critical: " (number->string battery-level) "%.")))))
  15. ;;; Utility functions.
  16. ;;; Get the battery level, return it as a number.
  17. (define (get-battery-level)
  18. (with-input-from-file +capacity-path+ read))
  19. ;;; Method to display a notification dialog.
  20. (define (user-notify message)
  21. (run ("notify-send '" ,message "'")))
  22. ;;; Core daemon loop.
  23. (define (core previous-level)
  24. (let ((current-level (get-battery-level)))
  25. (let ((crossed-thresholds
  26. (filter (lambda (n)
  27. (and (>= n current-level)
  28. (< n previous-level)))
  29. (map car +thresholds+))))
  30. (if (not (null? crossed-thresholds))
  31. (user-notify ((cdr (assoc (last crossed-thresholds) +thresholds+)) current-level))))
  32. (sleep +check-interval+)
  33. (core current-level)))
  34. ;;; Entry point of the program.
  35. (define (main)
  36. (core 100))
  37. (main)