From b0d7ca3ad79f3fc191f9fe83ebc4398cdf64b6f2 Mon Sep 17 00:00:00 2001 From: norly Date: Tue, 14 Feb 2012 13:35:44 +0000 Subject: [PATCH] Moved battery status to fileRead(). Renamed 'power' to 'battery'. --- config.h | 1 + statuses/Makefile | 2 +- statuses/battery.c | 95 ++++++++++++++++++++++++++++++++++++++++++ statuses/battery.h | 7 ++++ statuses/power.c | 101 --------------------------------------------- statuses/power.h | 7 ---- statuses/tools.c | 2 +- sysstatus.c | 10 ++--- 8 files changed, 110 insertions(+), 115 deletions(-) create mode 100644 statuses/battery.c create mode 100644 statuses/battery.h delete mode 100644 statuses/power.c delete mode 100644 statuses/power.h diff --git a/config.h b/config.h index 53f1c58..91ad943 100644 --- a/config.h +++ b/config.h @@ -9,6 +9,7 @@ #define NUM_CPUS 2 #define CPU_HISTORY_SIZE 10 +#define POWER_BASEDIR "/sys/class/power_supply/" #define NETIF_BASEDIR "/sys/class/net/" #endif diff --git a/statuses/Makefile b/statuses/Makefile index 5f8162a..da19ecb 100644 --- a/statuses/Makefile +++ b/statuses/Makefile @@ -1,4 +1,4 @@ -STATUSES=cpuusage.o datetime.o memusage.o netif.o power.o temp.o uptime.o volume_alsa.o +STATUSES=battery.o cpuusage.o datetime.o memusage.o netif.o temp.o uptime.o volume_alsa.o OTHERS=tools.o all: *.c *.h ../config.h Makefile $(STATUSES) $(OTHERS) diff --git a/statuses/battery.c b/statuses/battery.c new file mode 100644 index 0000000..e3e3c1b --- /dev/null +++ b/statuses/battery.c @@ -0,0 +1,95 @@ +#include +#include +#include "string.h" +#include "battery.h" + +#ifndef POWER_BASEDIR + #define POWER_BASEDIR "/sys/class/power_supply/" +#endif + + +void status_battery(char *batname) +{ + char batpath[256]; + int batpathlen; + + char stline[16]; + ssize_t stlen; + + int batt_now = 0; + int batt_full = -1; + int batt_percent = -1; + int batt_current = 1; + int batt_voltage = 0; + float batt_time = -1; + float batt_watts = -1; + + + // Prepare path + batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname); + if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) + { + statusError("status_battery", + "batpath buffer too small", + batname); + return; + } + strcpy(batpath, POWER_BASEDIR); + strcat(batpath, batname); + + + // Get info + strcpy(&batpath[batpathlen], "/energy_now"); + stlen = fileRead(stline, sizeof(stline), batpath); + if (stlen > 0) + batt_now = atoi(stline); + + strcpy(&batpath[batpathlen], "/energy_full"); + stlen = fileRead(stline, sizeof(stline), batpath); + if (stlen > 0) + batt_full = atoi(stline); + + strcpy(&batpath[batpathlen], "/current_now"); + stlen = fileRead(stline, sizeof(stline), batpath); + if (stlen > 0) + batt_current = atoi(stline); + + strcpy(&batpath[batpathlen], "/voltage_now"); + stlen = fileRead(stline, sizeof(stline), batpath); + if (stlen > 0) + batt_voltage = atoi(stline); + + + // Prettyprint + if (batt_full > 0) + batt_percent = batt_now / (batt_full / 100); + + if (batt_percent <= 40) // 40 + { + if (batt_percent <= 25) // 25 + { + if (batt_percent <= 10) // 10 + fputs("^fg(red)", stdout); + else // 11-25% + fputs("^fg(orange)", stdout); + } + else // 26-40% + fputs("^fg(yellow)", stdout); + } + else + { + if (batt_percent > 70) // 70 + fputs("^fg(white)", stdout); + else // 41-70% + fputs("^fg(green)", stdout); + } + + batt_time = (float)batt_now / (float)batt_current; + batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000); + + if (batt_watts == 0) // fully charged and not in use + printf(" %s: %d%% _ _ ", batname, batt_percent); + else + printf(" %s: %d%% %.1fh %.1fW ", + batname, batt_percent, batt_time, batt_watts); +} diff --git a/statuses/battery.h b/statuses/battery.h new file mode 100644 index 0000000..0824392 --- /dev/null +++ b/statuses/battery.h @@ -0,0 +1,7 @@ + +#ifndef __BATTERY_H__ +#define __BATTERY_H__ + +void status_battery(char *batname); + +#endif diff --git a/statuses/power.c b/statuses/power.c deleted file mode 100644 index 47277d0..0000000 --- a/statuses/power.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include -#include -#include "power.h" - -void status_power() -{ - char *stline; - size_t stlen; - FILE *stfile; - - int batt_now = 0; - int batt_full = -1; - int batt_percent = -1; - int batt_current = 1; - int batt_voltage = 0; - float batt_time = -1; - float batt_watts = -1; - - stfile = fopen("/sys/class/power_supply/BAT0/energy_now", "r"); - if (stfile != NULL) - { - stline = NULL; - stlen = getline(&stline, &stlen, stfile); - fclose(stfile); - - batt_now = atoi(stline); - free(stline); - } - - stfile = fopen("/sys/class/power_supply/BAT0/energy_full", "r"); - if (stfile != NULL) - { - stline = NULL; - stlen = getline(&stline, &stlen, stfile); - fclose(stfile); - - batt_full = atoi(stline); - free(stline); - } - - stfile = fopen("/sys/class/power_supply/BAT0/current_now", "r"); - if (stfile != NULL) - { - stline = NULL; - stlen = getline(&stline, &stlen, stfile); - fclose(stfile); - - batt_current = atoi(stline); - free(stline); - } - - stfile = fopen("/sys/class/power_supply/BAT0/voltage_now", "r"); - if (stfile != NULL) - { - stline = NULL; - stlen = getline(&stline, &stlen, stfile); - fclose(stfile); - - batt_voltage = atoi(stline); - free(stline); - } - - // Hack for chinese battery. - //batt_now -= 27220000; - //batt_full = 27000000; - - if (batt_full > 0) - batt_percent = batt_now / (batt_full / 100); - - if (batt_percent <= 40) // 40 - { - if (batt_percent <= 25) // 25 - { - if (batt_percent <= 10) // 10 - fputs("^fg(red)", stdout); - else // 11-25% - fputs("^fg(orange)", stdout); - } - else // 26-40% - fputs("^fg(yellow)", stdout); - } - else - { - if (batt_percent > 70) // 70 - fputs("^fg(white)", stdout); - else // 41-70% - fputs("^fg(green)", stdout); - } - - batt_time = (float)batt_now / (float)batt_current; - batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000); - - if (batt_watts == 0) // fully charged and on AC - printf(" BAT0: %d%% _ _ ", batt_percent); - else - printf(" BAT0: %d%% %.1fh %.1fW ", batt_percent, batt_time, batt_watts); - - //fputs(" Power status ", stdout); -} diff --git a/statuses/power.h b/statuses/power.h deleted file mode 100644 index db06166..0000000 --- a/statuses/power.h +++ /dev/null @@ -1,7 +0,0 @@ - -#ifndef __POWER_H__ -#define __POWER_H__ - -void status_power(); - -#endif diff --git a/statuses/tools.c b/statuses/tools.c index 7a4a677..83fe2b1 100644 --- a/statuses/tools.c +++ b/statuses/tools.c @@ -54,4 +54,4 @@ ssize_t fileRead(char *buf, size_t bufsize, char *file) buf[0] = '\0'; return readbytes; -} \ No newline at end of file +} diff --git a/sysstatus.c b/sysstatus.c index 4e5d6c8..f5e980c 100644 --- a/sysstatus.c +++ b/sysstatus.c @@ -2,14 +2,14 @@ #include #include #include -#include "statuses/uptime.h" -#include "statuses/memusage.h" +#include "statuses/battery.h" #include "statuses/cpuusage.h" +#include "statuses/datetime.h" +#include "statuses/memusage.h" #include "statuses/netif.h" -#include "statuses/power.h" #include "statuses/volume_alsa.h" #include "statuses/temp.h" -#include "statuses/datetime.h" +#include "statuses/uptime.h" #include "config.h" void updatestatus() @@ -18,7 +18,7 @@ void updatestatus() status_cpuusage(); - status_power(); + status_battery("BAT0"); status_memusage(); -- 2.30.2