Moved battery status to fileRead().
authornorly <ny-git@enpas.org>
Tue, 14 Feb 2012 13:35:44 +0000 (13:35 +0000)
committernorly <ny-git@enpas.org>
Tue, 14 Feb 2012 13:35:44 +0000 (13:35 +0000)
Renamed 'power' to 'battery'.

config.h
statuses/Makefile
statuses/battery.c [new file with mode: 0644]
statuses/battery.h [new file with mode: 0644]
statuses/power.c [deleted file]
statuses/power.h [deleted file]
statuses/tools.c
sysstatus.c

index 53f1c580337f2767062f8be11ba9861371475779..91ad943ae491d23f99091dbebb2b963d0ef58285 100644 (file)
--- 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
index 5f8162a08b771cb89478a8b1d5125161d329facf..da19ecbeeb57fa6344cba20eb3de7777a11a86b6 100644 (file)
@@ -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 (file)
index 0000000..e3e3c1b
--- /dev/null
@@ -0,0 +1,95 @@
+#include <stdio.h>
+#include <stdlib.h>
+#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 (file)
index 0000000..0824392
--- /dev/null
@@ -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 (file)
index 47277d0..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdlib.h>
-#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 (file)
index db06166..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-
-#ifndef __POWER_H__
-#define __POWER_H__
-
-void status_power();
-
-#endif
index 7a4a677fc270a8ee4aebfcf2a28e65ef8353a29c..83fe2b1ab86aa8ebae45cef7a996360fb3ceeae8 100644 (file)
@@ -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
+}
index 4e5d6c85bdea92b93c2262f87172daa4206c0ca2..f5e980c82f654df68c0ea980bbec64ba5161ba10 100644 (file)
@@ -2,14 +2,14 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
-#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();