[gpm] ACPI battery rate information from HAL



I've been doing some testing, where instead of trusting HAL (i.e. what
ACPI tells HAL), working out the charge rate using the difference
between divided by the time elapsed between updates.

The numbers are pretty similar when in the middle of the discharge:

** (gnome-power-manager:14501): DEBUG: GPM calculated rate = 20763
** (gnome-power-manager:14501): DEBUG: HAL calculated rate = 19213

But at the extremes (i.e. >80% charged, and <20%) it would appear that
the difference is considerable:

** (gnome-power-manager:18951): DEBUG: GPM calculated rate = 2354
** (gnome-power-manager:18951): DEBUG: HAL calculated rate = 23180

Where the HAL (h/w) value seems to think I'll be charged in 5 minutes,
but the GPM (s/w) value seems to think it's going to take nearer 50
minutes.

In reality it's closer to the second value (~40 minutes), certainly not
5! This seems to be about the same for discharge, although to a lesser
extent.

I'm guessing this is the acpi battery doing a non-linear charge rate
approximation, somehow normalised, but I'm somewhat confused.

Patch attached, which is for discussion, rather than actually using in
g-p-m -- any such "re-adjustment" should probably be done in HAL if
required.

Richard

Index: src/gpm-power.c
===================================================================
RCS file: /cvs/gnome/gnome-power-manager/src/gpm-power.c,v
retrieving revision 1.22
diff -u -r1.22 gpm-power.c
--- src/gpm-power.c	11 Feb 2006 19:29:10 -0000	1.22
+++ src/gpm-power.c	11 Feb 2006 19:50:29 -0000
@@ -87,6 +87,8 @@
 
 typedef struct {
 	char		       *udi;
+	int		        last_charge;
+	GTimer		       *timer;
 	GpmPowerBatteryKind	battery_kind;
 	GpmPowerBatteryStatus	battery_status;
 } BatteryDeviceCacheEntry;
@@ -155,6 +157,8 @@
 				&status->last_full_charge);
 	gpm_hal_device_get_int (udi, "battery.charge_level.current",
 				&status->current_charge);
+	/* assign this a fixed value */
+	entry->last_charge = status->current_charge;
 
 	/* battery might not be rechargeable, have to check */
 	gpm_hal_device_get_bool (udi, "battery.is_rechargeable",
@@ -233,8 +237,27 @@
 	} else if (strcmp (key, "battery.charge_level.current") == 0) {
 		gpm_hal_device_get_int (udi, key, &status->current_charge);
 
+		gdouble time_elapsed;
+		int charge_rate_new;
+
+		/* get hours elapsed since last update */
+		time_elapsed = g_timer_elapsed (entry->timer, NULL);
+		time_elapsed = time_elapsed / (60.0f * 60.0f);
+
+		charge_rate_new = abs ((int) ((entry->last_charge - status->current_charge) / time_elapsed));
+		g_debug ("GPM calculated rate = %i", charge_rate_new);
+		g_debug ("HAL calculated rate = %i", status->charge_rate);
+
+		/* THIS OVERWRITES THE HAL VALUE */
+		status->charge_rate = charge_rate_new;
+
+		g_timer_start (entry->timer);
+		entry->last_charge = status->current_charge;
+
 	} else if (strcmp (key, "battery.charge_level.rate") == 0) {
+		/* COMMENTED OUT FOR A TEST
 		gpm_hal_device_get_int (udi, key, &status->charge_rate);
+		*/
 		/* FIXME: following can be removed if bug #5752 of hal on freedesktop
 		   gets fixed and is part of a new release of HAL and we depend on that
 		   version*/
@@ -261,6 +284,9 @@
 	entry = g_new0 (BatteryDeviceCacheEntry, 1);
 
 	entry->udi = g_strdup (udi);
+	entry->timer = g_timer_new ();
+	entry->last_charge = 0;
+
 	battery_device_cache_entry_update_all (entry);
 
 	return entry;
@@ -270,6 +296,7 @@
 battery_device_cache_entry_free (BatteryDeviceCacheEntry *entry)
 {
 	g_free (entry->udi);
+	g_timer_destroy (entry->timer);
 	g_free (entry);
 	entry = NULL;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]