15 int batt_percent = -1;
19 float batt_watts = -1;
21 stfile = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
25 stlen = getline(&stline, &stlen, stfile);
28 batt_now = atoi(stline);
32 stfile = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
36 stlen = getline(&stline, &stlen, stfile);
39 batt_full = atoi(stline);
43 stfile = fopen("/sys/class/power_supply/BAT0/current_now", "r");
47 stlen = getline(&stline, &stlen, stfile);
50 batt_current = atoi(stline);
54 stfile = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
58 stlen = getline(&stline, &stlen, stfile);
61 batt_voltage = atoi(stline);
65 // Hack for chinese battery.
66 //batt_now -= 27220000;
67 //batt_full = 27000000;
70 batt_percent = batt_now / (batt_full / 100);
72 if (batt_percent <= 40) // 40
74 if (batt_percent <= 25) // 25
76 if (batt_percent <= 10) // 10
77 fputs("^fg(red)", stdout);
79 fputs("^fg(orange)", stdout);
82 fputs("^fg(yellow)", stdout);
86 if (batt_percent > 70) // 70
87 fputs("^fg(white)", stdout);
89 fputs("^fg(green)", stdout);
92 batt_time = (float)batt_now / (float)batt_current;
93 batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000);
95 if (batt_watts == 0) // fully charged and on AC
96 printf(" BAT0: %d%% _ _ ", batt_percent);
98 printf(" BAT0: %d%% %.1fh %.1fW ", batt_percent, batt_time, batt_watts);
100 //fputs(" Power status ", stdout);