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