More cleanup, 'temp' and 'uptime' use fileRead().
[sysstatus.git] / statuses / power.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include "power.h"
6
7 void status_power()
8 {
9         char *stline;
10         size_t stlen;
11         FILE *stfile;
12
13         int batt_now = 0;
14         int batt_full = -1;
15         int batt_percent = -1;
16         int batt_current = 1;
17         int batt_voltage = 0;
18         float batt_time = -1;
19         float batt_watts = -1;
20
21         stfile = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
22         if (stfile != NULL)
23         {
24                 stline = NULL;
25                 stlen = getline(&stline, &stlen, stfile);
26                 fclose(stfile);
27
28                 batt_now = atoi(stline);
29                 free(stline);
30         }
31
32         stfile = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
33         if (stfile != NULL)
34         {
35                 stline = NULL;
36                 stlen = getline(&stline, &stlen, stfile);
37                 fclose(stfile);
38
39                 batt_full = atoi(stline);
40                 free(stline);
41         }
42
43         stfile = fopen("/sys/class/power_supply/BAT0/current_now", "r");
44         if (stfile != NULL)
45         {
46                 stline = NULL;
47                 stlen = getline(&stline, &stlen, stfile);
48                 fclose(stfile);
49
50                 batt_current = atoi(stline);
51                 free(stline);
52         }
53
54         stfile = fopen("/sys/class/power_supply/BAT0/voltage_now", "r");
55         if (stfile != NULL)
56         {
57                 stline = NULL;
58                 stlen = getline(&stline, &stlen, stfile);
59                 fclose(stfile);
60
61                 batt_voltage = atoi(stline);
62                 free(stline);
63         }
64
65     // Hack for chinese battery.
66     //batt_now -= 27220000;
67     //batt_full = 27000000;
68
69         if (batt_full > 0)
70                 batt_percent = batt_now / (batt_full / 100);
71
72         if (batt_percent <= 40) // 40
73         {
74                 if (batt_percent <= 25) // 25
75                 {
76                         if (batt_percent <= 10) // 10
77                                 fputs("^fg(red)", stdout);
78                         else // 11-25%
79                                 fputs("^fg(orange)", stdout);
80                 }
81                 else // 26-40%
82                         fputs("^fg(yellow)", stdout);
83         }
84         else
85         {
86                 if (batt_percent > 70) // 70
87                         fputs("^fg(white)", stdout);
88                 else // 41-70%
89                         fputs("^fg(green)", stdout);
90         }
91
92         batt_time = (float)batt_now / (float)batt_current;
93         batt_watts = ((float)batt_voltage / 1000000) * ((float)batt_current / 10000000);
94
95         if (batt_watts == 0)    // fully charged and on AC
96                 printf(" BAT0: %d%% _ _ ", batt_percent);
97         else
98                 printf(" BAT0: %d%% %.1fh %.1fW ", batt_percent, batt_time, batt_watts);
99
100         //fputs(" Power status ", stdout);
101 }