10 #define POWER_BASEDIR "/sys/class/power_supply/"
14 void status_battery(GlobalData *g, char *batname)
17 char text[32] = { 0 };
27 int chargePercent = -1;
37 batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
38 if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath)) {
39 statusError("status_battery",
40 "batpath buffer too small",
44 strcpy(batpath, POWER_BASEDIR);
45 strcat(batpath, batname);
48 /* Is the battery present? */
49 if (access(batpath, F_OK)) {
50 //printf(" ^fg(grey)[%s] ", batname);
56 strcpy(&batpath[batpathlen], "/charge_now");
57 stlen = fileRead(stline, sizeof(stline), batpath);
59 chargeNow = atoi(stline);
62 strcpy(&batpath[batpathlen], "/charge_full");
63 stlen = fileRead(stline, sizeof(stline), batpath);
65 chargeFull = atoi(stline);
68 strcpy(&batpath[batpathlen], "/current_now");
69 stlen = fileRead(stline, sizeof(stline), batpath);
71 currentNow = atoi(stline);
74 strcpy(&batpath[batpathlen], "/voltage_now");
75 stlen = fileRead(stline, sizeof(stline), batpath);
77 voltageNow = atoi(stline);
83 chargePercent = chargeNow / (chargeFull / 100);
86 if (chargePercent <= 40) {
87 if (chargePercent <= 25) {
88 if (chargePercent <= 10) {
89 s.color = "#FF0000"; // red
92 s.color = "#FFA500"; // orange
96 s.color = "#FFFF00"; // yellow
99 if (chargePercent > 70) {
100 s.color = "#FFFFFF"; // white
103 s.color = "#22FF22"; // green
107 battTime = (float)chargeNow / (float)currentNow;
109 if (currentNow == 0) {
110 // fully charged and not in use
111 snprintf(text, sizeof(text), "%s: %d%% _ _",
112 batname, chargePercent);
114 snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
115 batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
118 line_append_item(g, &s);