-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbat.c
52 lines (41 loc) · 1.04 KB
/
bat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdbool.h>
#define CLEAR_LINE "\33[2K\r"
double getPowerDraw();
double timeRemaining(void);
int dEqual(double a, double b, double epsilon);
bool isLinux();
int main(void)
{
// First check if is linux
if (!isLinux())
{
printf("This project relies on the linux kernel to function properly."
"\n Your laptop is not using the linux kernel.\n");
}
double draw = getPowerDraw();
double hours = timeRemaining();
double prevDraw = draw;
printf("%.1f W%5.1f hr", draw, hours);
fflush(stdout); // actual printing occurs here
for (;;)
{
sleep(1);
prevDraw = draw;
draw = getPowerDraw();
if (!dEqual(prevDraw, draw, 1.0E-4))
{
printf(CLEAR_LINE);
hours = timeRemaining();
printf("%.1f W%5.1f hr", draw, hours);
fflush(stdout);
}
}
return 0;
}
int dEqual(double a, double b, double epsilon)
{
return fabs(a - b) < epsilon;
}