8 #define POWER_BASEDIR "/sys/class/power_supply/"
12 void status_battery(char *batname)
22 int chargePercent = -1;
29 batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
30 if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
31 statusError("status_battery",
32 "batpath buffer too small",
36 strcpy(batpath, POWER_BASEDIR);
37 strcat(batpath, batname);
40 /* Is the battery present? */
41 if (access(batpath, F_OK)) {
42 //printf(" ^fg(grey)[%s] ", batname);
48 strcpy(&batpath[batpathlen], "/energy_now");
49 stlen = fileRead(stline, sizeof(stline), batpath);
51 chargeNow = atoi(stline);
54 strcpy(&batpath[batpathlen], "/energy_full");
55 stlen = fileRead(stline, sizeof(stline), batpath);
57 chargeFull = atoi(stline);
60 strcpy(&batpath[batpathlen], "/power_now");
61 stlen = fileRead(stline, sizeof(stline), batpath);
66 strcpy(&batpath[batpathlen], "/voltage_now");
67 stlen = fileRead(stline, sizeof(stline), batpath);
75 chargePercent = chargeNow / (chargeFull / 100);
78 if (chargePercent <= 40) {
79 if (chargePercent <= 25) {
80 if (chargePercent <= 10) {
81 fputs("^fg(red)", stdout);
84 fputs("^fg(orange)", stdout);
88 fputs("^fg(yellow)", stdout);
91 if (chargePercent > 70) {
92 fputs("^fg(white)", stdout);
95 fputs("^fg(green)", stdout);
99 battTime = (float)chargeNow / (float)battW;
102 // fully charged and not in use
103 printf(" %s: %d%% _ _ ",
104 batname, chargePercent);
106 printf(" %s: %d%% %.1fh %.1fW ",
107 batname, chargePercent, battTime, (float)battW / 1000000.0);