diff --git a/README.md b/README.md index 706921d..68afd39 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cl-battery.asd b/cl-battery.asd new file mode 100644 index 0000000..fcb9a2d --- /dev/null +++ b/cl-battery.asd @@ -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"))) diff --git a/cl-battery.lisp b/cl-battery.lisp new file mode 100644 index 0000000..f39a68b --- /dev/null +++ b/cl-battery.lisp @@ -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*)) diff --git a/main.lisp b/main.lisp deleted file mode 100644 index dbb76e3..0000000 --- a/main.lisp +++ /dev/null @@ -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) diff --git a/package.lisp b/package.lisp new file mode 100644 index 0000000..8c4caf5 --- /dev/null +++ b/package.lisp @@ -0,0 +1,5 @@ + + +(defpackage #:cl-battery + (:use #:cl) + (:nicknames :battery ))