>From 905c2ccd805bb6be0ed11e4f85f9baf316106da1 Mon Sep 17 00:00:00 2001 From: Jeremy Compostella Date: Sun, 18 Dec 2011 19:38:29 +0100 Subject: [PATCH] battery.el: Retrieve more information from sysfs The sysfs power_supply interface provides more information than the ones retrieved for now. This patch adds the rate and temperature properties. It provides the ability to estimate the remaining time too using the current rate, the current voltage and the remaining capacity. The time remaining estimation works on both charging and discharging states. --- lisp/battery.el | 37 ++++++++++++++++++++++++++++++++++--- 1 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lisp/battery.el b/lisp/battery.el index 3b245ed..b6b98d5 100644 --- a/lisp/battery.el +++ b/lisp/battery.el @@ -420,11 +420,16 @@ This function works only with the new `/sys/class/power_supply/' format introduced in Linux version 2.4.25. The following %-sequences are provided: +%r Current rate +%d Temperature (in degrees Celsius) %c Current capacity (mAh or mWh) %B Battery status (verbose) %p Battery load percentage -%L AC line status (verbose)" - (let (charging-state +%L AC line status (verbose) +%m Remaining time (to charge or discharge) in minutes +%h Remaining time (to charge or discharge) in hours +%t Remaining time (to charge or discharge) in the form `h:min'" + (let (charging-state rate temperature hours (charge-full 0.0) (charge-now 0.0) (energy-full 0.0) @@ -444,6 +449,11 @@ The following %-sequences are provided: (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t) (member charging-state '("Unknown" "Full" nil)) (setq charging-state (match-string 1))) + (when (or (re-search-forward "POWER_SUPPLY_CURRENT_NOW=\\([0-9]*\\)$" nil t) + (re-search-forward "POWER_SUPPLY_POWER_NOW=\\([0-9]*\\)$" nil t)) + (setq rate (float (string-to-number (match-string 1))))) + (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t) + (setq temperature (match-string 1))) (let (full-string now-string) ;; Sysfs may list either charge (mAh) or energy (mWh). ;; Keep track of both, and choose which to report later. @@ -466,12 +476,33 @@ The following %-sequences are provided: (setq energy-full (+ energy-full (string-to-number full-string)) energy-now (+ energy-now - (string-to-number now-string))))))))) + (string-to-number now-string)))))) + (goto-char (point-min)) + (when (and energy-now rate (not (zerop rate)) + (re-search-forward "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t)) + (let ((remaining (if (string= charging-state "Discharging") + energy-now + (- energy-full energy-now)))) + (setq hours (/ (/ (* remaining (string-to-number (match-string 1))) rate) + 10000000.0))))))) (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0)) (number-to-string charge-now)) ((or (> energy-full 0) (> energy-now 0)) (number-to-string energy-now)) (t "N/A"))) + (cons ?r (if rate + (format "%.1f" (/ rate 1000000.0)) + "N/A")) + (cons ?m (if hours + (format "%d" (* hours 60)) + "N/A")) + (cons ?h (if hours + (format "%d" hours) + "N/A")) + (cons ?t (if hours + (format "%d:%02d" hours (* (- hours (floor hours)) 60)) + "N/A")) + (cons ?d (or temperature "N/A")) (cons ?B (or charging-state "N/A")) (cons ?p (cond ((> charge-full 0) (format "%.1f" -- 1.7.2.5