Moved battery status to fileRead().
[sysstatus.git] / statuses / battery.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "string.h"
4 #include "battery.h"
5
6 #ifndef POWER_BASEDIR
7         #define POWER_BASEDIR "/sys/class/power_supply/"
8 #endif
9
10
11 void status_battery(char *batname)
12 {
13         char batpath[256];
14         int batpathlen;
15
16         char stline[16];
17         ssize_t stlen;
18
19         int batt_now = 0;
20         int batt_full = -1;
21         int batt_percent = -1;
22         int batt_current = 1;
23         int batt_voltage = 0;
24         float batt_time = -1;
25         float batt_watts = -1;
26
27
28         // Prepare path
29         batpathlen = sizeof(POWER_BASEDIR) - 1 + strlen(batname);
30         if (batpathlen + 1 + sizeof("/energy_full") >= sizeof(batpath))
31         {
32                 statusError("status_battery",
33                         "batpath buffer too small",
34                         batname);
35                 return;
36         }
37         strcpy(batpath, POWER_BASEDIR);
38         strcat(batpath, batname);
39
40
41         // Get info
42         strcpy(&batpath[batpathlen], "/energy_now");
43         stlen = fileRead(stline, sizeof(stline), batpath);
44         if (stlen > 0)
45                 batt_now = atoi(stline);
46
47         strcpy(&batpath[batpathlen], "/energy_full");
48         stlen = fileRead(stline, sizeof(stline), batpath);
49         if (stlen > 0)
50                 batt_full = atoi(stline);
51
52         strcpy(&batpath[batpathlen], "/current_now");
53         stlen = fileRead(stline, sizeof(stline), batpath);
54         if (stlen > 0)
55                 batt_current = atoi(stline);
56
57         strcpy(&batpath[batpathlen], "/voltage_now");
58         stlen = fileRead(stline, sizeof(stline), batpath);
59         if (stlen > 0)
60                 batt_voltage = atoi(stline);
61
62
63         // Prettyprint
64         if (batt_full > 0)
65                 batt_percent = batt_now / (batt_full / 100);
66
67         if (batt_percent <= 40) // 40
68         {
69                 if (batt_percent <= 25) // 25
70                 {
71                         if (batt_percent <= 10) // 10
72                                 fputs("^fg(red)", stdout);
73                         else // 11-25%
74                                 fputs("^fg(orange)", stdout);
75                 }
76                 else // 26-40%
77                         fputs("^fg(yellow)", stdout);
78         }
79         else
80         {
81                 if (batt_percent > 70) // 70
82                         fputs("^fg(white)", stdout);
83                 else // 41-70%
84                         fputs("^fg(green)", stdout);
85         }
86
87         batt_time = (float)batt_now / (float)batt_current;
88         batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000);
89
90         if (batt_watts == 0)    // fully charged and not in use
91                 printf(" %s: %d%% _ _ ", batname, batt_percent);
92         else
93                 printf(" %s: %d%% %.1fh %.1fW ",
94                         batname, batt_percent, batt_time, batt_watts);
95 }