summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Schaefer <trashzopf@googlemail.com>2012-07-30 15:40:56 +0200
committerMichael Stapelberg <michael@stapelberg.de>2012-08-13 01:44:40 +0200
commit3baf27bf1d9b51fa8de3203a64993de2464ed356 (patch)
tree72fa418d34952d6ea4176de6d2c98a0a7cd75880
parentcfaeba4df17397ac8d99155bc166bba90db0dbbf (diff)
use correct units for battery calculation
-rw-r--r--src/print_battery_info.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/print_battery_info.c b/src/print_battery_info.c
index b992efb..3e130c7 100644
--- a/src/print_battery_info.c
+++ b/src/print_battery_info.c
@@ -40,11 +40,11 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
char consumptionbuf[256];
const char *walk, *last;
char *outwalk = buffer;
+ bool watt_as_unit;
int full_design = -1,
remaining = -1,
present_rate = -1,
- voltage = -1,
- current = -1;
+ voltage = -1;
charging_status_t status = CS_DISCHARGING;
memset(statusbuf, '\0', sizeof(statusbuf));
@@ -72,13 +72,22 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
if (*walk != '=')
continue;
- if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
- BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
+ if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
+ watt_as_unit = true;
remaining = atoi(walk+1);
+ }
+ else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
+ watt_as_unit = false;
+ remaining = atoi(walk+1);
+ }
else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
- current = atoi(walk+1);
+ present_rate = atoi(walk+1);
else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
voltage = atoi(walk+1);
+ /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
+ * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
+ * unit instead of μAh. We will calculate it as we need it
+ * later. */
else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
present_rate = atoi(walk+1);
else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
@@ -101,9 +110,15 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char
}
}
- /* on some systems POWER_SUPPLY_POWER_NOW does not exist, so we have to calculate it */
- if (present_rate == -1)
- present_rate = ((float)voltage / 1000.0) * ((float)current / 1000.0);
+ /* the difference between POWER_SUPPLY_ENERGY_NOW and
+ * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
+ * given in mWh, the charge in mAh. So calculate every value given in
+ * ampere to watt */
+ if (!watt_as_unit) {
+ present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
+ remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
+ full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
+ }
if ((full_design == -1) || (remaining == -1)) {
OUTPUT_FULL_TEXT("No battery");