Avril 4 years ago
parent 370da9f700
commit 27383a5d40
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -3,10 +3,4 @@ CL-BATTERY
---
A Simple Battery Script By Common Lisp
TODO
=
---
Add Details Functions (Get Current, Get States, etc.)
>Copyleft Z.Shang 2014

@ -0,0 +1,15 @@
(asdf:defsystem :cl-battery
:description
"Battery stats"
:author
"Z.Shang"
:license
"copyleft"
:version
"0.0.2"
:serial
t
:components ( (:file "package")
(:file "cl-battery")))

@ -0,0 +1,52 @@
(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*))

@ -1,24 +0,0 @@
(defpackage :cl-battery
(:use :cl)
(:export
show-bat
)
)
(in-package #:cl-battery)
(defun show-bat ()
(let ((bat_stat (open #P"/sys/class/power_supply/BAT0/status"))
(bat_now (open #P"/sys/class/power_supply/BAT0/current_now"))
(bat_full (open #P"/sys/class/power_supply/BAT0/charge_full"))
(ac_online (open #P"/sys/class/power_supply/ADP0/online"))
)
(format *standard-output* "~A ~$% ~A~%" (read-line bat_stat) (* (float (/ (read bat_now) (read bat_full))) 100)
(if (equal (read ac_online) 1)
"To Be Fully Charged"
"Left"))
(close bat_stat)
(close bat_now)
(close bat_full)))
(sb-ext:save-lisp-and-die #P"bat" :toplevel #'cl-battery:show-bat :executable t)

@ -0,0 +1,5 @@
(defpackage #:cl-battery
(:use #:cl)
(:nicknames :battery ))
Loading…
Cancel
Save