added battery voltages tests in assembly tests

This commit is contained in:
Vincent-FK 2021-04-11 16:45:01 +02:00
parent ab4b5c7c10
commit 428290beed
3 changed files with 304 additions and 71 deletions

View File

@ -3,6 +3,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>

View File

@ -6,22 +6,54 @@
#include "funkey_prod_screens.h"
/// -------------- FUNCTIONS IMPLEMENTATION --------------
/// ---------------- 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 */
if ((fp = fopen(BATTERY_PRESENT_FILE, "r")) == NULL) {
printf("Error! opening file: %s\n", BATTERY_PRESENT_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 batery present */
//printf("%s\n", buf);
/* Check battery present */
//printf("In %s, res= %s\n", __func__, buf);
if(atoi(buf) == 1){
res = 1;
}
@ -29,6 +61,116 @@ static int is_battery_present(){
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;
@ -39,6 +181,9 @@ static int wait_event_loop(){
/// -------- Main loop ---------
while (!stop_menu_loop)
{
/* Vars */
bool all_tests_ok = false;
/// -------- Handle Keyboard Events ---------
while (SDL_PollEvent(&event))
switch(event.type)
@ -66,84 +211,173 @@ static int wait_event_loop(){
}
}
/* To investigate but with Buildroot, we need this: */
SDL_Flip(hw_surface);
/* 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();
/* Check batery present */
if(is_battery_present() == 1){
stop_menu_loop = 1;
res = 0;
}
}
/* Sleep for some time */
/* 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[]){
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;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);
/* Write "Screen ok ? */
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 - 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 - 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 - 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 - text_surface->h;
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
SDL_FreeSurface(text_surface);*/
/// Render screen
//SDL_Flip(hw_surface);
///
int res = wait_event_loop();

View File

@ -1,9 +1,6 @@
#ifndef __PROD_SCREEN_WAITBATTERY__
#define __PROD_SCREEN_WAITBATTERY__
#define BATTERY_PRESENT_FILE "/sys/class/power_supply/axp20x-battery/present"
#define CHECK_BATTERY_DELAY_MS 500
int launch_prod_screen_waitbattery(int argc, char *argv[]);
#endif //__PROD_SCREEN_WAITBATTERY__