9 * Get battery information from /sys. Note that it uses the design capacity to
10 * calculate the percentage, not the last full capacity, so you can see how
11 * worn off your battery is.
14 const char *get_battery_info(struct battery *bat) {
16 static char part[512];
21 charging_status_t status = CS_DISCHARGING;
23 slurp(bat->path, buf, sizeof(buf));
25 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
34 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
35 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
36 remaining = atoi(walk+1);
37 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
38 present_rate = atoi(walk+1);
39 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
41 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
44 /* The only thing left is the full capacity */
45 if (bat->use_last_full) {
46 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
47 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
50 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
51 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
55 full_design = atoi(walk+1);
59 if ((full_design == 1) || (remaining == -1))
62 if (present_rate > 0) {
64 int seconds, hours, minutes;
65 if (status == CS_CHARGING)
66 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
67 else if (status == CS_DISCHARGING)
68 remaining_time = ((float)remaining / (float)present_rate);
69 else remaining_time = 0;
71 seconds = (int)(remaining_time * 3600.0);
72 hours = seconds / 3600;
73 seconds -= (hours * 3600);
74 minutes = seconds / 60;
75 seconds -= (minutes * 60);
77 (void)snprintf(part, sizeof(part), "%s %.02f%% %02d:%02d:%02d",
78 (status == CS_CHARGING ? "CHR" :
79 (status == CS_DISCHARGING ? "BAT" : "FULL")),
80 (((float)remaining / (float)full_design) * 100),
81 max(hours, 0), max(minutes, 0), max(seconds, 0));
83 (void)snprintf(part, sizeof(part), "%s %.02f%%",
84 (status == CS_CHARGING ? "CHR" :
85 (status == CS_DISCHARGING ? "BAT" : "FULL")),
86 (((float)remaining / (float)full_design) * 100));