crashtest-r0ket/firmware/basic/menu.c

90 lines
2.7 KiB
C
Raw Normal View History

#include <sysinit.h>
#include "basic/basic.h"
2011-07-12 23:13:19 +00:00
#include "lcd/print.h"
/**************************************************************************/
void handleMenu(const struct MENU *the_menu) {
uint8_t back = 0;
int8_t menuselection = 0;
uint8_t numentries = 0;
uint8_t visible_lines = 0;
uint8_t current_offset = 0;
if (the_menu == NULL) return;
2011-08-01 22:03:37 +00:00
setSystemFont();
for (numentries = 0; the_menu->entries[numentries] != NULL; numentries++);
2011-08-01 22:03:37 +00:00
visible_lines = lcdGetVisibleLines()-1; // subtract title line
#ifdef SAFETY
if (visible_lines < 2) return;
#endif
while (!back) {
// Display current menu page
2011-08-01 03:10:37 +00:00
lcdClear();
lcdPrintln(the_menu->title);
for (uint8_t i = current_offset; i < (visible_lines + current_offset) && i < numentries; i++) {
if (i == menuselection) {
2011-08-01 03:10:37 +00:00
lcdPrint("*");
}
2011-08-01 03:10:37 +00:00
lcdSetCrsrX(14);
lcdPrintln(the_menu->entries[i]->text);
}
2011-08-01 03:10:37 +00:00
lcdRefresh();
switch (getInputWait()) {
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:
2011-07-12 23:13:19 +00:00
lcdClear();
lcdPrintln("Called...");
lcdRefresh();
getInputWaitRelease();
if (the_menu->entries[menuselection]->callback!=NULL)
the_menu->entries[menuselection]->callback();
2011-08-01 22:03:37 +00:00
lcdRefresh();
getInputWait();
break;
default:
/* no button pressed */
break;
}
getInputWaitRelease();
}
return;
}