8 #define POWER_BASEDIR "/sys/class/power_supply/"
12 void status_battery(char *batname)
22 int batt_percent = -1;
27 float batt_watts = -1;
31 batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
32 if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath))
34 statusError("status_battery",
35 "batpath buffer too small",
39 strcpy(batpath, POWER_BASEDIR);
40 strcat(batpath, batname);
43 // Is the battery present?
44 if (access(batpath, F_OK))
46 //printf(" ^fg(grey)[%s] ", batname);
52 strcpy(&batpath[batpathlen], "/energy_now");
53 stlen = fileRead(stline, sizeof(stline), batpath);
55 batt_now = atoi(stline);
57 strcpy(&batpath[batpathlen], "/energy_full");
58 stlen = fileRead(stline, sizeof(stline), batpath);
60 batt_full = atoi(stline);
62 strcpy(&batpath[batpathlen], "/power_now");
63 stlen = fileRead(stline, sizeof(stline), batpath);
65 batt_power = atoi(stline);
67 strcpy(&batpath[batpathlen], "/voltage_now");
68 stlen = fileRead(stline, sizeof(stline), batpath);
70 batt_voltage = atoi(stline);
75 batt_percent = batt_now / (batt_full / 100);
77 if (batt_percent <= 40) // 40
79 if (batt_percent <= 25) // 25
81 if (batt_percent <= 10) // 10
82 fputs("^fg(red)", stdout);
84 fputs("^fg(orange)", stdout);
87 fputs("^fg(yellow)", stdout);
91 if (batt_percent > 70) // 70
92 fputs("^fg(white)", stdout);
94 fputs("^fg(green)", stdout);
97 batt_current = ((float)batt_power * 100) / ((float)batt_voltage / 100000);
98 batt_time = (float)batt_now / (float)batt_current;
99 batt_watts = ((float)batt_power / 1000000);
101 if (batt_watts == 0) // fully charged and not in use
102 printf(" %s: %d%% _ _ ", batname, batt_percent);
104 printf(" %s: %d%% %.1fh %.1fW ",
105 batname, batt_percent, batt_time, batt_watts);