From aa0028dcf52ad8c796d56d6598359312e98e3cff Mon Sep 17 00:00:00 2001 From: bsx Date: Wed, 25 May 2011 22:24:53 +0200 Subject: [PATCH] implemented menu scrolling (sec helped) --- modules/menutest.c | 171 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 130 insertions(+), 41 deletions(-) diff --git a/modules/menutest.c b/modules/menutest.c index b4d1afa..bff3036 100644 --- a/modules/menutest.c +++ b/modules/menutest.c @@ -6,13 +6,19 @@ #include "lcd/backlight.h" #include "lcd/allfonts.h" +#define BTN_NONE 0 +#define BTN_UP (1<<0) +#define BTN_DOWN (1<<1) +#define BTN_LEFT (1<<2) +#define BTN_RIGHT (1<<3) +#define BTN_ENTER (1<<4) + void ReinvokeISP(void); void incBacklight(void); void decBacklight(void); void gotoISP(void); -void handleMenu(void); /**************************************************************************/ @@ -23,16 +29,79 @@ struct MENU_DEF { typedef const struct MENU_DEF * menuentry; +struct MENU { + char *title; + menuentry *entries; +}; + const struct MENU_DEF menu_incBL = {"Backlight++", &incBacklight}; const struct MENU_DEF menu_decBL = {"Backlight--", &decBacklight}; const struct MENU_DEF menu_ISP = {"Invoke ISP", &gotoISP}; +const struct MENU_DEF menu_Ep = {"p1e4", NULL}; +const struct MENU_DEF menu_Eq = {"p1e5", NULL}; +const struct MENU_DEF menu_Er = {"p1e6", NULL}; +const struct MENU_DEF menu_Es = {"p1e7", NULL}; +const struct MENU_DEF menu_Et = {"p2e1", NULL}; +const struct MENU_DEF menu_Eu = {"p2e2", NULL}; +const struct MENU_DEF menu_Ev = {"p2e3", NULL}; static menuentry menu[] = { &menu_incBL, &menu_decBL, &menu_ISP, + &menu_Ep, + &menu_Eq, + &menu_Er, + &menu_Es, + &menu_Et, + &menu_Eu, + &menu_Ev, + &menu_Ev, + &menu_ISP, + NULL, }; +static const struct MENU mainmenu = {"Mainmenu", menu}; + +void handleMenu(const struct MENU *the_menu) ; +void delayms(int); + +uint8_t getInput(void) { + uint8_t result = BTN_NONE; + + if (gpioGetValue(RB_BTN1)==0) { + while(gpioGetValue(RB_BTN1)==0); + result += BTN_UP; + } + + if (gpioGetValue(RB_BTN0)==0) { + while(gpioGetValue(RB_BTN0)==0); + result += BTN_DOWN; + } + + if (gpioGetValue(RB_BTN4)==0) { + while(gpioGetValue(RB_BTN4)==0); + result += BTN_ENTER; + } + + if (gpioGetValue(RB_BTN2)==0) { + while(gpioGetValue(RB_BTN2)==0); + result += BTN_LEFT; + } + + if (gpioGetValue(RB_BTN3)==0) { + while(gpioGetValue(RB_BTN3)==0); + result += BTN_RIGHT; + } + + if (result == (BTN_LEFT+BTN_RIGHT)){ /* Development hack */ + gotoISP(); + } + + return result; +} + + void module_menutest(void) { backlightInit(); @@ -49,69 +118,89 @@ void module_menutest(void) { DoString(0, 0, "MenĂ¼"); - if (gpioGetValue(RB_BTN4)==0) { - while(gpioGetValue(RB_BTN4)==0); - handleMenu(); + if (getInput() == BTN_ENTER) { + handleMenu(&mainmenu); } } return; } -void handleMenu(void) { +void handleMenu(const struct MENU *the_menu) { uint8_t back = 0; - uint8_t menuselection = 0; + int8_t menuselection = 0; + uint8_t numentries = 0; + uint8_t visible_lines = 0; + uint8_t current_offset = 0; + + if (the_menu == NULL) return; font = &Font_7x8; + for (numentries = 0; the_menu->entries[numentries] != NULL; numentries++); + + visible_lines = RESY/font->u8Height; + + if (visible_lines < 2) return; + + visible_lines--; // subtract title line + while (!back) { uint8_t line = 0; - uint8_t col = 0; - - lcdDisplay(0); - delayms(10); lcdFill(0); // clear display buffer - DoString(0, line, "Menu"); + DoString(0, line, the_menu->title); line += font->u8Height; - if (gpioGetValue(RB_BTN1)==0) { - while(gpioGetValue(RB_BTN1)==0); - if (menuselection != 0) menuselection--; - } - - if (gpioGetValue(RB_BTN0)==0) { - while(gpioGetValue(RB_BTN0)==0); - if (menuselection != 2) - menuselection++; - else - menuselection = 0; // wrap around - } - - for (uint8_t i = 0; i < 3; i++) { - DoString(14, line + i*8, menu[i]->text); + for (uint8_t i = current_offset; i < (visible_lines + current_offset) && i < numentries; i++) { + DoString(14, line, the_menu->entries[i]->text); if (i == menuselection) { - DoString(0, line + i*8, "* "); + DoString(0, line, "* "); } + line += font->u8Height; } - if (gpioGetValue(RB_BTN4)==0) { - while(gpioGetValue(RB_BTN4)==0); - menu[menuselection]->callback(); + lcdDisplay(0); + + switch (getInput()) { + case BTN_UP: + menuselection--; + if (menuselection < current_offset) { + if (menuselection < 0) { + menuselection = numentries-1; + current_offset = ((numentries-1)/visible_lines) * visible_lines; + } else { + current_offset -= visible_lines; + } + } + break; + case BTN_DOWN: + menuselection++; + if (menuselection > (current_offset + visible_lines-1) || menuselection >= numentries) { + if (menuselection >= numentries) { + menuselection = 0; + current_offset = 0; + } else { + current_offset += visible_lines; + } + } + break; + case BTN_LEFT: + return; + case BTN_RIGHT: + if (the_menu->entries[menuselection]->callback!=NULL) + the_menu->entries[menuselection]->callback(); + break; + case BTN_ENTER: + if (the_menu->entries[menuselection]->callback!=NULL) + the_menu->entries[menuselection]->callback(); + break; + default: + /* no button pressed */ + break; } - if (gpioGetValue(RB_BTN2)==0) { - while(gpioGetValue(RB_BTN2)==0); - back = 1; - } - - // fallback ISP via BTN3 ;) - - if (gpioGetValue(RB_BTN3)==0) { - while(gpioGetValue(RB_BTN3)==0); - gotoISP(); - } } return;