unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] battery.el Retrieve more information from sysfs
@ 2011-12-18 18:53 Jérémy Compostella
  2011-12-21 10:03 ` Jérémy Compostella
  2011-12-22 22:52 ` Stefan Monnier
  0 siblings, 2 replies; 10+ messages in thread
From: Jérémy Compostella @ 2011-12-18 18:53 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

All,

I just bought a new laptop and installed a fresh Debian on it. I used to
look at my current battery status using Emacs. I like to look at the
current rate too since this laptop will be used during a long trip and I
want to take care of the consumption in real time.

The issue is that with the 2.6.32 kernel from Debian for this laptop I
do not have the /proc/acpi/battery part. However, the sysfs is able to
report all the data I need. The current Emacs battery module is not able
to retrieve the information I need from sysfs but it does from
/proc/acpi/battery. So I made a small evolution to enable the following:

- Get the current rate
- Get the current temperature (does not work on my laptop but the
  power_supply interface is able to provide it, so I added it too.
- Do the time remaining estimation using the current voltage, the
  remaining capacity and the current rate. It works on both charging and
  discharging state.

I attached the patch but I warn you that it has been generated using git
since I was unable to retrieve the bazar repository for the last two
days (it hangs during the download process). By the way, do you have any
advice which could help me ?

Please merge it or review it.

Best regards,

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: battery.el: Retrieve more information from sysfs patch --]
[-- Type: text/x-diff, Size: 3556 bytes --]

From 905c2ccd805bb6be0ed11e4f85f9baf316106da1 Mon Sep 17 00:00:00 2001
From: Jeremy Compostella <jeremy.compostella@gmail.com>
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


[-- Attachment #3: Type: text/plain, Size: 97 bytes --]


Jeremy

PS: I signed my Emacs assignment and post it by mail 10 days ago.
--
Sent from my Emacs

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2011-12-23  7:10 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-18 18:53 [PATCH] battery.el Retrieve more information from sysfs Jérémy Compostella
2011-12-21 10:03 ` Jérémy Compostella
2011-12-21 19:30   ` Karl Fogel
2011-12-22  0:08     ` Kan-Ru Chen
2011-12-22 10:18       ` Jérémy Compostella
2011-12-22 11:30     ` Roland Winkler
2011-12-22 22:52 ` Stefan Monnier
2011-12-23  3:59   ` Richard Stallman
2011-12-23  4:38     ` Karl Fogel
2011-12-23  7:10       ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).