battery: Add divider for voltage_now
[sysstatus.git] / src / status / battery.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 #include "common.h"
7 #include "tools.h"
8
9 #ifndef POWER_BASEDIR
10   #define POWER_BASEDIR "/sys/class/power_supply/"
11 #endif
12
13
14 void status_battery(GlobalData *g, char *batname)
15 {
16   StatusItem s;
17   char text[32] = { 0 };
18
19   char batpath[256];
20   int batpathlen;
21
22   char stline[16];
23   ssize_t stlen;
24
25   int chargeNow = 0;
26   int chargeFull = -1;
27   int chargePercent = -1;
28   int currentNow = 1;
29   int voltageNow = 0;
30   float battTime = -1;
31
32
33   statusitem_init(&s);
34   s.text = text;
35
36   /* Prepare path */
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",
41                 batname);
42     return;
43   }
44   strcpy(batpath, POWER_BASEDIR);
45   strcat(batpath, batname);
46
47
48   /* Is the battery present? */
49   if (access(batpath, F_OK)) {
50     //printf(" ^fg(grey)[%s] ", batname);
51     return;
52   }
53
54
55   /* Get info */
56   strcpy(&batpath[batpathlen], "/charge_now");
57   stlen = fileRead(stline, sizeof(stline), batpath);
58   if (stlen > 0) {
59     chargeNow = atoi(stline);
60   }
61
62   strcpy(&batpath[batpathlen], "/charge_full");
63   stlen = fileRead(stline, sizeof(stline), batpath);
64   if (stlen > 0) {
65     chargeFull = atoi(stline);
66   }
67
68   strcpy(&batpath[batpathlen], "/current_now");
69   stlen = fileRead(stline, sizeof(stline), batpath);
70   if (stlen > 0) {
71     currentNow = atoi(stline);
72   }
73
74   strcpy(&batpath[batpathlen], "/voltage_now");
75   stlen = fileRead(stline, sizeof(stline), batpath);
76   if (stlen > 0) {
77     voltageNow = atoi(stline);
78   }
79
80
81   /* Prettyprint */
82   if (chargeFull > 0) {
83     chargePercent = chargeNow / (chargeFull / 100);
84   }
85
86   if (chargePercent <= 40) {
87     if (chargePercent <= 25) {
88       if (chargePercent <= 10) {
89         s.color = "#FF0000";  // red
90       } else {
91         // 11-25%
92         s.color = "#FFA500";  // orange
93       }
94     } else {
95       // 26-40%
96       s.color = "#FFFF00";  // yellow
97     }
98   } else {
99     if (chargePercent > 70) {
100       s.color = "#FFFFFF";  // white
101     } else {
102       // 41-70%
103       s.color = "#22FF22";  // green
104     }
105   }
106
107   battTime = (float)chargeNow / (float)currentNow;
108
109   if (currentNow == 0) {
110     // fully charged and not in use
111     snprintf(text, sizeof(text), "%s: %d%% _ _",
112               batname, chargePercent);
113   } else {
114     snprintf(text, sizeof(text), "%s: %d%% %.1fh %.1fW",
115               batname, chargePercent, battTime, (float)voltageNow / 1000000.0 * (float)currentNow / 1000000.0);
116   }
117
118   line_append_item(g, &s);
119 }