From e3c5a5952d68af054fede34bc5b88dc8851ee362 Mon Sep 17 00:00:00 2001 From: Kuristian Date: Wed, 6 Mar 2024 19:10:31 +0000 Subject: [PATCH 1/3] Fixed compiler warnings, mostly removing unused variables and commenting out a few things I need to refactor later --- main/modules/screen_battleship.c | 16 ++++++++++------ main/modules/screen_library.c | 9 ++++----- main/modules/screen_pointclick.c | 26 -------------------------- main/modules/screen_repertoire.c | 2 ++ main/modules/screen_settings.c | 23 ++++++++++++----------- main/modules/screen_shades.c | 3 +-- 6 files changed, 29 insertions(+), 50 deletions(-) diff --git a/main/modules/screen_battleship.c b/main/modules/screen_battleship.c index 025865c..39ee331 100644 --- a/main/modules/screen_battleship.c +++ b/main/modules/screen_battleship.c @@ -33,12 +33,15 @@ extern const uint8_t caronv_png_end[] asm("_binary_caronv_png_end"); extern const uint8_t carond_png_start[] asm("_binary_carond_png_start"); extern const uint8_t carond_png_end[] asm("_binary_carond_png_end"); +// for boards (array of 20) #define water 0 #define boat 1 #define missedshot 2 #define boathit 3 #define boatdestroyed 4 +#define boatnotplaced 255 // used on playership and ennemyship 6 array + #define playing 0 #define victory 1 #define defeat 2 @@ -53,6 +56,7 @@ extern const uint8_t carond_png_end[] asm("_binary_carond_png_end"); #define southeast 4 #define southwest 5 + #define invalid -1 static const char* TAG = "battleship"; @@ -717,8 +721,8 @@ screen_t screen_battleship_entry(QueueHandle_t application_event_queue, QueueHan } for (int i = 0; i < 6; i++) { - playership[i] = -1; - ennemyship[i] = -1; + playership[i] = boatnotplaced; + ennemyship[i] = boatnotplaced; } // set the starter, can be overridden if a player oppenent is the inviter @@ -940,7 +944,7 @@ screen_t screen_battleship_placeships( // set all block to water aside from the placed boats for (int i = 0; i < 20; i++) playerboard[i] = water; for (int i = 0; i < 6; i++) { - if (playership[i] != -1) + if (playership[i] != boatnotplaced) playerboard[playership[i]] = boat; } @@ -964,10 +968,10 @@ screen_t screen_battleship_placeships( if (shipplaced > 0) { // remove the middle of the long ship if (shipplaced == long_whole) { - playership[shipplaced] = -1; + playership[shipplaced] = boatnotplaced; } shipplaced--; - playership[shipplaced] = -1; + playership[shipplaced] = boatnotplaced; displayflag = 1; } else { // exit if (Screen_Confirmation(forfeitprompt, application_event_queue, keyboard_event_queue)) @@ -1181,7 +1185,7 @@ screen_t screen_battleship_battle( ESP_LOGE(TAG, "Failed to add listener"); } else ESP_LOGI(TAG, "listening"); - badge_comms_message_t message; + // badge_comms_message_t message; while (1) { event_t event = {0}; diff --git a/main/modules/screen_library.c b/main/modules/screen_library.c index fbb0f6e..1fa17b4 100644 --- a/main/modules/screen_library.c +++ b/main/modules/screen_library.c @@ -37,7 +37,7 @@ static const char* TAG = "library"; void DisplayLibraryEntry(int cursor); -const char library_items_name[Nb_item_library][60] = { +const char library_items_name[][60] = { "Samuel Morse", "Queen Victoria's Message", "Transatlantic Telegraph Cables", @@ -55,7 +55,7 @@ const char library_items_name[Nb_item_library][60] = { "Entry 13", }; -const uint8_t library_pos[Nb_item_library][2] = { +const uint8_t library_pos[][2] = { {74, 38}, {86, 34}, {98, 37}, @@ -73,7 +73,7 @@ const uint8_t library_pos[Nb_item_library][2] = { {222, 37}, }; -const char library_item_content[Nb_item_library][5][600] = { +const char library_item_content[][5][600] = { // entry 1 {"Samuel F.B. Morse developed an electric telegraph (1832–35) and then invented, with his friend Alfred Vail, the " "Morse Code (1838). The latter is a system for representing letters of the alphabet, numerals, and punctuation " @@ -154,7 +154,7 @@ const char library_item_content[Nb_item_library][5][600] = { {"Entry 13"}, }; -const uint8_t library_item_nbpage[Nb_item_library] = {0, 1, 1, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0}; +const uint8_t library_item_nbpage[] = {0, 1, 1, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0}; pax_vec1_t DrawLibraryContent(int _yoffset, const char* _message) { pax_buf_t* gfx = bsp_get_gfx_buffer(); @@ -270,7 +270,6 @@ pax_vec1_t DrawLibraryContent(int _yoffset, const char* _message) { } void Display_library_content(QueueHandle_t keyboard_event_queue, int cursor, int page) { - pax_buf_t* gfx = bsp_get_gfx_buffer(); AddSWtoBufferLR("", ""); if (page > 0) { AddOneTextSWtoBuffer(SWITCH_1, "Previous"); diff --git a/main/modules/screen_pointclick.c b/main/modules/screen_pointclick.c index eb66922..4fc62b5 100644 --- a/main/modules/screen_pointclick.c +++ b/main/modules/screen_pointclick.c @@ -1015,7 +1015,6 @@ screen_t screen_library_closeup( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, true, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "library_closeup"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1041,7 +1040,6 @@ screen_t screen_library_closeup( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1054,7 +1052,6 @@ screen_t screen_library_closeup_d1( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, true, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "library_closeup_d1"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1080,7 +1077,6 @@ screen_t screen_library_closeup_d1( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1093,7 +1089,6 @@ screen_t screen_lighthouse1_n_gc_d1( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "lighthouse1_n_gc_d1"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1116,7 +1111,6 @@ screen_t screen_lighthouse1_n_gc_d1( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1130,7 +1124,6 @@ screen_t screen_lighthouse2_n_go( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "lighthouse2_n_go"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1153,7 +1146,6 @@ screen_t screen_lighthouse2_n_go( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1166,7 +1158,6 @@ screen_t screen_messageboard_closeup( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, true, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "messageboard_closeup"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1192,7 +1183,6 @@ screen_t screen_messageboard_closeup( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1205,7 +1195,6 @@ screen_t screen_road1_e_jorge( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, true, false); - int displayflag = 1; ESP_LOGE(TAG, "road1_e_jorge"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1233,7 +1222,6 @@ screen_t screen_road1_e_jorge( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1246,7 +1234,6 @@ screen_t screen_town1_n_d1( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "town1_n_d1"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1270,7 +1257,6 @@ screen_t screen_town1_n_d1( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1283,7 +1269,6 @@ screen_t screen_town1_e_d1( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "town1_e_d1"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1307,7 +1292,6 @@ screen_t screen_town1_e_d1( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1320,7 +1304,6 @@ screen_t screen_town1_s_d1_boat( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "town1_s_d1_boat"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1344,7 +1327,6 @@ screen_t screen_town1_s_d1_boat( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1357,7 +1339,6 @@ screen_t screen_town1_s_d1_messageboard( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "town1_s_d1_messageboard"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1379,7 +1360,6 @@ screen_t screen_town1_s_d1_messageboard( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1392,7 +1372,6 @@ screen_t screen_dune2_n_d( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, true, false, false); - int displayflag = 1; ESP_LOGE(TAG, "dune2_n_d"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1415,7 +1394,6 @@ screen_t screen_dune2_n_d( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -1428,7 +1406,6 @@ screen_t screen_shop_closeup( ) { InitKeyboard(keyboard_event_queue); configure_keyboard_presses(keyboard_event_queue, false, false, false, true, false); - int displayflag = 1; ESP_LOGE(TAG, "shop_closeup"); pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); @@ -1455,7 +1432,6 @@ screen_t screen_shop_closeup( case SWITCH_5: break; default: break; } - displayflag = 1; break; default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); } @@ -2144,7 +2120,6 @@ screen_t screen_pointclick_town1( int displayflag = 1; int nbdirection = 4; int move_forward = 0; - int move_back = 0; int button2 = 0; int button4 = 0; ESP_LOGE(TAG, "town1"); @@ -2215,7 +2190,6 @@ screen_t screen_pointclick_town1( displayflag = 0; } move_forward = 0; - move_back = 0; button2 = 0; button4 = 0; event_t event = {0}; diff --git a/main/modules/screen_repertoire.c b/main/modules/screen_repertoire.c index f5b8265..fdd93d1 100644 --- a/main/modules/screen_repertoire.c +++ b/main/modules/screen_repertoire.c @@ -48,6 +48,8 @@ uint16_t GetNBrepertoryID(void) { bool res = nvs_get_u16_wrapped("Repertoire", "IDcount", &value); if (log) ESP_LOGE(TAG, "Get NBrepertoryID: %d", value); + if (res != ESP_OK) + ESP_LOGE(TAG, "GetNBrepertoryID failed"); return value; } diff --git a/main/modules/screen_settings.c b/main/modules/screen_settings.c index 6575d07..a6fc881 100644 --- a/main/modules/screen_settings.c +++ b/main/modules/screen_settings.c @@ -153,6 +153,7 @@ typedef enum _menu_wifi_action { ACTION_PHASE2_CHAP, } menu_wifi_action_t; +/* static bool edit_network_type(wifi_auth_mode_t* network_type) { pax_buf_t* gfx = bsp_get_gfx_buffer(); @@ -194,7 +195,7 @@ static bool edit_network_type(wifi_auth_mode_t* network_type) { // menu_navigate_previous(menu); // menu_navigate_next(menu); - /*if (action != ACTION_NONE) { + if (action != ACTION_NONE) { if (action == ACTION_BACK) { pick = -1; break; @@ -206,7 +207,7 @@ static bool edit_network_type(wifi_auth_mode_t* network_type) { action = ACTION_NONE; render_wifi_help(pax_buffer); } -}*/ +} pax_background(gfx, WHITE); menu_render(gfx, menu, 0, 0, 296, 128); @@ -259,7 +260,7 @@ static bool edit_network_type(wifi_auth_mode_t* network_type) { bsp_display_flush(); return false; -} +}*/ static void draw_wifi_defaults() { const pax_font_t* font = pax_font_saira_regular; @@ -378,14 +379,14 @@ static void edit_wifi(QueueHandle_t application_event_queue, QueueHandle_t keybo } } -static void draw() { - const pax_font_t* font = pax_font_saira_regular; - pax_buf_t* gfx = bsp_get_gfx_buffer(); - pax_background(gfx, WHITE); - pax_draw_text(gfx, RED, font, 18, 5, 5, "Settings & OTA update"); - pax_insert_png_buf(gfx, settings_png_start, settings_png_end - settings_png_start, 0, gfx->width - 24, 0); - bsp_display_flush(); -} +// static void draw() { +// const pax_font_t* font = pax_font_saira_regular; +// pax_buf_t* gfx = bsp_get_gfx_buffer(); +// pax_background(gfx, WHITE); +// pax_draw_text(gfx, RED, font, 18, 5, 5, "Settings & OTA update"); +// pax_insert_png_buf(gfx, settings_png_start, settings_png_end - settings_png_start, 0, gfx->width - 24, 0); +// bsp_display_flush(); +// } // screen_t screen_settings_entry(QueueHandle_t application_event_queue, QueueHandle_t keyboard_event_queue) { // if (log) diff --git a/main/modules/screen_shades.c b/main/modules/screen_shades.c index 219ed17..f883759 100644 --- a/main/modules/screen_shades.c +++ b/main/modules/screen_shades.c @@ -32,8 +32,7 @@ screen_t screen_shades_entry(QueueHandle_t application_event_queue, QueueHandle_ }; xQueueSend(keyboard_event_queue, &kbsettings, portMAX_DELAY); - const pax_font_t* font = pax_font_saira_regular; - pax_buf_t* gfx = bsp_get_gfx_buffer(); + pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_background(gfx, WHITE); pax_insert_png_buf(gfx, shades_png_start, shades_png_end - shades_png_start, 0, 0, 0); bsp_display_flush(); From d1e931e174ff20e13fb929be219c0c7a72abaa0a Mon Sep 17 00:00:00 2001 From: Kuristian Date: Thu, 7 Mar 2024 23:22:30 +0000 Subject: [PATCH 2/3] Refactored the many text handling function into a smaller (still need to add cursor feature). Also added a break; in a switch statmement and a default value for a NVS get since flagged by the compiler as warnings --- main/application.c | 309 ++++++-------------------------- main/application.h | 25 +-- main/modules/screen_billboard.c | 5 +- main/modules/screen_hangman.c | 2 +- main/modules/screen_library.c | 117 +----------- main/modules/screen_test.c | 12 +- 6 files changed, 70 insertions(+), 400 deletions(-) diff --git a/main/application.c b/main/application.c index 3fe697b..27bf50c 100644 --- a/main/application.c +++ b/main/application.c @@ -353,179 +353,6 @@ int WaitingforOpponent(const char* _prompt, QueueHandle_t application_event_queu bsp_set_addressable_led(LED_YELLOW); return 1; } -// Parse _message[] into an array of _nbwords -// and makes them into up to _maxnblines which are _maxlinelength pixel long -// can be centered if the _centered flag is high -void DisplayWallofTextWords( - int _fontsize, - int _maxlinelength, - int _maxnblines, - int _nbwords, - int _xoffset, - int _yoffset, - char _message[200], - int _centered -) { - // set screen font and buffer - const pax_font_t* font = pax_font_sky; - pax_buf_t* gfx = bsp_get_gfx_buffer(); - - // to verify text length on buffer - pax_vec1_t dims = { - .x = 999, - .y = 999, - }; - - // char message[128] = "The quick brown fox jumps over the lazy dog, The quick brie da"; // message to parse - char linetodisplay[128] = ""; - char Words[50][32]; // Array of parsed words - - // counters - int nblines = 0; - int j = 0; - int wordcount = 0; - - pax_background(gfx, WHITE); - - // goes through each character until the end of the string (unless nblines >= maxnblines) - for (int i = 0; i <= (strlen(_message)); i++) { - // If space or end of string found, process word/lines - if (_message[i] == ' ' || _message[i] == '\0') { - Words[wordcount][j] = '\0'; // end of string terminate the word - j = 0; - // pax_draw_text(gfx, BLACK, font, fontsize, xoffset, wordcount * fontsize, Words[wordcount]);//use to - // display words - strcat(linetodisplay, Words[wordcount]); // add word to linetodisplay - strcat(linetodisplay, " "); // and a space that was not parsed - - // if longer than maxlinelength, go to the next line - dims = pax_text_size(font, _fontsize, linetodisplay); - if ((int)dims.x > _maxlinelength) { - linetodisplay[strlen(linetodisplay) - (strlen(Words[wordcount]) + 2)] = - '\0'; // remove the last word and 2 spaces - - // center the text - if (_centered) { - dims = pax_text_size(font, _fontsize, linetodisplay); - _xoffset = gfx->height / 2 - (int)(dims.x / 2); - } - - pax_draw_text( - gfx, - BLACK, - font, - _fontsize, - _xoffset, - _yoffset + nblines * _fontsize, - linetodisplay - ); // displays - // the line - strcpy( - linetodisplay, - Words[wordcount] - ); // Add the latest word that was parsed and removed to the next line - nblines++; - if (nblines >= _maxnblines) { - break; - } - } - - // If it is the last word of the string - if (_message[i] == '\0') { - pax_draw_text(gfx, BLACK, font, _fontsize, _xoffset, _yoffset + nblines * _fontsize, linetodisplay); - } - wordcount++; // Move to the next word - - } else { - Words[wordcount][j] = _message[i]; // Store the character into newString - j++; // Move to the next character within the word - } - } - - bsp_display_flush(); -} - -// Parse _message[] into lines -// and makes them into up to _maxnblines which are _maxlinelength pixel long -// can be centered if the _centered flag is high -void DisplayWallofText( - int _fontsize, int _maxlinelength, int _maxnblines, int _xoffset, int _yoffset, char _message[500], int _centered -) { - // set screen font and buffer - const pax_font_t* font = pax_font_sky; - pax_buf_t* gfx = bsp_get_gfx_buffer(); - - // to verify text length on buffer - pax_vec1_t dims = { - .x = 999, - .y = 999, - }; - - // char message[128] = "The quick brown fox jumps over the lazy dog, The quick brie da"; // message to parse - char linetodisplay[128] = ""; - char Words[64]; // Array of parsed words - - // counters - int nblines = 0; - int j = 0; - int wordcount = 0; - - // pax_background(gfx, WHITE); - - // goes through each character until the end of the string (unless nblines >= maxnblines) - for (int i = 0; i <= (strlen(_message)); i++) { - // If space or end of string found, process word/lines - if (_message[i] == ' ' || _message[i] == '\0') { - Words[j] = '\0'; // end of string terminate the word - j = 0; - // pax_draw_text(gfx, BLACK, font, fontsize, xoffset, wordcount * fontsize, Words[wordcount]);//use to - // display words - strcat(linetodisplay, Words); // add word to linetodisplay - strcat(linetodisplay, " "); // and a space that was not parsed - - // if longer than maxlinelength, go to the next line - dims = pax_text_size(font, _fontsize, linetodisplay); - if ((int)dims.x > _maxlinelength) { - linetodisplay[strlen(linetodisplay) - (strlen(Words) + 2)] = '\0'; // remove the last word and 2 spaces - - // center the text - if (_centered) { - dims = pax_text_size(font, _fontsize, linetodisplay); - _xoffset = gfx->height / 2 - (int)(dims.x / 2); - } - - pax_draw_text( - gfx, - BLACK, - font, - _fontsize, - _xoffset, - _yoffset + nblines * _fontsize, - linetodisplay - ); // displays - // the line - strcpy(linetodisplay, - Words); // Add the latest word that was parsed and removed to the next line - nblines++; - if (nblines >= _maxnblines) { - break; - } - } - - // If it is the last word of the string - if (_message[i] == '\0') { - pax_draw_text(gfx, BLACK, font, _fontsize, _xoffset, _yoffset + nblines * _fontsize, linetodisplay); - } - wordcount++; // Move to the next word - - } else { - Words[j] = _message[i]; // Store the character into newString - j++; // Move to the next character within the word - } - } - - // bsp_display_flush(); -} pax_vec1_t WallofText(int _yoffset, const char* _message, int _centered, int _cursor) { pax_buf_t* gfx = bsp_get_gfx_buffer(); @@ -637,7 +464,29 @@ pax_vec1_t WallofText(int _yoffset, const char* _message, int _centered, int _cu return cursorloc; } -pax_vec1_t WallofTextnb_line(int _yoffset, const char* _message, int _centered, int _cursor, int _maxlinelength) { +int countwords(const char* text) { + int wordcount = 0; + for (int i = 0; i <= (strlen(text)); i++) { + if (text[i] == ' ' || text[i] == '\0') { + wordcount++; + } + } + return wordcount; +} + +void getword(const char* text, char* word, int word_nb) { + int wordcount = 0; + strncpy(word, "\0", 1); + for (int i = 0; i <= (strlen(text)); i++) { + if (text[i] == ' ' || text[i] == '\0') { + wordcount++; + } else if (word_nb == wordcount) { + strncat(word, &text[i], 1); + } + } +} + +void drawParagraph(int x_offset, int y_offset, const char* text, bool centered) { pax_buf_t* gfx = bsp_get_gfx_buffer(); pax_vec1_t dims = { .x = 999, @@ -648,13 +497,13 @@ pax_vec1_t WallofTextnb_line(int _yoffset, const char* _message, int _centered, .y = 0, }; - ESP_LOGE(TAG, "Unhandled event type %s", _message); - ESP_LOGE(TAG, "Unhandled event type %d", strlen(_message)); - // char message[128] = "The quick brown fox jumps over the lazy dog, The quick brie da"; // message to parse + int _cursor = 1; + int maxnblines = 12; + int word_nb = countwords(text); + char linetodisplay[128] = ""; - char Words[64]; // Parsed Word - int _xoffset = 6; - // int _maxlinelength = gfx->height - _xoffset * 2; + char word[50] = ""; + int maxlinelength = gfx->height - x_offset * 2; // counters int nblines = 0; @@ -663,88 +512,40 @@ pax_vec1_t WallofTextnb_line(int _yoffset, const char* _message, int _centered, int cursorfound = 0; if (_cursor == 0) { - cursorloc.x = _xoffset; - cursorloc.y = _yoffset; + cursorloc.x = x_offset; + cursorloc.y = y_offset; cursorfound = 1; } - // pax_background(gfx, WHITE); - - // goes through each character until the end of the string (unless nblines >= maxnblines) - for (int i = 0; i <= (strlen(_message)); i++) { - // If space or end of string found, process word/lines - if (_message[i] == ' ' || _message[i] == '\0') { - Words[j] = '\0'; // end of string terminate the word - j = 0; - strcat(linetodisplay, Words); // add word to linetodisplay - strcat(linetodisplay, " "); // and a space that was not parsed - - // if longer than maxlinelenght, go to the next line - dims = pax_text_size(font1, fontsizeS, linetodisplay); - if ((int)dims.x > _maxlinelength || _message[i] == '\0') { - if (_message[i] != '\0') - linetodisplay[strlen(linetodisplay) - (strlen(Words) + 2)] = - '\0'; // remove the last word and 2 spaces - - // center the text - if (_centered) { - dims = pax_text_size(font1, fontsizeS, linetodisplay); - _xoffset = gfx->height / 2 - (int)(dims.x / 2); - } - - ESP_LOGE(TAG, "line to display length: %d", strlen(linetodisplay)); - - pax_draw_text( - gfx, - BLACK, - font1, - fontsizeS, - _xoffset, - _yoffset + nblines * fontsizeS, - linetodisplay - ); // displays the line - - // calculate the cursor position - - if ((_cursor < strlen(linetodisplay)) && !cursorfound) { - ESP_LOGE(TAG, "cursor on: %c", linetodisplay[_cursor]); - linetodisplay[_cursor] = '\0'; - cursorloc = pax_text_size(font1, fontsizeS, linetodisplay); - cursorloc.x = cursorloc.x + _xoffset; - cursorloc.y = _yoffset + nblines * fontsizeS; - cursorfound = 1; - } else { - _cursor = _cursor - strlen(linetodisplay); - if (!_cursor) { - cursorloc.x = _xoffset; - cursorloc.y = _yoffset + (nblines + 1) * fontsizeS; - cursorfound = 1; - } - } - ESP_LOGE(TAG, "cursor x: %f", cursorloc.x); - ESP_LOGE(TAG, "cursor y: %f", cursorloc.y); - + for (int i = 0; i <= word_nb; i++) { + getword(text, word, i); + strncat(linetodisplay, word, strlen(word)); // add word to linetodisplay + strcat(linetodisplay, " "); // and a space that was not parsed + + // if longer than maxlinelenght, make new line + dims = pax_text_size(font1, fontsizeS, linetodisplay); + if ((int)dims.x > maxlinelength || i >= (word_nb)) { + if ((int)dims.x > maxlinelength) { + linetodisplay[strlen(linetodisplay) - strlen(word) - 2] = '\0'; // remove the last word and 2 spaces + i--; // rewind 1 word back + } - strcpy(linetodisplay, Words); // Add the latest word that was parsed to the next line - strcat(linetodisplay, " "); - nblines++; - if (nblines >= 8) { - break; - } + // center the text + if (centered) { + dims = pax_text_size(font1, fontsizeS, linetodisplay); + x_offset = gfx->height / 2 - (int)(dims.x / 2); } - // If it is the last word of the string - // if (_message[i] == '\0') { - // pax_draw_text(gfx, BLACK, font1, fontsizeS, _xoffset, _yoffset + nblines * fontsizeS, linetodisplay); - // } - wordcount++; // Move to the next word + // displays the line + pax_draw_text(gfx, BLACK, font1, fontsizeS, x_offset, y_offset + nblines * fontsizeS, linetodisplay); - } else { - Words[j] = _message[i]; // Store the character into newString - j++; // Move to the next character within the word + strncpy(linetodisplay, "\0", 1); + nblines++; + if (nblines >= maxnblines || i >= (word_nb - 1)) { + return; + } } } - return cursorloc; } void AddSwitchesBoxtoBuffer(int _switch) // in black @@ -1090,7 +891,7 @@ esp_err_t nvs_get_u8_blob_wrapped(const char* namespace, const char* key, uint8_ } res = nvs_get_blob(handle, key, value, &length); if (res != ESP_OK) { - value[0] = NULL; + value = NULL; return res; } nvs_close(handle); diff --git a/main/application.h b/main/application.h index 19d0173..25552bb 100644 --- a/main/application.h +++ b/main/application.h @@ -50,28 +50,11 @@ void AddSWtoBufferLR(const char* SW1str, const char* SW5str); void AddBlocktoBuffer(int _x, int _y); -// Parse _message[] into an array of _nbwords -// and makes them into up to _maxnblines which are _maxlinelength pixel long -// can be centered if the _centered flag is high -void DisplayWallofTextWords( - int _fontsize, - int _maxlinelength, - int _maxnblines, - int _nbwords, - int _xoffset, - int _yoffset, - char _message[200], - int _centered -); -pax_vec1_t WallofText(int _yoffset, const char* _message, int _centered, int _cursor); -pax_vec1_t WallofTextnb_line(int _yoffset, const char* _message, int _centered, int _cursor, int _maxlinelength); -// Parse _message[] into lines -// and makes them into up to _maxnblines which are _maxlinelength pixel long -// can be centered if the _centered flag is high +int countwords(const char* text); +void getword(const char* text, char* word, int word_nb); +void drawParagraph(int x_offset, int y_offset, const char* text, bool centered); -void DisplayWallofText( - int _fontsize, int _maxlinelenght, int _maxnblines, int _xoffset, int _yoffset, char _message[500], int _centered -); +pax_vec1_t WallofText(int _yoffset, const char* _message, int _centered, int _cursor); void Justify_right_text( pax_buf_t* buf, pax_col_t color, const pax_font_t* font, float font_size, float x, float y, const char* text diff --git a/main/modules/screen_billboard.c b/main/modules/screen_billboard.c index ec46d9b..71db003 100644 --- a/main/modules/screen_billboard.c +++ b/main/modules/screen_billboard.c @@ -177,7 +177,6 @@ screen_t screen_billboard_entry(QueueHandle_t application_event_queue, QueueHand // ESP_LOGI(TAG, "unsubscription result: %s", esp_err_to_name(err)); return screen_home; break; - case SWITCH_2: break; case SWITCH_3: break; case SWITCH_4: break; @@ -206,6 +205,7 @@ screen_t screen_billboard_entry(QueueHandle_t application_event_queue, QueueHand break; default: break; } + break; case event_communication: switch (event.args_communication.type) { case MESSAGE_TYPE_CHAT: @@ -220,8 +220,7 @@ screen_t screen_billboard_entry(QueueHandle_t application_event_queue, QueueHand default: break; } break; - break; - default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); + default: ESP_LOGE(TAG, "Unhandled event type %u", event.type); break; } } } diff --git a/main/modules/screen_hangman.c b/main/modules/screen_hangman.c index 738d132..9146ce9 100644 --- a/main/modules/screen_hangman.c +++ b/main/modules/screen_hangman.c @@ -4966,7 +4966,7 @@ screen_t screen_hangman_definition( pax_background(gfx, WHITE); Addborder2toBuffer(); pax_draw_text(gfx, BLACK, font1, fontsizeS * 2, 20, 20, victorian_words[_chosenwordID]); - DisplayWallofText(fontsizeS, 270, 6, 10, 55, victorian_definitions[_chosenwordID], 1); + drawParagraph(8, 55, victorian_definitions[_chosenwordID], false); bsp_display_flush(); InitKeyboard(keyboard_event_queue); diff --git a/main/modules/screen_library.c b/main/modules/screen_library.c index 1fa17b4..d2fc367 100644 --- a/main/modules/screen_library.c +++ b/main/modules/screen_library.c @@ -156,119 +156,6 @@ const char library_item_content[][5][600] = { const uint8_t library_item_nbpage[] = {0, 1, 1, 3, 2, 2, 0, 0, 0, 0, 0, 0, 0}; -pax_vec1_t DrawLibraryContent(int _yoffset, const char* _message) { - pax_buf_t* gfx = bsp_get_gfx_buffer(); - pax_vec1_t dims = { - .x = 999, - .y = 999, - }; - pax_vec1_t cursorloc = { - .x = 0, - .y = 0, - }; - - int _centered = 1; - int _cursor = 1; - int maxnblines = 12; - - ESP_LOGE(TAG, "Unhandled event type %s", _message); - ESP_LOGE(TAG, "Unhandled event type %d", strlen(_message)); - char linetodisplay[128] = ""; - char Words[64]; // Parsed Word - int _xoffset = 6; - int _maxlinelength = gfx->height - _xoffset * 2; - - // counters - int nblines = 0; - int j = 0; - int wordcount = 0; - int cursorfound = 0; - - if (_cursor == 0) { - cursorloc.x = _xoffset; - cursorloc.y = _yoffset; - cursorfound = 1; - } - - // pax_background(gfx, WHITE); - - // goes through each character until the end of the string (unless nblines >= maxnblines) - for (int i = 0; i <= (strlen(_message)); i++) { - // If space or end of string found, process word/lines - if (_message[i] == ' ' || _message[i] == '\0') { - Words[j] = '\0'; // end of string terminate the word - j = 0; - strcat(linetodisplay, Words); // add word to linetodisplay - strcat(linetodisplay, " "); // and a space that was not parsed - - // if longer than maxlinelenght, go to the next line - dims = pax_text_size(font1, fontsizeS, linetodisplay); - if ((int)dims.x > _maxlinelength || _message[i] == '\0') { - if (_message[i] != '\0') - linetodisplay[strlen(linetodisplay) - (strlen(Words) + 2)] = - '\0'; // remove the last word and 2 spaces - - // center the text - if (_centered) { - dims = pax_text_size(font1, fontsizeS, linetodisplay); - _xoffset = gfx->height / 2 - (int)(dims.x / 2); - } - - ESP_LOGE(TAG, "line to display length: %d", strlen(linetodisplay)); - - pax_draw_text( - gfx, - BLACK, - font1, - fontsizeS, - _xoffset, - _yoffset + nblines * fontsizeS, - linetodisplay - ); // displays the line - - // calculate the cursor position - - if ((_cursor < strlen(linetodisplay)) && !cursorfound) { - ESP_LOGE(TAG, "cursor on: %c", linetodisplay[_cursor]); - linetodisplay[_cursor] = '\0'; - cursorloc = pax_text_size(font1, fontsizeS, linetodisplay); - cursorloc.x = cursorloc.x + _xoffset; - cursorloc.y = _yoffset + nblines * fontsizeS; - cursorfound = 1; - } else { - _cursor = _cursor - strlen(linetodisplay); - if (!_cursor) { - cursorloc.x = _xoffset; - cursorloc.y = _yoffset + (nblines + 1) * fontsizeS; - cursorfound = 1; - } - } - ESP_LOGE(TAG, "cursor x: %f", cursorloc.x); - ESP_LOGE(TAG, "cursor y: %f", cursorloc.y); - - - strcpy(linetodisplay, Words); // Add the latest word that was parsed to the next line - strcat(linetodisplay, " "); - nblines++; - if (nblines >= maxnblines) { - break; - } - } - - // If it is the last word of the string - // if (_message[i] == '\0') { - // pax_draw_text(gfx, BLACK, font1, fontsizeS, _xoffset, _yoffset + nblines * fontsizeS, linetodisplay); - // } - wordcount++; // Move to the next word - - } else { - Words[j] = _message[i]; // Store the character into newString - j++; // Move to the next character within the word - } - } - return cursorloc; -} - void Display_library_content(QueueHandle_t keyboard_event_queue, int cursor, int page) { AddSWtoBufferLR("", ""); if (page > 0) { @@ -282,7 +169,7 @@ void Display_library_content(QueueHandle_t keyboard_event_queue, int cursor, int } else { AddOneTextSWtoBuffer(SWITCH_5, "Continue"); } - DrawLibraryContent(Y_offset_library, library_item_content[cursor][page]); + drawParagraph(6, Y_offset_library, library_item_content[cursor][page], true); bsp_display_flush(); } @@ -368,7 +255,7 @@ void DisplayLibraryEntry(int cursor) { DrawArrowHorizontal(SWITCH_3); AddOneTextSWtoBuffer(SWITCH_5, "Select"); // pax_center_text(gfx, BLACK, font1, fontsizeS, gfx->height / 2, 87, library_items_name[cursor]); - WallofTextnb_line(87, library_items_name[cursor], 1, 10, 140); + drawParagraph(78, 87, library_items_name[cursor], true); // pax_insert_png_buf( // gfx, // diamondb_png_start, diff --git a/main/modules/screen_test.c b/main/modules/screen_test.c index 53b5b12..245d582 100644 --- a/main/modules/screen_test.c +++ b/main/modules/screen_test.c @@ -62,22 +62,22 @@ void Display_credits_entry(int cursor) { break; case production_sponsor: Addborder2toBuffer(); - WallofText( + drawParagraph( + 6, 20, "Thanks to espressif for providing us with the glorious ESP32C6, powering this device, and to Allnet " "for helping us with production!", - 1, - 10 + true ); break; case thankyou: Addborder2toBuffer(); - WallofText( + drawParagraph( + 6, 20, "Thank you to the Hacker Hotel team for making this event happen, and thank you for attending and " "playing~ \n\n\n\n With love, badge.team", - 1, - 10 + true ); break; From e152046af528145dae477babe2438f85ffd5cc7d Mon Sep 17 00:00:00 2001 From: Kuristian Date: Sat, 9 Mar 2024 21:38:59 +0000 Subject: [PATCH 3/3] changed strcpy and strcat to strncpy and strncat --- main/application.c | 4 ++-- main/modules/screen_billboard.c | 13 ++++++----- main/modules/screen_repertoire.c | 39 ++++++++++++++++++++------------ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/main/application.c b/main/application.c index 27bf50c..1199c33 100644 --- a/main/application.c +++ b/main/application.c @@ -476,7 +476,7 @@ int countwords(const char* text) { void getword(const char* text, char* word, int word_nb) { int wordcount = 0; - strncpy(word, "\0", 1); + strcpy(word, ""); for (int i = 0; i <= (strlen(text)); i++) { if (text[i] == ' ' || text[i] == '\0') { wordcount++; @@ -539,7 +539,7 @@ void drawParagraph(int x_offset, int y_offset, const char* text, bool centered) // displays the line pax_draw_text(gfx, BLACK, font1, fontsizeS, x_offset, y_offset + nblines * fontsizeS, linetodisplay); - strncpy(linetodisplay, "\0", 1); + strcpy(linetodisplay, ""); nblines++; if (nblines >= maxnblines || i >= (word_nb - 1)) { return; diff --git a/main/modules/screen_billboard.c b/main/modules/screen_billboard.c index 71db003..0652254 100644 --- a/main/modules/screen_billboard.c +++ b/main/modules/screen_billboard.c @@ -70,8 +70,8 @@ void receive_str(void) { void send_str(char _nickname[nicknamelength], char _payload[messagelength]) { badge_message_chat_t data; - strcpy(data.nickname, _nickname); - strcpy(data.payload, _payload); + strncpy(data.nickname, _nickname, strlen(_nickname)); + strncpy(data.payload, _payload, strlen(_payload)); badge_communication_send_chat(&data); } @@ -106,8 +106,8 @@ void DisplayBillboard(int _addmessageflag, char* _nickname, char* _message) { // if the add message flag is raised if (_addmessageflag) { // add message to the board - strcpy(nicknamearray[messagecursor], _nickname); - strcpy(messagearray[messagecursor], _message); + strncpy(nicknamearray[messagecursor], _nickname, strlen(_nickname)); + strncpy(messagearray[messagecursor], _message, strlen(_message)); // increment or reset cursor if (messagecursor >= nbmessages - 1) { @@ -121,10 +121,11 @@ void DisplayBillboard(int _addmessageflag, char* _nickname, char* _message) { int j = 0 - i; if (j < 0) j = nbmessages - i; - strcpy(billboardline, nicknamearray[j]); + strcpy(billboardline, ""); + strncpy(billboardline, nicknamearray[j], strlen(nicknamearray[j])); if (strlen(nicknamearray[j]) != 0) // don't put the colon if there is no message strcat(billboardline, ": "); - strcat(billboardline, messagearray[j]); + strncat(billboardline, messagearray[j], strlen(messagearray[j])); int shift = (i + messagecursor + nbmessages - 1) % nbmessages; diff --git a/main/modules/screen_repertoire.c b/main/modules/screen_repertoire.c index fdd93d1..d598677 100644 --- a/main/modules/screen_repertoire.c +++ b/main/modules/screen_repertoire.c @@ -58,8 +58,8 @@ bool StoreRepertoire(char _repertoryIDlist[nicknamelength], uint8_t mac[8], uint char strmac[15] = "MAC"; char nb[15]; snprintf(nb, 15, "%d", _ID); - strcat(strnick, nb); - strcat(strmac, nb); + strncat(strnick, nb, strlen(nb)); + strncat(strmac, nb, strlen(nb)); if (log) { ESP_LOGE(TAG, "StoreRepertoire: "); ESP_LOGE(TAG, "set _ID: %d", _ID); @@ -82,13 +82,13 @@ bool GetRepertoire(char _repertoryIDlist[nicknamelength], uint8_t mac[8], uint16 // This is used as I can't get nvs_get_str_wrapped to populate a 1 dimension char array char value[2][nicknamelength] = {"", ""}; snprintf(nb, 15, "%d", _ID); - strcat(strnick, nb); - strcat(strmac, nb); + strncat(strnick, nb, strlen(nb)); + strncat(strmac, nb, strlen(nb)); bool res = nvs_get_str_wrapped("Repertoire", strnick, value[0], sizeof(value[0])); if (res == ESP_OK) { res = nvs_get_u8_blob_wrapped("Repertoire", strmac, mac, 8); } - strcpy(_repertoryIDlist, value[0]); + strncpy(_repertoryIDlist, value[0], strlen(value[0])); if (log) { ESP_LOGE(TAG, "GetRepertoire: "); ESP_LOGE(TAG, "ID: %d", _ID); @@ -106,7 +106,7 @@ void send_repertoire(void) { char _nickname[nicknamelength] = "Error get owner nickname"; nvs_get_str_wrapped("owner", "nickname", _nickname, sizeof(_nickname)); - strcpy(data.nickname, _nickname); + strncpy(data.nickname, _nickname, strlen(_nickname)); badge_communication_send_repertoire(&data); @@ -187,12 +187,17 @@ void Display_repertoire( char rightfield[nicknamelength] = ""; char buf[10] = ""; if (!show_name_or_mac) { - strcpy(leftfield, _repertoryIDlist[i]); + strncpy(leftfield, _repertoryIDlist[i], strlen(_repertoryIDlist[i])); if (i <= nb_item_sur - 1) - strcpy(rightfield, _surroundingIDlist[i + page * maxperpage]); + strncpy( + rightfield, + _surroundingIDlist[i + page * maxperpage], + strlen(_surroundingIDlist[i + page * maxperpage]) + ); } else { for (int y = 0; y < 8; y++) { snprintf(buf, 10, "%02x", repertory_mac[i][y]); + strcat(leftfield, strcat(buf, ":")); if (i <= nb_item_sur - 1) snprintf(buf, 10, "%02x", surrounding_mac[i + page * maxperpage][y]); @@ -224,11 +229,11 @@ void Display_repertoire( char str[20]; // page snprintf(str, 12, "%d", page + 1); - strcat(pagefooter, str); + strncat(pagefooter, str, strlen(str)); strcat(pagefooter, "/"); // max page snprintf(str, 12, "%d", max_page + 1); - strcat(pagefooter, str); + strncat(pagefooter, str, strlen(str)); pax_center_text(gfx, BLACK, font1, fontsizeS, gfx->height / 2, gfx->width - pagefooter_o_x, pagefooter); } } @@ -479,7 +484,7 @@ screen_t screen_repertoire_entry(QueueHandle_t application_event_queue, QueueHan if (nbrepertoryID == 0) break; char promt[128] = "Are you sure you want to remove "; - strcat(promt, repertoryIDlist[cursor.yabs]); + strncat(promt, repertoryIDlist[cursor.yabs], strlen(repertoryIDlist[cursor.yabs])); strcat(promt, "?"); if (Screen_Confirmation(promt, application_event_queue, keyboard_event_queue)) { for (int i = cursor.yabs; i < nbrepertoryID; i++) { @@ -505,7 +510,11 @@ screen_t screen_repertoire_entry(QueueHandle_t application_event_queue, QueueHan strcpy(surroundingIDlist[cursor.yabs], ""); nbsurroundingID--; for (int i = cursor.yabs; i < nbsurroundingID; i++) { - strcpy(surroundingIDlist[i], surroundingIDlist[i + 1]); + strncpy( + surroundingIDlist[i], + surroundingIDlist[i + 1], + strlen(surroundingIDlist[i + 1]) + ); for (int y = 0; y < 8; y++) surrounding_mac[i][y] = surrounding_mac[i + 1][y]; } nbrepertoryID++; @@ -526,7 +535,7 @@ screen_t screen_repertoire_entry(QueueHandle_t application_event_queue, QueueHan badge_message_repertoire_t* ts = &event.args_communication.data_repertoire; char inboundnick[nicknamelength] = ""; uint8_t _inbound_mac[8]; - strcpy(inboundnick, ts->nickname); + strncpy(inboundnick, ts->nickname, strlen(ts->nickname)); for (int i = 0; i < 8; i++) { _inbound_mac[i] = event.args_communication.src.long_address[i]; ESP_LOGI(TAG, "MAC: %d \n", _inbound_mac[i]); @@ -571,14 +580,14 @@ screen_t screen_repertoire_entry(QueueHandle_t application_event_queue, QueueHan } if (flag_line_surrounding == 8) { flag_already_exist = 1; - strcpy(surroundingIDlist[i], inboundnick); + strncpy(surroundingIDlist[i], inboundnick, strlen(inboundnick)); } flag_line_surrounding = 0; } // add incoming message to surrounding if ((!flag_already_exist) && (nbsurroundingID < maxIDsurrounding)) { - strcpy(surroundingIDlist[nbsurroundingID], inboundnick); + strncpy(surroundingIDlist[nbsurroundingID], inboundnick, strlen(inboundnick)); for (int i = 0; i < 8; i++) surrounding_mac[nbsurroundingID][i] = _inbound_mac[i]; nbsurroundingID++; }