From: Creamy Goodness Date: Wed, 8 Sep 2010 03:16:44 +0000 (-0600) Subject: fix to detect n900 battery; add support to built in battery variables to work with... X-Git-Url: http://vcs.maemo.org/git/?p=monky;a=commitdiff_plain;h=492b86773cdd4d6c993dee8e02c0c93302ccd7ed fix to detect n900 battery; add support to built in battery variables to work with maemo 'sysfs' --- diff --git a/src/conky.c b/src/conky.c index bf0f18e..02473ae 100644 --- a/src/conky.c +++ b/src/conky.c @@ -3540,7 +3540,7 @@ static void main_loop(void) if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) { char buf[max_user_text]; - get_battery_short_status(buf, max_user_text, "BAT0"); + get_battery_short_status(buf, max_user_text, "bq27200-0"); //why is this even hardcoded here? oh well. if(buf[0] == 'D') { update_interval = update_interval_bat; } else { diff --git a/src/linux.c b/src/linux.c index ce5f733..3c0c1e9 100644 --- a/src/linux.c +++ b/src/linux.c @@ -1549,10 +1549,24 @@ present voltage: 16608 mV On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_* */ +/*on n900 with power kernel: (non power kernel could use "hal-device bme" and do something else) +/sys/class/power_supply/bq27200-0/uevent +PHYSDEVPATH=/class/i2c-adapter/i2c-2/2-0055 +PHYSDEVBUS=i2c +PHYSDEVDRIVER=bq27200-battery +POWER_SUPPLY_NAME=bq27200-0 +POWER_SUPPLY_TYPE=Battery +POWER_SUPPLY_PRESENT=1 << this is always 1, it means the battery is inserted +POWER_SUPPLY_VOLTAGE_NOW=4039 << this keeps updating during charging +POWER_SUPPLY_CURRENT_NOW=1960 << this goes negative when charging! +POWER_SUPPLY_CAPACITY=98 << supposed to be the %, but it stops updating when charging until it hits 100% +POWER_SUPPLY_TEMP=39 << only temperature sensor in n900 :( +*/ + #define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply" -#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" -#define APM_PATH "/proc/apm" -#define MAX_BATTERY_COUNT 4 +#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" //not for n900 +#define APM_PATH "/proc/apm" //not for n900 +#define MAX_BATTERY_COUNT 4 //more like 1 static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL }; @@ -1646,11 +1660,14 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) if (sysfs_bat_fp[idx] != NULL) { /* SYSFS */ - int present_rate = -1; - int remaining_capacity = -1; - char charging_state[64]; + int present_rate = -9999; //we will put "current now" into this. negative when charging! + int remaining_capacity = -1; //in % + char charging_state[64];//can't get this without hal bme + int voltage = -1; + //int capacity = -1; + int temp = 9999; char present[4]; - + strcpy(present, "no"); strcpy(charging_state, "unknown"); while (!feof(sysfs_bat_fp[idx])) { @@ -1660,100 +1677,108 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) /* let's just hope units are ok */ if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0) - strcpy(present, "yes"); - else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0) - strcpy(present, "no"); - else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0) - sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state); - /* present_rate is not the same as the - current flowing now but it is the same value - which was used in the past. so we continue - the tradition! */ + strcpy(present, "yes");//n900 always yes + else if (strncmp(buf, "POWER_SUPPLY_VOLTAGE_NOW=", 25) == 0) + sscanf(buf, "POWER_SUPPLY_VOLTAGE_NOW=%d", &voltage); +// else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0) +// sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state); else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0) sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate); - else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) - sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); - else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0) - sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]); - else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) - sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); - else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0) - sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]); + else if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) + sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity); + else if (strncmp(buf, "POWER_SUPPLY_TEMP=", 18) == 0) + sscanf(buf, "POWER_SUPPLY_TEMP=%d", &temp); +// else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) +// sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); +// else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0) +// sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]); +// else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) +// sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); +// else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0) +// sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]); } fclose(sysfs_bat_fp[idx]); sysfs_bat_fp[idx] = NULL; /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */ - if (remaining_capacity > acpi_last_full[idx]) - acpi_last_full[idx] = remaining_capacity; /* normalize to 100% */ + //lance notes, we don't care +// if (remaining_capacity > acpi_last_full[idx]) +// acpi_last_full[idx] = remaining_capacity; /* normalize to 100% */ /* not present */ - if (strcmp(present, "No") == 0) { - strncpy(last_battery_str[idx], "not present", 64); - } + //not possible +// if (strcmp(present, "no") == 0) { +// strncpy(last_battery_str[idx], "not present", 64); +// } /* charging */ - else if (strcmp(charging_state, "Charging") == 0) { - if (acpi_last_full[idx] != 0 && present_rate > 0) { + if (present_rate <= 0) {//if charging /* e.g. charging 75% */ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", - (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 )); - /* e.g. 2h 37m */ - format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, - (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600)); - } else if (acpi_last_full[idx] != 0 && present_rate <= 0) { - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1); - snprintf(last_battery_time_str[idx], + (int) (((float) remaining_capacity / 100) )); + snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } - } + /* e.g. 2h 37m */ +// format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, +// (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600)); + } + +// else if (acpi_last_full[idx] != 0 && present_rate <= 0) {//supposed to be guessing the time to fully charge, won't work though. +// snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%", +// (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); +// snprintf(last_battery_time_str[idx], +// sizeof(last_battery_time_str[idx]) - 1, "unknown"); +// } else { +// strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1); +// snprintf(last_battery_time_str[idx], +// sizeof(last_battery_time_str[idx]) - 1, "unknown"); +// } +// } /* discharging */ - else if (strncmp(charging_state, "Discharging", 64) == 0) { - if (present_rate > 0) { + else if (present_rate > 0) { + /* e.g. discharging 35% */ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", - (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 )); - /* e.g. 1h 12m */ - format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, - (long) (((float) remaining_capacity / present_rate) * 3600)); - } else if (present_rate == 0) { /* Thanks to Nexox for this one */ - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full"); - snprintf(last_battery_time_str[idx], - sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } else { - snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, - "discharging %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - snprintf(last_battery_time_str[idx], + (int) (((float) remaining_capacity / 100) )); + + snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown"); - } + /* e.g. 1h 12m */ +// format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1, +// (long) (((float) remaining_capacity / present_rate) * 3600)); +// else { +// snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, +// "discharging %d%%", +// (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); +// snprintf(last_battery_time_str[idx], +// sizeof(last_battery_time_str[idx]) - 1, "unknown"); +// } } /* charged */ /* thanks to Lukas Zapletal */ - else if (strncmp(charging_state, "Charged", 64) == 0 || strncmp(charging_state, "Full", 64) == 0) { - /* Below happens with the second battery on my X40, - * when the second one is empty and the first one - * being charged. */ - if (remaining_capacity == 0) - strcpy(last_battery_str[idx], "empty"); - else - strcpy(last_battery_str[idx], "charged"); - } + + if (remaining_capacity == 100) + strcpy(last_battery_str[idx], "charged"); + +// else if (strncmp(charging_state, "Charged", 64) == 0 || strncmp(charging_state, "Full", 64) == 0) { +// /* Below happens with the second battery on my X40, +// * when the second one is empty and the first one +// * being charged. */ +// if (remaining_capacity == 0) +// strcpy(last_battery_str[idx], "empty"); +// else +// strcpy(last_battery_str[idx], "charged"); +// } /* unknown, probably full / AC */ - else { - if (acpi_last_full[idx] != 0 - && remaining_capacity != acpi_last_full[idx]) - snprintf(last_battery_str[idx], 64, "unknown %d%%", - (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); - else - strncpy(last_battery_str[idx], "AC", 64); - } - } else if (acpi_bat_fp[idx] != NULL) { +// else { +// if (acpi_last_full[idx] != 0 +// && remaining_capacity != acpi_last_full[idx]) +// snprintf(last_battery_str[idx], 64, "unknown %d%%", +// (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100)); +// else +// strncpy(last_battery_str[idx], "AC", 64); +// } + } else if (acpi_bat_fp[idx] != NULL) {//skipped on n900 /* ACPI */ int present_rate = -1; int remaining_capacity = -1; @@ -1887,7 +1912,7 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item) } fclose(acpi_bat_fp[idx]); acpi_bat_fp[idx] = NULL; - } else { + } else {//also skipped on n900 /* APM */ if (apm_bat_fp[idx] == NULL) { apm_bat_fp[idx] = open_file(APM_PATH, &rep2); @@ -1996,18 +2021,20 @@ int get_battery_perct(const char *bat) char buf[256]; if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL) break; - - if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) { - sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); - } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) { - sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]); - } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) { - sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); - } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) { - sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]); + if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) { + sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity); + +// if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) { +// sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity); +// } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) { +// sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]); +// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) { +// sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity); +// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) { +// sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]); +// } } } - fclose(sysfs_bat_fp[idx]); sysfs_bat_fp[idx] = NULL; @@ -2055,9 +2082,10 @@ int get_battery_perct(const char *bat) return 0; } /* compute the battery percentage */ - last_battery_perct[idx] = - (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100); - if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100; + last_battery_perct[idx] = + remaining_capacity; + //(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100); + //if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100; return last_battery_perct[idx]; }