Renamed 'power' to 'battery'.
#define NUM_CPUS 2
#define CPU_HISTORY_SIZE 10
+#define POWER_BASEDIR "/sys/class/power_supply/"
#define NETIF_BASEDIR "/sys/class/net/"
#endif
-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)
--- /dev/null
+#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);
+}
--- /dev/null
+
+#ifndef __BATTERY_H__
+#define __BATTERY_H__
+
+void status_battery(char *batname);
+
+#endif
+++ /dev/null
-#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);
-}
+++ /dev/null
-
-#ifndef __POWER_H__
-#define __POWER_H__
-
-void status_power();
-
-#endif
buf[0] = '\0';
return readbytes;
-}
\ No newline at end of file
+}
#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()
status_cpuusage();
- status_power();
+ status_battery("BAT0");
status_memusage();