-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprodScreen_waitBattery.c
385 lines (313 loc) · 12.1 KB
/
prodScreen_waitBattery.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
//#include <SDL/SDL_image.h>
#include "funkey_prod_screens.h"
/// ---------------- DEFINES ----------------
#define BATTERY_PRESENT_FILE "/sys/class/power_supply/axp20x-battery/present"
#define BATTERY_VOLTAGE_NOW_FILE "/sys/class/power_supply/axp20x-battery/voltage_now"
#define USB_PRESENT_FILE "/sys/class/power_supply/axp20x-usb/present"
#define CHECK_BATTERY_DELAY_MS 100
#define NB_BAT_MESUREMENTS 50
#define Y_PADDING_BAT_RES 2
#define MIN_VALID_VOLTAGE 3100000
#define BAT_TESTS \
X(BAT_CONNECTED, "BAT_CONNECTED") \
X(USB_NOT_CONNECTED, "USB_NOT_CONNECTED") \
X(BAT_VALUES_OK, "BAT_VALUES_OK") \
X(NB_BAT_TESTS, "")
#undef X
#define X(a, b) a,
typedef enum{BAT_TESTS} ENUM_BAT_TESTS;
/// ----------- STATIC VARS --------------
#undef X
#define X(a, b) b,
static const char *bat_tests_strings[] = {BAT_TESTS};
static int bat_measurements[NB_BAT_MESUREMENTS] = {0};
static int idx_bat_measurements = 0;
static int nb_bat_measurements = 0;
static bool false_measurement_found = false;
static bool render = true;
/// -------------- STATIC FUNCTIONS IMPLEMENTATION --------------
static int is_battery_present(){
char buf[10];
FILE *fp;
int res = 0;
/* Read battery file */
char *file = BATTERY_PRESENT_FILE;
if ((fp = fopen(file, "r")) == NULL) {
printf("Error! opening file: %s\n", file);
exit(EXIT_FAILURE);
}
if(!fscanf(fp, "%s", buf)) exit(EXIT_FAILURE);
fclose(fp);
/* Check battery present */
//printf("In %s, res= %s\n", __func__, buf);
if(atoi(buf) == 1){
res = 1;
}
return res;
}
static int get_battery_voltage(){
char buf[10];
FILE *fp;
int res = 0;
/* Read battery voltage file */
char *file = BATTERY_VOLTAGE_NOW_FILE;
if ((fp = fopen(file, "r")) == NULL) {
printf("Error! opening file: %s\n", file);
exit(EXIT_FAILURE);
}
if(!fscanf(fp, "%s", buf)) exit(EXIT_FAILURE);
fclose(fp);
/* Check voltage */
//printf("In %s, res= %s\n", __func__, buf);
return atoi(buf);
}
static int is_usb_present(){
char buf[10];
FILE *fp;
int res = 0;
/* Read usb file */
char *file = USB_PRESENT_FILE;
if ((fp = fopen(file, "r")) == NULL) {
printf("Error! opening file: %s\n", file);
exit(EXIT_FAILURE);
}
if(!fscanf(fp, "%s", buf)) exit(EXIT_FAILURE);
fclose(fp);
/* Check usb present */
//printf("In %s, res= %s\n", __func__, buf);
if(atoi(buf) == 1){
res = 1;
}
return res;
}
static void render_static_text(){
SDL_Surface *text_surface = NULL;
SDL_Rect text_pos;
/* Fill screen white */
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
/* Write Title */
text_surface = TTF_RenderText_Shaded(font_title, prog_title, text_color, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = Y_PADDING_BAT_RES;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Write "Test title*/
/*SDL_Color dark_gray={20,20,20};
text_surface = TTF_RenderText_Shaded(font_title, "BATTERY TESTS:", dark_gray, bg_color);
text_pos.x = X_PADDING;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - (text_surface->h*(NB_BAT_TESTS+1) + Y_PADDING_BAT_RES*NB_BAT_TESTS)/2;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);*/
/* Write "Test title*/
/*SDL_Color red={255,0,0};
text_surface = TTF_RenderText_Shaded(font_title, "INSERT BATTERY", red, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - text_surface->h/2;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);*/
/* Write:
"Press
L=FAIL
*/
SDL_Color red_color={220,20,20};
text_surface = TTF_RenderText_Shaded(font_info, "Press", red_color, bg_color);
text_pos.x = X_PADDING;
text_pos.y = SCREEN_VERTICAL_SIZE - Y_PADDING_BAT_RES - 2*text_surface->h;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
text_surface = TTF_RenderText_Shaded(font_info, "L=FAIL", red_color, bg_color);
text_pos.x = X_PADDING;
text_pos.y = SCREEN_VERTICAL_SIZE - Y_PADDING_BAT_RES - text_surface->h;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Write:
Press
R=OK"
*/
/*SDL_Color green_color={20,220,20};
text_surface = TTF_RenderText_Shaded(font_info, "Press", green_color, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE - text_surface->w - X_PADDING;
text_pos.y = SCREEN_VERTICAL_SIZE - Y_PADDING_BAT_RES - 2*text_surface->h;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
text_surface = TTF_RenderText_Shaded(font_info, "R=OK", green_color, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE - text_surface->w - X_PADDING;
text_pos.y = SCREEN_VERTICAL_SIZE - Y_PADDING_BAT_RES - text_surface->h;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);*/
/// Render screen
render = true;
}
static int wait_event_loop(){
SDL_Event event;
int stop_menu_loop = 0;
int prev_ms = 0;
int res = EXIT_FAILURE;
/// -------- Main loop ---------
while (!stop_menu_loop)
{
/* Vars */
bool all_tests_ok = false;
/// -------- Handle Keyboard Events ---------
while (SDL_PollEvent(&event))
switch(event.type)
{
case SDL_QUIT:
stop_menu_loop = 1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_m:
stop_menu_loop = 1;
res = ERROR_MANUAL_FAIL;
break;
//case SDLK_n:
case SDLK_ESCAPE:
stop_menu_loop = 1;
res = 0;
break;
default:
break;
}
}
/* Sleep for some time before next tests */
if(SDL_GetTicks() - prev_ms > CHECK_BATTERY_DELAY_MS){
/* Render BG text */
render_static_text();
/* Perform tests */
SDL_Surface *text_surface = NULL;
SDL_Rect text_pos;
bool bat_connected = false;
bool usb_connected = true;
int cur_bat_test_idx;
all_tests_ok = true;
for(cur_bat_test_idx = 0; cur_bat_test_idx < NB_BAT_TESTS; cur_bat_test_idx++){
switch(cur_bat_test_idx){
/* Check battery present */
case BAT_CONNECTED:
if(is_battery_present() != 1){
/* bat bool*/
bat_connected = false;
/* Show ERROR message */
SDL_Color red={255,0,0};
text_surface = TTF_RenderText_Shaded(font_title, "CONNECT BATTERY", red, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - Y_PADDING - (text_surface->h*(NB_BAT_TESTS) + Y_PADDING_BAT_RES*(NB_BAT_TESTS-1))/2
+ (text_surface->h+Y_PADDING_BAT_RES)*cur_bat_test_idx;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Force render screen */
render = true;
/* Set tests KO */
all_tests_ok = false;
}
else{
bat_connected = true;
}
break;
/* Check USB present */
case USB_NOT_CONNECTED:
if(is_usb_present() == 1){
/* usb bool*/
usb_connected = true;
/* Show ERROR message */
SDL_Color red={255,0,0};
text_surface = TTF_RenderText_Shaded(font_title, "UNPLUG USB", red, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - Y_PADDING - (text_surface->h*(NB_BAT_TESTS) + Y_PADDING_BAT_RES*(NB_BAT_TESTS-1))/2
+ (text_surface->h+Y_PADDING_BAT_RES)*cur_bat_test_idx;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Force render screen */
render = true;
/* Set tests KO */
all_tests_ok = false;
}
else{
usb_connected = false;
}
break;
/* Check battery voltage */
case BAT_VALUES_OK:
if(bat_connected && !usb_connected){
/* Getting new bat measurement */
bat_measurements[idx_bat_measurements] = get_battery_voltage();
/* Check if voltage value not valid */
if(bat_measurements[idx_bat_measurements] < MIN_VALID_VOLTAGE){
false_measurement_found = true;
}
/* Update idx */
idx_bat_measurements = (idx_bat_measurements+1)%NB_BAT_MESUREMENTS;
nb_bat_measurements++;
/* Check error */
if(false_measurement_found){
/* Show ERROR message */
if(false_measurement_found){
SDL_Color red={255,0,0};
text_surface = TTF_RenderText_Shaded(font_title, "WRONG VOLTAGE", red, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - Y_PADDING - (text_surface->h*(NB_BAT_TESTS) + Y_PADDING_BAT_RES*(NB_BAT_TESTS-1))/2
+ (text_surface->h+Y_PADDING_BAT_RES)*cur_bat_test_idx;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Set tests KO */
all_tests_ok = false;
/* Force render screen */
render = true;
}
}
else if(nb_bat_measurements <NB_BAT_MESUREMENTS){
/* Show measurement message */
SDL_Color dark_gray={40,40,40};
char text_tmp[40];
sprintf(text_tmp, "Check voltage (%d/%d)...", idx_bat_measurements, NB_BAT_MESUREMENTS);
text_surface = TTF_RenderText_Shaded(font_info, text_tmp, dark_gray, bg_color);
text_pos.x = SCREEN_HORIZONTAL_SIZE/2 - text_surface->w/2;
text_pos.y = SCREEN_VERTICAL_SIZE/2 - Y_PADDING - (text_surface->h*(NB_BAT_TESTS) + Y_PADDING_BAT_RES*(NB_BAT_TESTS-1))/2
+ (text_surface->h+Y_PADDING_BAT_RES)*cur_bat_test_idx;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Force render screen */
render = true;
/* Set tests KO */
all_tests_ok = false;
}
}
break;
default:
printf("ERROR in %s, wrong value for cur_bat_test_idx: %d\n",
__func__, cur_bat_test_idx);
break;
}
}
/* Update time*/
prev_ms = SDL_GetTicks();
}
/* Flip screen */
if(render){
SDL_Flip(hw_surface);
render = false;
}
/* Exit success if all tests ok */
if(all_tests_ok){
printf("All battery tests ok");
stop_menu_loop = 1;
res = 0;
break;
}
/* FPS handling */
SDL_Delay(SLEEP_PERIOD_MS);
}
return res;
}
/// -------------- PUBLIC FUNCTIONS IMPLEMENTATION --------------
int launch_prod_screen_waitbattery(int argc, char *argv[]){
///
int res = wait_event_loop();
return res;
}