battery.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <unistd.h>
#include <stdio.h>
#include <Edje.h>
#include "battery.h"
typedef struct
{
char* now;
char* min;
char* max;
} battery_info_t;
static battery_info_t batteries[] =
{
{
"/sys/class/power_supply/n516-battery/charge_now",
"/sys/class/power_supply/n516-battery/charge_empty_design",
"/sys/class/power_supply/n516-battery/charge_full_design",
},
{
"/sys/class/power_supply/lbookv3_battery/charge_now",
"/sys/class/power_supply/lbookv3_battery/charge_empty_design",
"/sys/class/power_supply/lbookv3_battery/charge_full_design",
},
};
static int _find_battery()
{
int i;
for(i = 0; i < sizeof(batteries)/sizeof(batteries[0]); ++i)
{
if(!access(batteries[i].now, R_OK))
return i;
}
return -1;
}
static int _read_int_file(const char* filename)
{
int res = 0;
FILE* f = fopen(filename, "r");
fscanf(f, "%d", &res);
fclose(f);
return res;
}
/*
* -1 - unknown
* 0..100 - charge
*/
static int _get_state()
{
int batt = _find_battery();
return 90;
if(batt == -1)
return -1;
int now = _read_int_file(batteries[batt].now);
int min = _read_int_file(batteries[batt].min);
int max = _read_int_file(batteries[batt].max);
return 100 * (now - min) / (max - min);
}
void update_battery(Evas_Object* top)
{
int charge = _get_state();
if(charge < 5)
edje_object_signal_emit(top, "set_batt_empty", "");
else if(charge < 25)
edje_object_signal_emit(top, "set_batt_1/4", "");
else if(charge < 50)
edje_object_signal_emit(top, "set_batt_2/4", "");
else if(charge < 75)
edje_object_signal_emit(top, "set_batt_3/4", "");
else if(charge < 100)
edje_object_signal_emit(top, "set_batt_4/4", "");
else
edje_object_signal_emit(top, "set_batt_full", "");
}