7 #define POWER_BASEDIR "/sys/class/power_supply/"
11 void status_battery(char *batname)
21 int batt_percent = -1;
25 float batt_watts = -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);
42 strcpy(&batpath[batpathlen], "/energy_now");
43 stlen = fileRead(stline, sizeof(stline), batpath);
45 batt_now = atoi(stline);
47 strcpy(&batpath[batpathlen], "/energy_full");
48 stlen = fileRead(stline, sizeof(stline), batpath);
50 batt_full = atoi(stline);
52 strcpy(&batpath[batpathlen], "/current_now");
53 stlen = fileRead(stline, sizeof(stline), batpath);
55 batt_current = atoi(stline);
57 strcpy(&batpath[batpathlen], "/voltage_now");
58 stlen = fileRead(stline, sizeof(stline), batpath);
60 batt_voltage = atoi(stline);
65 batt_percent = batt_now / (batt_full / 100);
67 if (batt_percent <= 40) // 40
69 if (batt_percent <= 25) // 25
71 if (batt_percent <= 10) // 10
72 fputs("^fg(red)", stdout);
74 fputs("^fg(orange)", stdout);
77 fputs("^fg(yellow)", stdout);
81 if (batt_percent > 70) // 70
82 fputs("^fg(white)", stdout);
84 fputs("^fg(green)", stdout);
87 batt_time = (float)batt_now / (float)batt_current;
88 batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000);
90 if (batt_watts == 0) // fully charged and not in use
91 printf(" %s: %d%% _ _ ", batname, batt_percent);
93 printf(" %s: %d%% %.1fh %.1fW ",
94 batname, batt_percent, batt_time, batt_watts);