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.

53 lines
1.3 KiB

(in-package :cl-battery)
(defparameter *battery-device* "BAT0")
(defparameter *ac-device* "AC")
(defun %path (device name)
(pathname (format nil "/sys/class/power_supply/~a/~a" device name)))
(defun stat ()
(let* ((bat-stat (open (%path *battery-device* "status")))
(bat-now (open (%path *battery-device* "current_now")))
(bat-full (open (%path *battery-device* "charge_full")))
(ac-online (open (%path *ac-device* "online")))
(bat-cap (open (%path *battery-device* "capacity")))
(-now (read bat-now))
(-full (read bat-full))
(-cap (read bat-cap)))
(let ((r (list (read-line bat-stat)
(float (/ -now -full))
(= 1 (read ac-online))
-now
-full)))
(mapc #'close (list bat-stat bat-now bat-full ac-online bat-cap))
(values r -cap))))
(defun status ()
"Get current charging status"
(car (stat)))
(defun capacity ()
"Capacity"
(cadr (multiple-value-list (stat))))
(defun level ()
"Current level / max level. Also returns (current max) as second value"
(let ((st (stat)))
(values (cadr st)
(cdddr st))))
(defun charging? ()
"Is the battery charging"
(caddr (stat)))
(mapc #'export
'(stat
status
level
charging?
capacity
*battery-device*
*ac-device*))