-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.cpp
72 lines (58 loc) · 1.33 KB
/
battery.cpp
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
#include "battery.h"
#include <qfile.h>
MyBattery::MyBattery()
{
m_acStatus = PowerStatus::Offline;
m_percent = 0;
}
int
MyBattery::batteryPercentRemaining()
{
return m_percent;
}
int
MyBattery::acStatus()
{
return m_acStatus;
}
const MyBattery &
MyBattery::operator = (const MyBattery &bat)
{
m_acStatus = bat.m_acStatus;
m_percent = bat.m_percent;
return *this;
}
bool
MyBattery::updateStatus()
{
QFile my_file("/proc/apm");
if (!my_file.open(IO_ReadOnly))
return false;
QString str;
if(my_file.readLine(str, 100)<1)
return false;
float buf_float[2];
int buf_int[6];
memset(buf_float, 0, sizeof(buf_float));
memset(buf_int, 0, sizeof(buf_int));
/*
printf("STR:%s", (const char*)str);
*/
sscanf(str, "%f %f 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d",
&buf_float[0], &buf_float[1],
&buf_int[0], &buf_int[1], &buf_int[2], &buf_int[3],
&buf_int[4], &buf_int[5] );
/*
printf("%f %f %d %d %d %d %d %d\n",
buf_float[0], buf_float[1],
buf_int[0], buf_int[1], buf_int[2], buf_int[3], buf_int[4],
buf_int[5]);
*/
m_acStatus = buf_int[1];
if (m_acStatus!=0)
m_percent = 100;
else
m_percent = buf_int[4];
my_file.close();
return true;
}