From da0dd55b2055daa9972951ebadb1328b1a0b4f23 Mon Sep 17 00:00:00 2001 From: Brent Perteet Date: Fri, 16 May 2025 07:56:18 -0500 Subject: [PATCH] Refactoring menu code to be non-blocking Added macros for GPIO read/write --- .settings/language.settings.xml | 6 +- .vscode/settings.json | 4 +- source/System/system.h | 12 +- source/controller.c | 0 source/controller.h | 7 ++ source/frq.c | 1 + source/io.h | 30 +++++ source/main.c | 59 ++++++---- source/menu.c | 201 ++++++++++++++++++++++++++++++++ source/menu.h | 28 +++-- source/utils.c | 9 +- 11 files changed, 316 insertions(+), 41 deletions(-) create mode 100644 source/controller.c create mode 100644 source/controller.h create mode 100644 source/io.h diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index c430e45..00fcf06 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -14,8 +14,8 @@ - - + + diff --git a/.vscode/settings.json b/.vscode/settings.json index a584167..c540288 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,8 @@ "clock_config.h": "c", "utils.h": "c", "ports.h": "c", - "mode.h": "c" + "mode.h": "c", + "arm_math.h": "c", + "io.h": "c" } } \ No newline at end of file diff --git a/source/System/system.h b/source/System/system.h index 2549823..2d0d02d 100644 --- a/source/System/system.h +++ b/source/System/system.h @@ -14,7 +14,11 @@ #define SYS_INFO_LENGTH 24 //max string length for setup parameters (model, mfg, etc.) - +typedef enum +{ + GUI_MODE_NORMAL = 0, + GUI_MODE_MENU +} GuiMode_t; //System data @@ -30,7 +34,11 @@ typedef struct uint32_t language; //system language -}SYSTEM_DATA_t; + uint32_t systemTime; // system time in milliseconds since boot + + GuiMode_t guiMode; + +} SYSTEM_DATA_t; void SYS_LoadFactoryDefaults(void); diff --git a/source/controller.c b/source/controller.c new file mode 100644 index 0000000..e69de29 diff --git a/source/controller.h b/source/controller.h new file mode 100644 index 0000000..c04a65e --- /dev/null +++ b/source/controller.h @@ -0,0 +1,7 @@ +#ifndef _CONTROLLER_H +#define _CONTROLLER_H + + + + +#endif \ No newline at end of file diff --git a/source/frq.c b/source/frq.c index 6a5a07b..002cc6d 100644 --- a/source/frq.c +++ b/source/frq.c @@ -247,6 +247,7 @@ void ClearFreqArray(void) } } +#define PIN_OUTPUT() void FREQ_Init(void) { uint32_t tmp; diff --git a/source/io.h b/source/io.h new file mode 100644 index 0000000..9be6c98 --- /dev/null +++ b/source/io.h @@ -0,0 +1,30 @@ +#ifndef IO_H_ +#define IO_H_ + +#include "fsl_gpio.h" + +#define LOW 0 +#define HIGH 1 + +#define PORT0 0 +#define PORT1 1 + +#define PIN_VBUS (PORT1, 6) +#define PIN_POT_CS (PORT1, 7) +#define PIN_SIG_CS (PORT1, 9) +#define PIN_SD_CS (PORT1, 10) +#define PIN_RAMP_CS (PORT1, 11) +#define PIN_SIG_RESET (PORT1, 12) +#define PIN_SD_RESET (PORT1, 13) +#define PIN_RAMP_RST (PORT1, 14) +#define PIN_POWER_CTL (PORT1, 16) +#define PIN_ESTOP (PORT1, 17) + +#define _P1(p1, p2) p1 +#define _P2(p1, p2) p2 + +#define GPIO_WRITE(port_pin, value) GPIO_PinWrite(GPIO, _P1 port_pin, _P2 port_pin, value ) +#define GPIO_READ(port_pin) GPIO_PinRead (GPIO, _P1 port_pin, _P2 port_pin) + + +#endif diff --git a/source/main.c b/source/main.c index 7b07b1e..f776457 100644 --- a/source/main.c +++ b/source/main.c @@ -49,6 +49,9 @@ //temp #include "fsl_sctimer.h" +#include "System/system.h" +#include "io.h" + extern volatile uint8_t BC_Duty_Cycle; uint8_t Task,Tx_Time_Out_Flag,i,Test_Mode; @@ -68,6 +71,7 @@ extern uint8_t frq_update_flag,Ports_Cleared_Flag; extern HARDWARE_FIX_t hwf; extern uint8_t tempString[40]; // Todo move +extern SYSTEM_DATA_t sys; #define BYPASS_USB_SAFETY 0 //Use for debugging #define DDS_PLL_TEST 0 @@ -203,6 +207,10 @@ int main(void) USB_Init(); + Menu_init(); + + sys.guiMode = GUI_MODE_NORMAL; + #if DDS_PLL_TEST //# Insert special test code here. @@ -215,24 +223,6 @@ uint32_t tmp; //# End Test code #endif -//######################### -// if(Read_Model_type() == LEICA) // Read Model Patches erroneous boards sent to Leica 8/2/24 -// { -// sprintf(tempString, "%d", hwf.mainPcbaPN); - -// if(strncmp(tempString,"208021",6) == 0) // check for HW rev. -// { -// hwf.mainPcbaPN = 208025; -// EE_WriteUINT32(EE_HWFIX_MAIN_PCBA_PN, hwf.mainPcbaPN);// Change to 208025 Assembly - -// } -// } - -// Leica_Patch(); // Patches erroneous boards sent to Leica 8/2/24 - -// Umag_Patch(); -//######################## - while(1) { @@ -243,7 +233,7 @@ uint32_t tmp; #if !BYPASS_USB_SAFETY //##################### Check_For_Usb(); - if(GPIO_PinRead(GPIO,1,6)) + if(GPIO_READ(PIN_VBUS)) { Task = USB_SAFE_TASK; // process primary keys as safety menu safe_key(); // process primary keys as safety menu @@ -253,7 +243,7 @@ uint32_t tmp; } else - if(!GPIO_PinRead(GPIO,1,6) && Ports_Cleared_Flag) + if(!GPIO_READ(PIN_VBUS) && Ports_Cleared_Flag) { Normal_Init(); // USB is unplugged so reinitialize } @@ -263,9 +253,32 @@ uint32_t tmp; Delay_Ticks(1); //10mS delay + KEY_Update(); + + + + if (sys.guiMode == GUI_MODE_MENU) + { + Menu_service(); + } + else + { + switch(KEY_GetPressed()) + { + case MENU_KEY: + { + + Menu_mainMenu(); + break; + + } + } + Display_Update(); + } + #if !BYPASS_USB_SAFETY - if((tickCount++ % 10 == 0) && !GPIO_PinRead(GPIO,1,6)) //every 10 ticks = 100mS AND !USB + if((tickCount++ % 10 == 0) && !GPIO_READ(PIN_VBUS)) //every 10 ticks = 100mS AND !USB #else if(tickCount++ % 10 == 0) //every 10 ticks = 100mS #endif @@ -287,7 +300,7 @@ uint32_t tmp; break; case PRIMARY_TASK: - pro_key(); // process primary keys front 6 first menu + //pro_key(); // process primary keys front 6 first menu break; case MENU_TASK: // Allows user to select options @@ -378,7 +391,7 @@ uint32_t tmp; if (Taps_adjust_timer == 0) Check_Taps(); // Check for optimum Taps #endif - Display_Update(); +// Display_Update(); } diff --git a/source/menu.c b/source/menu.c index 3330be7..35fa954 100644 --- a/source/menu.c +++ b/source/menu.c @@ -40,6 +40,45 @@ #define MENU_TIMER_PERIOD_MS (100) //100mS for 10Hz update +#define MENU_STACK_SIZE 4 +#define MENU_ITEM_SIZE 10 + +typedef struct Menu_s Menu_t; + +typedef int (*MenuHandler_t)(Menu_t *menu); + + +struct Menu_s +{ + MenuHandler_t handler; + bool init; + bool draw; + int selected; + MENU_ITEM_t items[MENU_ITEM_SIZE]; + int itemCount; + int displayIndex; + +}; + +typedef struct MenuData_s +{ + int stackCount; + Menu_t menuStack[MENU_STACK_SIZE]; + Menu_t *currentMenu; + +} MenuData_t; + + +typedef struct { + bool exitToMainScreen; +} MENU_t; + + + + + + + @@ -47,6 +86,8 @@ * Variables ******************************************************************************/ +static MenuData_t _menuData; + MENU_t menu; MENU_ITEM_t mainMenu[MAIN_MENU_NUM_TX10]; @@ -69,6 +110,9 @@ extern uint8_t Port_State[]; extern HARDWARE_FIX_t hwf; + + + /******************************************************************************* * Static Function Declarations ******************************************************************************/ @@ -92,10 +136,126 @@ static void DisplayLanguageMenu(uint32_t selected); static void DisplayRegulatoryInfo(void); + + /******************************************************************************* * Static Functions ******************************************************************************/ +static int handleMainMenu(Menu_t *menu) +{ + if (menu->init) + { + int itemCount = 0; + + menu->items[itemCount].pMonoIcon = menuMore; + menu->items[itemCount].id = MENU_ID_SYSINFO; + strcpy(menu->items[itemCount].text, "System Information"); //System info screen + itemCount++; + + menu->items[itemCount].pMonoIcon = menuMore; + menu->items[itemCount].id = MENU_ID_FREQUENCIES; + strcpy(menu->items[itemCount].text, "Frequencies"); //Frequency Selection Menu + itemCount++; + + menu->items[itemCount].pMonoIcon = 0; + menu->items[itemCount].id = MENU_ID_AUTOSHUTDOWN; + strcpy(menu->items[itemCount].text, "Auto Shutdown"); //Auto Shutdown selection + itemCount++; + + menu->items[itemCount].pMonoIcon = menuMore; + menu->items[itemCount].id = MENU_ID_LANGUAGE; + strcpy(menu->items[itemCount].text, "Language"); //Language Selection Menu + itemCount++; + + menu->itemCount = itemCount; + menu->init = false; + + menu->draw = true; + + } + + + switch(KEY_GetPressed()) + { + case KEY_UP: + { + if(--menu->selected < 0) + { + menu->selected = 0; + } + menu->draw = true; + break; + } + case KEY_DOWN: + { + if(++menu->selected >= menu->itemCount) + { + menu->selected = menu->itemCount - 1; + } + menu->draw = true; + break; + } + } + + if (menu->draw) + { + + LCD_Clear(); + + if(menu->selected >= (menu->displayIndex + MENU_MAX_LINES_DISPLAYED)) + { + menu->displayIndex++; + } + else if(menu->selected < menu->displayIndex) + { + menu->displayIndex--; + } + + + // Draw menu items + MENU_ITEM_t *item; + + for(uint32_t i = menu->displayIndex; i < menu->displayIndex + menu->itemCount; i++) //this can draw extra lines off the screen but we don't care + { + item = &menu->items[i]; + + //Menu strings + FL_DrawTranslatedString(item->text, MENU_MAIN_TEXT_X, MENU_MAIN_TEXT_Y_START + (i-menu->displayIndex)*MENU_LINE_HEIGHT, MENU_FONT, LCD_DRAW_SET, FL_ALIGN_LEFT); + + + // Draw item status + if (item->pMonoIcon != NULL) + { + GL_DrawMonoBitmap(item->pMonoIcon, MENU_MAIN_STATUS_X, MENU_MAIN_TEXT_Y_START + (i-menu->displayIndex)*MENU_LINE_HEIGHT + MENU_MAIN_STATUS_Y_OFF, LCD_DRAW_SET); + } + + if (item->id == MENU_ID_AUTOSHUTDOWN) + { + FL_DrawTranslatedString(tmr_GetAutoSDTimerString(), MENU_MAIN_STATUS_X, MENU_MAIN_TEXT_Y_START + (i - menu->displayIndex)*MENU_LINE_HEIGHT, MENU_FONT, LCD_DRAW_SET, FL_ALIGN_LEFT); + } + + + } + + //Draw selection bar + uint32_t selRectY0 = MENU_MAIN_TEXT_Y_START + (menu->selected - menu->displayIndex)*MENU_LINE_HEIGHT; + GL_DrawFilledRectangle(MENU_SEL_RECT_X0, selRectY0, MENU_SEL_RECT_X1, selRectY0 + MENU_LINE_HEIGHT, LCD_DRAW_XOR); + + //Draw menu icons + //Use DrawMenuBitmap(mainMenu[i], x, y) + + //DrawMenuScrollBar(displayIndex, menuNum); + + LCD_Update(); + + menu->draw = false; + } + + + + return 0; +} @@ -865,6 +1025,11 @@ void MENU_Init(void) ClearMenuItems(mainMenu, MAIN_MENU_NUM_TX10); ClearMenuItems(langMenu, LANG_MENU_NUM); + for (int i=0; i < MENU_STACK_SIZE; ++i) + { + _menuData.menuStack[i].selected = 0; + _menuData.menuStack[i].handler = NULL; + } //main menu uint32_t i = 0; @@ -1093,6 +1258,42 @@ void MENU_DisplayMain(uint32_t selected) +void Menu_init(void) +{ + _menuData.currentMenu = NULL; + _menuData.stackCount = 0; + for (int i=0; ihandler = handleMainMenu; + menu->selected = 0; + menu->init = true; + + _menuData.currentMenu = menu; +} + +void Menu_service(void) +{ + // run the menu state machine and prevent blocking + if (_menuData.currentMenu != NULL) + { + _menuData.currentMenu->handler(_menuData.currentMenu); + } + +} diff --git a/source/menu.h b/source/menu.h index d409d9f..a95067c 100644 --- a/source/menu.h +++ b/source/menu.h @@ -60,20 +60,28 @@ #define MENU_SEL_RECT_RADIUS 5 -typedef struct { - bool exitToMainScreen; -}MENU_t; +typedef enum +{ + MENU_ID_NONE = 0, + MENU_ID_AUTOSHUTDOWN, + MENU_ID_FREQUENCIES, + MENU_ID_SYSINFO, + MENU_ID_LANGUAGE, +} MenuItemId_t; -//Menu Item Struct -//contains pointers to both mono and color bitmaps. mono bitmap requires a color. -//each item may have a mono bitmap, a color bitmap, or no bitmap. -typedef struct { - - uint16_t * pMonoIcon; // bitmap +typedef struct MENU_ITEM_s +{ + uint32_t * pMonoIcon; // bitmap char text[MENU_MAX_STRING_LENGTH]; -}MENU_ITEM_t; + MenuItemId_t id; +} MENU_ITEM_t; + + +void Menu_init(void); +void Menu_mainMenu(void); +void Menu_service(void); void MENU_Init(void); void MENU_Main(void); diff --git a/source/utils.c b/source/utils.c index d80eb10..091d852 100644 --- a/source/utils.c +++ b/source/utils.c @@ -28,6 +28,7 @@ #include #include "hwFixes.h" #include "System\system.h" +#include "io.h" //#define ON 1 @@ -130,9 +131,13 @@ void Select_Bypass(RELAY_SELECT_t Bypass) // Bypass allows transmitting w/o full void Select_Estop(RELAY_SELECT_t E_stop) { if (E_stop) - GPIO_PinWrite(GPIO, 1, ESTOP, 0); // Isolated + { + GPIO_WRITE(PIN_ESTOP, LOW); + //GPIO_PinWrite(GPIO, 1, ESTOP, 0); // Isolated + } else - GPIO_PinWrite(GPIO, 1, ESTOP, 1); // Not Isolated + //GPIO_PinWrite(GPIO, 1, ESTOP, 1); // Not Isolated + GPIO_WRITE(PIN_ESTOP, HIGH); } void Select_Transformer()