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))
32 statusError("status_battery",
33 "batpath buffer too small",
37 strcpy(batpath, POWER_BASEDIR);
38 strcat(batpath, batname);
41 // Is the battery present?
42 if (access(batpath, F_OK))
44 //printf(" ^fg(grey)[%s] ", batname);
50 strcpy(&batpath[batpathlen], "/energy_now");
51 stlen = fileRead(stline, sizeof(stline), batpath);
53 chargeNow = atoi(stline);
55 strcpy(&batpath[batpathlen], "/energy_full");
56 stlen = fileRead(stline, sizeof(stline), batpath);
58 chargeFull = atoi(stline);
60 strcpy(&batpath[batpathlen], "/power_now");
61 stlen = fileRead(stline, sizeof(stline), batpath);
65 strcpy(&batpath[batpathlen], "/voltage_now");
66 stlen = fileRead(stline, sizeof(stline), batpath);
73 chargePercent = chargeNow / (chargeFull / 100);
75 if (chargePercent <= 40) // 40
77 if (chargePercent <= 25) // 25
79 if (chargePercent <= 10) // 10
80 fputs("^fg(red)", stdout);
82 fputs("^fg(orange)", stdout);
85 fputs("^fg(yellow)", stdout);
89 if (chargePercent > 70) // 70
90 fputs("^fg(white)", stdout);
92 fputs("^fg(green)", stdout);
95 battTime = (float)chargeNow / (float)battW;
97 if (battW == 0) // fully charged and not in use
98 printf(" %s: %d%% _ _ ",
99 batname, chargePercent);
101 printf(" %s: %d%% %.1fh %.1fW ",
102 batname, chargePercent, battTime, (float)battW / 1000000.0);