-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatterymonitor.c
executable file
·144 lines (127 loc) · 4.05 KB
/
batterymonitor.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*****************************************************************
*
*
*****************************************************************/
/*****************************************************************
* 模块名称
*
* GenieVendor电量监控
*
* 模块功能
*
* 1、启动socket监听和按键事件监听
* 2、头文件定义全局日志开关,定义一些通用宏
*
*****************************************************************/
#define _BATTERY_MONITOR_IMPL_
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "batterymonitor.h"
#include "genievendor.h"
#include "logic.h"
#undef LOG_TAG
#define LOG_TAG "GVBatteryMonitor"
/*电量上报间隔*/
#define REPORT_INTERVAL (120) //sec
/*电量节点*/
#define ADC_FD_PATH "/dev/ircut"
/*满电电压*/
#define BATTERY_VOLTAGE_MAX 4.2
#define BATTERY_VOLTAGE_MIN 3.3
pthread_mutex_t batterypthreadlock;
pthread_cond_t batterypthreadruncond;
void* _battery_monitor_thread(void* arg){
FUNCTION_BEGIN
int fd, ret, adc_val;//,adc_voltage;
/* tongfei removed begin */
/*
volatile float battery_voltage=0.0;
volatile int battery_percent=0;
*/
/* tongfei removed end */
/*tongfei code begin */
int battery_percent = 0;
/*tongfei code end */
//usleep(30 * 1000 * 1000);//sleep 30S when booting
while(1){
if(LCTX.is_poweron_keypress_lock){
pthread_mutex_lock(&batterypthreadlock);
pthread_cond_wait(&batterypthreadruncond,&batterypthreadlock);
pthread_mutex_unlock(&batterypthreadlock);
}
fd = open(ADC_FD_PATH, O_RDONLY);
if(fd < 0){
LOGE("BATTERY ERROR: Open ircut failed!");
goto next;
}
ret = read(fd, &adc_val, sizeof(adc_val));
if(ret != sizeof(adc_val)){
LOGE("BATTERY ERROR: Read ircut Failed");
goto next;
}
/* tongfei removed begin */
/*
else{
LOGD("BATTERY ADC Sample value is:%d",adc_val);
}
*/
/* tongfei removed end */
close(fd);
/* tongfei removed begin */
/*
adc_voltage=(3300*adc_val)/4095;
LOGD("BATTERY ADC Sample voltage is:%dmv",adc_voltage);
battery_voltage=(((float)(adc_voltage))/1000.0f);
battery_voltage*=2;
LOGD("Battery voltage is :%.2fv",battery_voltage);
battery_percent = (int)(((battery_voltage-BATTERY_VOLTAGE_MIN)/(BATTERY_VOLTAGE_MAX-BATTERY_VOLTAGE_MIN)) * 100.0f);
*/
/* tongfei removed end */
/* tongfei code begin: */
/*
从上面这些代码看,将adc值转换为电压,操作是:
adc+val * 3300 / 4095 / 1000 * 2
当ADC采集值为2047,则:
2047.5 * 3300 / 4095 / 1000 * 2 = 3.3(V)
当ADC采集2605,则:
2568.7 * 3300 / 4095 / 1000 * 2 = 4.14(V)
所以拿ADC直接跟2047--3.3V时ADC的值 和 2568--4.14V时ADC的值比较,就可以得出百分比了,这里不需要知道实际电压是多少。
5.21 = (2568 - 2047) / 100
如果出现电压不准,先确定是否ADC读不准的问题,有时候ADC值波动很大,需要硬件排查。
*/
battery_percent = (int)((adc_val - 2047.0f) / 5.21f);
/* tongfei code end */
if(battery_percent > 100) {
battery_percent = 100;
}
/* tongfei code begin: */
else if (battery_percent < 1){
battery_percent = 1;
}
/* tongfei code end */
LOGD("Battery adc value: %d, percent: %d%%",adc_val,battery_percent);
logic_handle_battery_percent(&LCTX, battery_percent);
next:
if(fd > 0){
close(fd);
}
usleep(REPORT_INTERVAL * 1000 * 1000);
}
FUNCTION_END
}
/*启动监控线程*/
int battery_monitor_start(pthread_t* thread){
FUNCTION_BEGIN
int ret;
ret = pthread_create(thread, NULL, _battery_monitor_thread, NULL);
if(0 == ret){
LOGD("battery monitor thread created!");
FUNCTION_END
return GV_OK;
} else {
LOGE("battery monitor thread create failed!");
FUNCTION_END
return GV_FAIL;
}
}