Move menu code into basic/menu.c, add keyinput with wait, fix some types.

This commit is contained in:
Stefan `Sec` Zehl 2011-07-09 20:19:05 +02:00
parent f941dd5451
commit 68f69b34ee
7 changed files with 122 additions and 107 deletions

View File

@ -70,7 +70,7 @@ void f_send(void){
int dy=8;
uint8_t buf[32];
int status;
int crc;
uint16_t crc;
buf[0]=0x05; // ID
buf[1]=0xEC; // ID
@ -107,18 +107,6 @@ void gotoISP(void) {
/**************************************************************************/
struct MENU_DEF {
char *text;
void (*callback)(void);
};
typedef const struct MENU_DEF * menuentry;
struct MENU {
char *title;
menuentry *entries;
};
const struct MENU_DEF menu_ISP = {"Invoke ISP", &gotoISP};
const struct MENU_DEF menu_init = {"F Init", &f_init};
const struct MENU_DEF menu_status = {"F Status", &f_status};
@ -138,8 +126,6 @@ static menuentry menu[] = {
static const struct MENU mainmenu = {"Mainmenu", menu};
void handleMenu(const struct MENU *the_menu) ;
void main_funk(void) {
backlightInit();
@ -150,96 +136,7 @@ void main_funk(void) {
handleMenu(&mainmenu);
gotoISP();
}
return;
}
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;
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;
lcdFill(0); // clear display buffer
DoString(0, line, the_menu->title);
line += font->u8Height;
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, "* ");
}
line += font->u8Height;
}
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:
lcdFill(0);
DoString(0,0,"Called....");
lcdDisplay(0);
if (the_menu->entries[menuselection]->callback!=NULL)
the_menu->entries[menuselection]->callback();
lcdDisplay(0);
while (getInput()==BTN_NONE) delayms(10);
break;
default:
/* no button pressed */
break;
}
}
return;
}
};
void tick_funk(void){
static int foo=0;

View File

@ -11,6 +11,7 @@ OBJS += voltage.o
OBJS += keyin.o
OBJS += uuid.o
OBJS += crc.o
OBJS += menu.o
LIBNAME=basic

View File

@ -144,6 +144,7 @@ uint32_t GetVoltage(void);
#define BTN_ENTER (1<<4)
uint8_t getInput(void);
uint8_t getInputRaw(void);
uint8_t getInputWait(void);
//uuid.c
uint32_t GetUUID32(void);
@ -153,7 +154,23 @@ uint16_t GetUUID16(void);
void iap_entry(uint32_t param_tab[], uint32_t result_tab[]);
// crc.c
uint16_t crc16(char * buf, int len);
uint16_t crc16(uint8_t * buf, int len);
// menu.c
struct MENU_DEF {
char *text;
void (*callback)(void);
};
typedef const struct MENU_DEF * menuentry;
struct MENU {
char *title;
menuentry *entries;
};
void handleMenu(const struct MENU *the_menu);
#endif

View File

@ -3,7 +3,7 @@
// Calculate the CRC for transmitted and received data using
// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
uint16_t crc16(char * buf, int len){
uint16_t crc16(uint8_t * buf, int len){
unsigned int crc=0xffff;
for(int i=0;i<len;i++){

View File

@ -62,3 +62,11 @@ uint8_t getInputRaw(void) {
return result;
}
uint8_t getInputWait(void) {
uint8_t key;
while ((key=getInput())==BTN_NONE)
delayms(10);
return key;
};

91
firmware/basic/menu.c Normal file
View File

@ -0,0 +1,91 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/fonts.h"
#include "lcd/render.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;
// font = &Font_7x8; // Font needs to be set externally?
for (numentries = 0; the_menu->entries[numentries] != NULL; numentries++);
visible_lines = (RESY/font->u8Height)-1; // subtract title line
#ifdef SAFETY
if (visible_lines < 2) return;
#endif
while (!back) {
uint8_t line = 0;
// Display current menu page
lcdFill(0);
DoString(0, line, the_menu->title);
line += font->u8Height;
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, "* ");
}
line += font->u8Height;
}
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:
DoString(0,0,"Called....");
lcdDisplay(0);
lcdFill(0);
if (the_menu->entries[menuselection]->callback!=NULL)
the_menu->entries[menuselection]->callback();
lcdDisplay(0);
getInputWait();
break;
default:
/* no button pressed */
break;
}
}
return;
}

View File

@ -1,5 +1,6 @@
#include "lpc134x.h"
#include "sysdefs.h"
#include "basic.h"
#include "core/iap/iap.h"