From dd20ee020b03b2a2d99c832713195508e0b3a67e Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Sun, 17 Sep 2023 21:10:39 +0200 Subject: [PATCH 1/4] Move ARRAY_SIZE macro to misc.h. This allows reuse in other files. --- driver/bk1080.c | 2 -- misc.h | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/driver/bk1080.c b/driver/bk1080.c index 7c9d6bb9..5afdbb04 100644 --- a/driver/bk1080.c +++ b/driver/bk1080.c @@ -21,8 +21,6 @@ #include "driver/system.h" #include "misc.h" -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - static const uint16_t BK1080_RegisterTable[] = { 0x0008, 0x1080, 0x0201, 0x0000, 0x40C0, 0x0A1F, 0x002E, 0x02FF, diff --git a/misc.h b/misc.h index 8a7b5906..9af3fd99 100644 --- a/misc.h +++ b/misc.h @@ -20,6 +20,9 @@ #include #include + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + #define IS_MR_CHANNEL(x) ((x) >= MR_CHANNEL_FIRST && (x) <= MR_CHANNEL_LAST) #define IS_FREQ_CHANNEL(x) ((x) >= FREQ_CHANNEL_FIRST && (x) <= FREQ_CHANNEL_LAST) #define IS_NOAA_CHANNEL(x) ((x) >= NOAA_CHANNEL_FIRST && (x) <= NOAA_CHANNEL_LAST) From 2ed7044ba02af6bce6d7ad8351663ec9f11658c4 Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Mon, 18 Sep 2023 16:46:46 +0200 Subject: [PATCH 2/4] ST7565: Less magic numbers, use ARRAY_SIZE --- driver/st7565.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/driver/st7565.c b/driver/st7565.c index 5f4a3302..78bd2926 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -21,6 +21,7 @@ #include "driver/spi.h" #include "driver/st7565.h" #include "driver/system.h" +#include "misc.h" uint8_t gStatusLine[128]; uint8_t gFrameBuffer[7][128]; @@ -59,10 +60,10 @@ void ST7565_BlitFullScreen(void) SPI_ToggleMasterMode(&SPI0->CR, false); ST7565_WriteByte(0x40); - for (Line = 0; Line < 7; Line++) { + for (Line = 0; Line < ARRAY_SIZE(gFrameBuffer); Line++) { ST7565_SelectColumnAndLine(4U, Line + 1U); GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (Column = 0; Column < 128; Column++) { + for (Column = 0; Column < ARRAY_SIZE(gFrameBuffer[0]); Column++) { while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) { } SPI0->WDR = gFrameBuffer[Line][Column]; @@ -83,7 +84,7 @@ void ST7565_BlitStatusLine(void) ST7565_SelectColumnAndLine(4, 0); GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - for (i = 0; i < 0x80; i++) { + for (i = 0; i < ARRAY_SIZE(gStatusLine); i++) { while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) { } SPI0->WDR = gStatusLine[i]; From 20a8b08ada4e1a9fb131467b155a2bb1ed69827c Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Mon, 18 Sep 2023 16:52:48 +0200 Subject: [PATCH 3/4] menu.c: Less magic numbers, at define for screen split. --- ui/menu.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ui/menu.c b/ui/menu.c index 508aee6f..7ab640bd 100644 --- a/ui/menu.c +++ b/ui/menu.c @@ -178,6 +178,9 @@ uint8_t gMenuCursor; int8_t gMenuScrollDirection; uint32_t gSubMenuSelection; +//Pixel where we split the screen: left is menu selection, right is menu item content +#define SPLIT (50) + void UI_DisplayMenu(void) { char String[16]; @@ -186,29 +189,39 @@ void UI_DisplayMenu(void) memset(gFrameBuffer, 0, sizeof(gFrameBuffer)); + /* Print the 3 visible menu items */ for (i = 0; i < 3; i++) { if (gMenuCursor || i) { if ((gMenuListCount - 1) != gMenuCursor || (i != 2)) { - UI_PrintString(MenuList[gMenuCursor + i - 1], 0, 127, i * 2, 8, false); + UI_PrintString(MenuList[gMenuCursor + i - 1], 0, SPLIT - 2, i * 2, 8, false); } } } - for (i = 0; i < 48; i++) { + + /* Invert for the select item */ + for (i = 0; i < SPLIT - 2; i++) { gFrameBuffer[2][i] ^= 0xFF; gFrameBuffer[3][i] ^= 0xFF; } - for (i = 0; i < 7; i++) { - gFrameBuffer[i][48] = 0xFF; - gFrameBuffer[i][49] = 0xFF; + + /* Create a vertical line line, 2 pixels wide */ + for (i = 0; i < ARRAY_SIZE(gFrameBuffer); i++) { + gFrameBuffer[i][SPLIT - 2] = 0xFF; + gFrameBuffer[i][SPLIT - 1] = 0xFF; } + + /* Print the number of the current menu item, 2 digits 1 pixel from split */ NUMBER_ToDigits(gMenuCursor + 1, String); - UI_DisplaySmallDigits(2, String + 6, 33, 6); + UI_DisplaySmallDigits(2, String + 6, SPLIT - (2 * 8) - 1, 6); + + /* Display submenu arrow */ if (gIsInSubMenu) { - memcpy(gFrameBuffer[0] + 50, BITMAP_CurrentIndicator, sizeof(BITMAP_CurrentIndicator)); + memcpy(gFrameBuffer[0] + SPLIT, BITMAP_CurrentIndicator, sizeof(BITMAP_CurrentIndicator)); } memset(String, 0, sizeof(String)); + /* Get menu item content as string */ switch (gMenuCursor) { case MENU_SQL: case MENU_MIC: From f681f4bb9bc1cd2b44a2d15e01c118b263812ccf Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Mon, 18 Sep 2023 16:56:34 +0200 Subject: [PATCH 4/4] menu.c: Add local print_item to make code more readable. Most arguments are equal, so wrap the function. --- ui/menu.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/ui/menu.c b/ui/menu.c index 7ab640bd..5f2664dc 100644 --- a/ui/menu.c +++ b/ui/menu.c @@ -181,6 +181,15 @@ uint32_t gSubMenuSelection; //Pixel where we split the screen: left is menu selection, right is menu item content #define SPLIT (50) +/* Small helper that does what original code did, + * but moving all consts to a single place, and documenting them a bit. */ +static inline void print_item(const char* string, uint8_t line) +{ + bool centre = true; //Align centre + uint8_t width = 8; //Fixed width in the menu + UI_PrintString(string, SPLIT, 127, line, width, centre); +} + void UI_DisplayMenu(void) { char String[16]; @@ -204,7 +213,7 @@ void UI_DisplayMenu(void) gFrameBuffer[3][i] ^= 0xFF; } - /* Create a vertical line line, 2 pixels wide */ + /* Create a vertical line, 2 pixels wide */ for (i = 0; i < ARRAY_SIZE(gFrameBuffer); i++) { gFrameBuffer[i][SPLIT - 2] = 0xFF; gFrameBuffer[i][SPLIT - 1] = 0xFF; @@ -437,40 +446,39 @@ void UI_DisplayMenu(void) break; } - UI_PrintString(String, 50, 127, 2, 8, true); + print_item(String, 2); if (gMenuCursor == MENU_OFFSET) { - UI_PrintString("MHz", 50, 127, 4, 8, true); + print_item("Mhz", 4); } if ((gMenuCursor == MENU_RESET || gMenuCursor == MENU_MEM_CH || gMenuCursor == MENU_DEL_CH) && gAskForConfirmation) { if (gAskForConfirmation == 1) { - strcpy(String, "SURE?"); + print_item("SURE?", 4); } else { - strcpy(String, "WAIT!"); + print_item("WAIT!", 4); } - UI_PrintString(String, 50, 127, 4, 8, true); } if ((gMenuCursor == MENU_R_CTCS || gMenuCursor == MENU_R_DCS) && gCssScanMode != CSS_SCAN_MODE_OFF) { - UI_PrintString("SCAN", 50, 127, 4, 8, true); + print_item("SCAN", 4); } if (gMenuCursor == MENU_UPCODE) { if (strlen(gEeprom.DTMF_UP_CODE) > 8) { - UI_PrintString(gEeprom.DTMF_UP_CODE + 8, 50, 127, 4, 8, true); + print_item(gEeprom.DTMF_UP_CODE + 8, 4); } } if (gMenuCursor == MENU_DWCODE) { if (strlen(gEeprom.DTMF_DOWN_CODE) > 8) { - UI_PrintString(gEeprom.DTMF_DOWN_CODE + 8, 50, 127, 4, 8, true); + print_item(gEeprom.DTMF_DOWN_CODE + 8, 4); } } if (gMenuCursor == MENU_D_LIST && gIsDtmfContactValid) { Contact[11] = 0; memcpy(&gDTMF_ID, Contact + 8, 4); sprintf(String, "ID:%s", Contact + 8); - UI_PrintString(String, 50, 127, 4, 8, true); + print_item(String, 4); } if (gMenuCursor == MENU_R_CTCS || gMenuCursor == MENU_T_CTCS || @@ -492,16 +500,16 @@ void UI_DisplayMenu(void) } if (gSubMenuSelection == 0xFF || !gEeprom.SCAN_LIST_ENABLED[i]) { - UI_PrintString(String, 50, 127, 2, 8, 1); + print_item(String, 2); } else { - UI_PrintString(String, 50, 127, 0, 8, 1); + print_item(String, 0); if (IS_MR_CHANNEL(gEeprom.SCANLIST_PRIORITY_CH1[i])) { sprintf(String, "PRI1:%d", gEeprom.SCANLIST_PRIORITY_CH1[i] + 1); - UI_PrintString(String, 50, 127, 2, 8, 1); + print_item(String, 2); } if (IS_MR_CHANNEL(gEeprom.SCANLIST_PRIORITY_CH2[i])) { sprintf(String, "PRI2:%d", gEeprom.SCANLIST_PRIORITY_CH2[i] + 1); - UI_PrintString(String, 50, 127, 4, 8, 1); + print_item(String, 4); } } }