6 #include "status/battery.h"
10 #define POWER_BASEDIR "/sys/class/power_supply/"
14 void status_battery(char *batname)
24 int chargePercent = -1;
30 batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
31 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)) {
43 //printf(" ^fg(grey)[%s] ", batname);
49 strcpy(&batpath[batpathlen], "/energy_now");
50 stlen = fileRead(stline, sizeof(stline), batpath);
52 chargeNow = atoi(stline);
55 strcpy(&batpath[batpathlen], "/energy_full");
56 stlen = fileRead(stline, sizeof(stline), batpath);
58 chargeFull = atoi(stline);
61 strcpy(&batpath[batpathlen], "/power_now");
62 stlen = fileRead(stline, sizeof(stline), batpath);
70 chargePercent = chargeNow / (chargeFull / 100);
73 if (chargePercent <= 40) {
74 if (chargePercent <= 25) {
75 if (chargePercent <= 10) {
76 fputs("^fg(red)", stdout);
79 fputs("^fg(orange)", stdout);
83 fputs("^fg(yellow)", stdout);
86 if (chargePercent > 70) {
87 fputs("^fg(white)", stdout);
90 fputs("^fg(green)", stdout);
94 battTime = (float)chargeNow / (float)battW;
97 // 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);