From cde9d77e2376dfaf1a44c0f5bbca52121a766f48 Mon Sep 17 00:00:00 2001 From: busebusemac Date: Thu, 10 Sep 2020 10:39:45 +0200 Subject: [PATCH] added simple img reader since fbv seems not to work when launched from bash script --- Makefile | 3 +- funkey_prod_screens.c | 67 ++++++++++++++---------- funkey_prod_screens.h | 10 +++- prodScreen_buttonsTest.c | 3 +- prodScreen_buttonsTest.h | 6 +-- prodScreen_displayTest.c | 2 +- prodScreen_displayTest.h | 2 +- prodScreen_failScreen.c | 2 +- prodScreen_failScreen.h | 2 +- prodScreen_ledTest.c | 2 +- prodScreen_ledTest.h | 2 +- prodScreen_magnetTest.c | 16 +++++- prodScreen_magnetTest.h | 2 +- prodScreen_showImage.c | 107 +++++++++++++++++++++++++++++++++++++++ prodScreen_showImage.h | 6 +++ prodScreen_speakerTest.c | 2 +- prodScreen_speakerTest.h | 2 +- prodScreen_validation.c | 2 +- prodScreen_validation.h | 2 +- prodScreen_waitBattery.c | 2 +- prodScreen_waitBattery.h | 2 +- 21 files changed, 191 insertions(+), 53 deletions(-) create mode 100644 prodScreen_showImage.c create mode 100644 prodScreen_showImage.h diff --git a/Makefile b/Makefile index 2756fe2..1b11cb8 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,8 @@ prodScreen_buttonsTest.c \ prodScreen_speakerTest.c \ prodScreen_ledTest.c \ prodScreen_magnetTest.c \ -prodScreen_validation.c +prodScreen_validation.c \ +prodScreen_showImage.c # Output EXEC=funkey_prod_screens diff --git a/funkey_prod_screens.c b/funkey_prod_screens.c index 0cdd6a8..93af9cd 100644 --- a/funkey_prod_screens.c +++ b/funkey_prod_screens.c @@ -16,14 +16,15 @@ SDL_Color text_color = {COLOR_TEXT_R, COLOR_TEXT_G, COLOR_TEXT_B}; /* Static Variables */ static s_prod_test prod_tests[] = { - {"FAIL", launch_prod_screen_fail}, - {"WAIT_BATTERY", launch_prod_screen_waitbattery}, - {"DISPLAY", launch_prod_screen_display}, - {"BUTTONS", launch_prod_screen_buttons}, - {"SPEAKER", launch_prod_screen_speaker}, - {"LED", launch_prod_screen_LED}, - {"MAGNET", launch_prod_screen_magnet}, - {"VALIDATE", launch_prod_screen_validation} + {"FAIL", launch_prod_screen_fail, 0}, + {"WAIT_BATTERY", launch_prod_screen_waitbattery, 0}, + {"DISPLAY", launch_prod_screen_display, 0}, + {"BUTTONS", launch_prod_screen_buttons, 0}, + {"SPEAKER", launch_prod_screen_speaker, 0}, + {"LED", launch_prod_screen_LED, 0}, + {"MAGNET", launch_prod_screen_magnet, 0}, + {"VALIDATE", launch_prod_screen_validation, 0}, + {"SHOW_IMAGE", launch_prod_screen_showImage, 1} }; static int idx_current_prod_test = 0; @@ -87,10 +88,16 @@ void deinit_libraries(){ void usage(char *progname){ int i; - fprintf(stderr, "Usage: %s [prod_test]\n\n", progname); + fprintf(stderr, "Usage: %s [prod_test] [optionnal: arg]\n\n", progname); fprintf(stderr, "\"prod_tests\" in:\n"); for (i = 0; i < sizeof(prod_tests)/sizeof(prod_tests[0]); i++ ){ - fprintf(stderr, " %s\n", prod_tests[i].cmd_line_argument); + if(!prod_tests[i].nb_args_needed){ + fprintf(stderr, " %s\n", prod_tests[i].cmd_line_argument); + } + else{ + fprintf(stderr, " %s [needs %d additional args]\n", + prod_tests[i].cmd_line_argument, prod_tests[i].nb_args_needed); + } } exit(EXIT_FAILURE); } @@ -98,37 +105,41 @@ void usage(char *progname){ int main(int argc, char *argv[]) { - int optind, i; + int i; int res = ERROR_MANUAL_FAIL; - if(argc != 2){ + if(argc < 2){ usage(argv[0]); } - for (optind = 1; optind < argc; optind++) { + char * prod_test_str = argv[1]; + int test_found = 0; - int test_found = 0; - - /* Check argument */ - for (i = 0; i < sizeof(prod_tests)/sizeof(prod_tests[0]); i++ ){ - if(!strcmp(prod_tests[i].cmd_line_argument, argv[optind])){ - test_found = 1; - idx_current_prod_test = i; - break; - } + /* Check argument */ + for (i = 0; i < sizeof(prod_tests)/sizeof(prod_tests[0]); i++ ){ + if(!strcmp(prod_tests[i].cmd_line_argument, prod_test_str)){ + test_found = 1; + idx_current_prod_test = i; + break; } + } - if(!test_found){ - usage(argv[0]); - } - - } + if(test_found && (prod_tests[i].nb_args_needed+2 != argc) ){ + fprintf(stderr, "ERROR: %s needs %d additional args\n", + prod_tests[idx_current_prod_test].cmd_line_argument, + prod_tests[idx_current_prod_test].nb_args_needed); + exit(EXIT_FAILURE); + } + + if(!test_found){ + usage(argv[0]); + } /// Init SDL init_libraries(); /// Launch Program - res = prod_tests[idx_current_prod_test].ptr_function_launch_test(); + res = prod_tests[idx_current_prod_test].ptr_function_launch_test(argc-2, &argv[2]); /// Deinit SDL deinit_libraries(); diff --git a/funkey_prod_screens.h b/funkey_prod_screens.h index befccde..264f160 100644 --- a/funkey_prod_screens.h +++ b/funkey_prod_screens.h @@ -5,7 +5,7 @@ #include #include #include -//#include +#include #include "prodScreen_failScreen.h" #include "prodScreen_waitBattery.h" #include "prodScreen_displayTest.h" @@ -14,6 +14,7 @@ #include "prodScreen_ledTest.h" #include "prodScreen_magnetTest.h" #include "prodScreen_validation.h" +#include "prodScreen_showImage.h" /// Defines @@ -40,10 +41,15 @@ #define FONT_NAME_INFO FONT_NAME_TITLE #define FONT_SIZE_INFO 18 +#define IMG_CONSOLE_LAYOUT FOLDER_RESSOURCES"/funkey_with_buttons.png" +#define IMG_BUTTON_LR_GREEN FOLDER_RESSOURCES"/button_LR_green.png" +#define IMG_BUTTON_NORMAL_GREEN FOLDER_RESSOURCES"/button_round_green.png" + typedef struct { char *cmd_line_argument; - int (*ptr_function_launch_test)(); + int (*ptr_function_launch_test)(int argc, char *argv[]); + int nb_args_needed; } s_prod_test; diff --git a/prodScreen_buttonsTest.c b/prodScreen_buttonsTest.c index c9b44b7..4b62cad 100644 --- a/prodScreen_buttonsTest.c +++ b/prodScreen_buttonsTest.c @@ -1,4 +1,3 @@ -#include #include "funkey_prod_screens.h" @@ -15,7 +14,7 @@ static int keys_pushed[NB_KEYS] = {0}; /// -------------- FUNCTIONS IMPLEMENTATION -------------- -int launch_prod_screen_buttons(){ +int launch_prod_screen_buttons(int argc, char *argv[]){ /* Declare Vars */ SDL_Surface *text_surface = NULL; diff --git a/prodScreen_buttonsTest.h b/prodScreen_buttonsTest.h index 56cf6c5..10293cc 100644 --- a/prodScreen_buttonsTest.h +++ b/prodScreen_buttonsTest.h @@ -1,10 +1,6 @@ #ifndef __PROD_SCREEN_BUTTONS__ #define __PROD_SCREEN_BUTTONS__ -#define IMG_CONSOLE_LAYOUT FOLDER_RESSOURCES"/funkey_with_buttons.png" -#define IMG_BUTTON_LR_GREEN FOLDER_RESSOURCES"/button_LR_green.png" -#define IMG_BUTTON_NORMAL_GREEN FOLDER_RESSOURCES"/button_round_green.png" - -int launch_prod_screen_buttons(); +int launch_prod_screen_buttons(int argc, char *argv[]); #endif //__PROD_SCREEN_BUTTONS__ \ No newline at end of file diff --git a/prodScreen_displayTest.c b/prodScreen_displayTest.c index f9f3cac..a13ef29 100644 --- a/prodScreen_displayTest.c +++ b/prodScreen_displayTest.c @@ -2,7 +2,7 @@ /// -------------- FUNCTIONS IMPLEMENTATION -------------- -int launch_prod_screen_display(){ +int launch_prod_screen_display(int argc, char *argv[]){ SDL_Event event; SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_displayTest.h b/prodScreen_displayTest.h index 54b9c4d..f4fa6f6 100644 --- a/prodScreen_displayTest.h +++ b/prodScreen_displayTest.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_DISPLAY__ #define __PROD_SCREEN_DISPLAY__ -int launch_prod_screen_display(); +int launch_prod_screen_display(int argc, char *argv[]); #endif //__PROD_SCREEN_DISPLAY__ \ No newline at end of file diff --git a/prodScreen_failScreen.c b/prodScreen_failScreen.c index 13dd3b4..9793366 100644 --- a/prodScreen_failScreen.c +++ b/prodScreen_failScreen.c @@ -48,7 +48,7 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_fail(){ +int launch_prod_screen_fail(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_failScreen.h b/prodScreen_failScreen.h index 9458569..bbde13d 100644 --- a/prodScreen_failScreen.h +++ b/prodScreen_failScreen.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_FAIL__ #define __PROD_SCREEN_FAIL__ -int launch_prod_screen_fail(); +int launch_prod_screen_fail(int argc, char *argv[]); #endif //__PROD_SCREEN_FAIL__ \ No newline at end of file diff --git a/prodScreen_ledTest.c b/prodScreen_ledTest.c index 9cd1ce6..13c663d 100644 --- a/prodScreen_ledTest.c +++ b/prodScreen_ledTest.c @@ -47,7 +47,7 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_LED(){ +int launch_prod_screen_LED(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_ledTest.h b/prodScreen_ledTest.h index c85dbc0..cad7990 100644 --- a/prodScreen_ledTest.h +++ b/prodScreen_ledTest.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_LED__ #define __PROD_SCREEN_LED__ -int launch_prod_screen_LED(); +int launch_prod_screen_LED(int argc, char *argv[]); #endif //__PROD_SCREEN_LED__ \ No newline at end of file diff --git a/prodScreen_magnetTest.c b/prodScreen_magnetTest.c index 6f25700..5121239 100644 --- a/prodScreen_magnetTest.c +++ b/prodScreen_magnetTest.c @@ -1,11 +1,20 @@ #include "funkey_prod_screens.h" +#include +/* Static variable */ +static int stop_menu_loop = 0; + +/* Handler for SIGUSR1, caused by closing the console */ +void handle_sigusr1(int sig) +{ + //printf("Caught signal USR1 %d\n", sig); + stop_menu_loop = 1; +} /// -------------- FUNCTIONS IMPLEMENTATION -------------- static int wait_event_loop(){ SDL_Event event; - int stop_menu_loop = 0; int res = 0; /// -------- Main loop --------- @@ -48,10 +57,13 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_magnet(){ +int launch_prod_screen_magnet(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; + /* Init Signals */ + signal(SIGUSR1, handle_sigusr1); + /* Fill screen white */ SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b)); diff --git a/prodScreen_magnetTest.h b/prodScreen_magnetTest.h index 0e0d11c..af776b2 100644 --- a/prodScreen_magnetTest.h +++ b/prodScreen_magnetTest.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_MAGNET__ #define __PROD_SCREEN_MAGNET__ -int launch_prod_screen_magnet(); +int launch_prod_screen_magnet(int argc, char *argv[]); #endif //__PROD_SCREEN_MAGNET__ \ No newline at end of file diff --git a/prodScreen_showImage.c b/prodScreen_showImage.c new file mode 100644 index 0000000..f9eb145 --- /dev/null +++ b/prodScreen_showImage.c @@ -0,0 +1,107 @@ +#include "funkey_prod_screens.h" + + +/// Defines + +/// Static variables + + +/// -------------- FUNCTIONS IMPLEMENTATION -------------- + +/* Nearest neighboor optimized with possible out of screen coordinates (for cropping) */ +void flip_NNOptimized_AllowOutOfScreen(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){ + int w1=src_surface->w; + //int h1=src_surface->h; + int w2=new_w; + int h2=new_h; + int x_ratio = (int)((src_surface->w<<16)/w2); + int y_ratio = (int)((src_surface->h<<16)/h2); + int x2, y2; + int i, j; + + /* Compute padding for centering when out of bounds */ + int y_padding = (SCREEN_VERTICAL_SIZE-new_h)/2; + int x_padding = 0; + if(w2>SCREEN_HORIZONTAL_SIZE){ + x_padding = (w2-SCREEN_HORIZONTAL_SIZE)/2 + 1; + } + int x_padding_ratio = x_padding*w1/w2; + + for (i=0;i=SCREEN_VERTICAL_SIZE){ + continue; + } + + uint16_t* t = (uint16_t*)(dst_surface->pixels+((i+y_padding)* ((w2>SCREEN_HORIZONTAL_SIZE)?SCREEN_HORIZONTAL_SIZE:w2) )*sizeof(uint16_t)); + y2 = ((i*y_ratio)>>16); + uint16_t* p = (uint16_t*)(src_surface->pixels + (y2*w1 + x_padding_ratio) *sizeof(uint16_t)); + int rat = 0; + for (j=0;j=SCREEN_HORIZONTAL_SIZE){ + continue; + } + x2 = (rat>>16); + *t++ = p[x2]; + rat += x_ratio; + } + } +} + + +int launch_prod_screen_showImage(int argc, char *argv[]){ + SDL_Event event; + int res = 0; + int stop_menu_loop = 0; + + /* Load Img */ + char *img_path = argv[0]; + SDL_Surface *image=IMG_Load(img_path); + if(!image) { + printf("ERROR IMG_Load: %s\n", IMG_GetError()); + printf("IMG path is: %s\n", img_path); + exit(1); + } + + + /* Convert img to RGB565 */ + SDL_Surface *image_rgb565 = SDL_CreateRGBSurface(SDL_SWSURFACE, image->w, image->h, 16, 0, 0, 0, 0); + SDL_BlitSurface(image, NULL, image_rgb565, NULL); + SDL_FreeSurface(image); + + /* Scale to fullscreen */ + flip_NNOptimized_AllowOutOfScreen(image_rgb565, hw_surface, hw_surface->w, hw_surface->h); + + /// -------- Main loop --------- + while (!stop_menu_loop) + { + /// -------- 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_ESCAPE: + stop_menu_loop = 1; + res = 0; + break; + default: + break; + } + } + + /* To investigate but with Buildroot, we need this: */ + SDL_Flip(hw_surface); + + /* Sleep for some time */ + SDL_Delay(SLEEP_PERIOD_MS); + } + + return res; +} + diff --git a/prodScreen_showImage.h b/prodScreen_showImage.h new file mode 100644 index 0000000..17f21cd --- /dev/null +++ b/prodScreen_showImage.h @@ -0,0 +1,6 @@ +#ifndef __PROD_SCREEN_SHOW_IMAGE__ +#define __PROD_SCREEN_SHOW_IMAGE__ + +int launch_prod_screen_showImage(int argc, char *argv[]); + +#endif //__PROD_SCREEN_SHOW_IMAGE__ \ No newline at end of file diff --git a/prodScreen_speakerTest.c b/prodScreen_speakerTest.c index f083339..4ef9255 100644 --- a/prodScreen_speakerTest.c +++ b/prodScreen_speakerTest.c @@ -48,7 +48,7 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_speaker(){ +int launch_prod_screen_speaker(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_speakerTest.h b/prodScreen_speakerTest.h index 1ff30bc..52cc626 100644 --- a/prodScreen_speakerTest.h +++ b/prodScreen_speakerTest.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_SPEAKER__ #define __PROD_SCREEN_SPEAKER__ -int launch_prod_screen_speaker(); +int launch_prod_screen_speaker(int argc, char *argv[]); #endif //__PROD_SCREEN_SPEAKER__ \ No newline at end of file diff --git a/prodScreen_validation.c b/prodScreen_validation.c index e6f76ac..459db61 100644 --- a/prodScreen_validation.c +++ b/prodScreen_validation.c @@ -48,7 +48,7 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_validation(){ +int launch_prod_screen_validation(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_validation.h b/prodScreen_validation.h index 8e8fb18..bc443c1 100644 --- a/prodScreen_validation.h +++ b/prodScreen_validation.h @@ -1,6 +1,6 @@ #ifndef __PROD_SCREEN_VALIDATION__ #define __PROD_SCREEN_VALIDATION__ -int launch_prod_screen_validation(); +int launch_prod_screen_validation(int argc, char *argv[]); #endif //__PROD_SCREEN_VALIDATION__ \ No newline at end of file diff --git a/prodScreen_waitBattery.c b/prodScreen_waitBattery.c index 7af7448..1bbc46e 100644 --- a/prodScreen_waitBattery.c +++ b/prodScreen_waitBattery.c @@ -88,7 +88,7 @@ static int wait_event_loop(){ return res; } -int launch_prod_screen_waitbattery(){ +int launch_prod_screen_waitbattery(int argc, char *argv[]){ SDL_Surface *text_surface = NULL; SDL_Rect text_pos; diff --git a/prodScreen_waitBattery.h b/prodScreen_waitBattery.h index efd28cf..4dc1cc5 100644 --- a/prodScreen_waitBattery.h +++ b/prodScreen_waitBattery.h @@ -4,6 +4,6 @@ #define BATTERY_PRESENT_FILE "/sys/class/power_supply/axp20x-battery/present" #define CHECK_BATTERY_DELAY_MS 500 -int launch_prod_screen_waitbattery(); +int launch_prod_screen_waitbattery(int argc, char *argv[]); #endif //__PROD_SCREEN_WAITBATTERY__ \ No newline at end of file