From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?utf-8?Q?J=C3=A9r=C3=A9my_Compostella?= Newsgroups: gmane.emacs.devel Subject: [PATCH] battery.el Retrieve more information from sysfs Date: Sun, 18 Dec 2011 19:53:48 +0100 Message-ID: <87r50190eb.fsf@Apollo.jerryland.fr> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1324234426 7356 80.91.229.12 (18 Dec 2011 18:53:46 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 18 Dec 2011 18:53:46 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Dec 18 19:53:42 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RcLri-00077Z-1Y for ged-emacs-devel@m.gmane.org; Sun, 18 Dec 2011 19:53:42 +0100 Original-Received: from localhost ([::1]:38225 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RcLrh-0008M3-A9 for ged-emacs-devel@m.gmane.org; Sun, 18 Dec 2011 13:53:41 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:41660) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RcLre-0008Lj-8P for emacs-devel@gnu.org; Sun, 18 Dec 2011 13:53:39 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RcLrc-00036U-Uk for emacs-devel@gnu.org; Sun, 18 Dec 2011 13:53:38 -0500 Original-Received: from mail-we0-f169.google.com ([74.125.82.169]:58769) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RcLrc-00036K-Le for emacs-devel@gnu.org; Sun, 18 Dec 2011 13:53:36 -0500 Original-Received: by werf1 with SMTP id f1so1209653wer.0 for ; Sun, 18 Dec 2011 10:53:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:mime-version:content-type; bh=lnYQmAd84aOLAuK4FcNT0pMwkFlZaKKO9g/ekF6f2tQ=; b=ozYww2KwhuJv5M22Om74do4wciG4k0EcbNGmf1uGwHg8wGar6/KrJYTE8kiBvdmaQk VhxUwzRLG4jKXq5/Bvc+WgXYyI5X8mR1f6xQ/pgJNL6Ri3XZRbzNjGN7Qkiqtv7vSNx9 No1zoXni79aVqW+/AU9u5OX5VHwSPemcQ1pT0= Original-Received: by 10.216.132.213 with SMTP id o63mr6100034wei.11.1324234415510; Sun, 18 Dec 2011 10:53:35 -0800 (PST) Original-Received: from Apollo.jerryland.fr ([85.69.251.50]) by mx.google.com with ESMTPS id fw16sm22834992wbb.13.2011.12.18.10.53.33 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 18 Dec 2011 10:53:34 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.82.169 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:146804 Archived-At: --=-=-= Content-Type: text/plain 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, --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-battery.el-Retrieve-more-information-from-sysfs.patch Content-Description: battery.el: Retrieve more information from sysfs patch >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 --=-=-= Content-Type: text/plain Jeremy PS: I signed my Emacs assignment and post it by mail 10 days ago. -- Sent from my Emacs --=-=-=--