Best menu system in existence

Just like a drug addict, I say ‘This will be my last menu I create from scratch’, and yet, a week or so passes and I create another. And another. Well it’s a good thing that I have this addiction, because each menu system turns out to be better than the last.

It just so happens that this menu system that I’ve created, is the best currently, and that can ever be. Yep, it’s true.

I’ve just attached them via pastebin, since the syntax highlighting is so nice, I’ve created an account and will put my code there on a temporary basis. If there is a bigger project, then it can go into its own sourceforge project account. Ultimately, it is your responsibility to save this, since I cannot guarantee that wordpress or pastebin will always be around. If you like it, take it 🙂 The compiled size is 3516 bytes, which is pretty decent in my view(and that is including the ShiftRegLCD LCD functions).

Next up, adding things to it. I think I’ll go ahead and add all of the BT and XBee configuration code, then I’ll start playing with the NunChucks! Can’t wait to start playing with solar panels…

This is the main code, which includes the entire menu system: http://pastebin.com/3KXvST5V
Here is a simple file that has three simple functions to handle multiple buttons on a ADC channel: http://pastebin.com/Ddj8pcbm

Raw paste of code is below (not pretty :P)

Ehh, raw paste of code, why not 😛

#include
#include

#define HMENU 4
#define WMENU 20

//#define DEBUG 1

ShiftRegLCD lcd(4, 3, 5, 4);

#define UPARROW ((uint8_t)3)
#define DOWNARROW ((uint8_t)4)
#define SELECT ((uint8_t)5)
#define HEART ((uint8_t)0)
#define SMILE ((uint8_t)2)
#define KEY ((uint8_t)1)

#define XBEECONF 0
#define BTCONF 1
#define CONNCPU 2
#define CONNGAR 3
#define CONNSAB 4

#define BACKMAIN 0
#define XBEEPPP 1
#define XBEESM1 2
#define XBEECLOSE 3

#define BTPPP 1
#define BTATR0 2
#define BTATH 3
#define BTATF 4
#define BTATD 5
#define BTATA 6
#define BTCLOSE 7
#define MAINSUB 0
#define MAINLEN 4 //actual – 1
const prog_char PROGMEM MenuStr1[] = “XBee Configure”;
const prog_char PROGMEM MenuStr2[] = “BT Configure”;
const prog_char PROGMEM MenuStr3[] = “Connect to CPU”;
const prog_char PROGMEM MenuStr4[] = “Connect to Garden”;
const prog_char PROGMEM MenuStr5[] = “Connect to Sable”;
#define XBEESUB 5 //starts at 2

#define XBEELEN 3 //actual – 1
const prog_char PROGMEM Xbee1[] = “Back to Main”;
const prog_char PROGMEM Xbee2[] = “+++”;
const prog_char PROGMEM Xbee3[] = “SM1,ST3e8,SO1”;
const prog_char PROGMEM Xbee4[] = “WN,CN”;
#define BTSUB 9

#define BTLEN 7 //actual – 1
const prog_char PROGMEM BTser1[] = “Back to Main”;
const prog_char PROGMEM BTser2[] = “+++ const prog_char PROGMEM BTser3[] = “ATR0”;
const prog_char PROGMEM BTser4[] = “ATH (drops conn)”;
const prog_char PROGMEM BTser5[] = “ATF?”;
const prog_char PROGMEM BTser6[] = “ATD”;
const prog_char PROGMEM BTser7[] = “ATA”;
const prog_char PROGMEM BTser8[] = “ATO”;
//currentmenu
#define MAIN MAINSUB
#define XBEE XBEESUB
#define BT BTSUB

PGM_P PROGMEM MenuTable[] = {
MenuStr1,
MenuStr2,
MenuStr3,
MenuStr4,
MenuStr5,
Xbee1,
Xbee2,
Xbee3,
Xbee4,
BTser1,
BTser2,
BTser3,
BTser4,
BTser5,
BTser6,
BTser7,
BTser8,
};
byte Select[8]={ B01000, B01100, B00110, B11111, B11111, B00110, B01100, B01000};

char menu = 0; //0 = main
char menulen = MAINLEN;
char menusel = 0; //cursor
char index[20];
char currmenu = MAIN; //current menu

void setup()
{
lcd.createChar(5, Select);

for(int i=0; iindex[i] = i;
}

//handle things here, not in loop
void select(uint8_t cmenu, uint8_t menus)
{
switch(cmenu)
{
case MAIN:
if(menus == XBEECONF)
{
menu=XBEESUB;
menulen=XBEELEN;
currmenu = XBEE;
}
else if(menus == BTCONF)
{
menu=BTSUB;
menulen=BTLEN;
currmenu = BT;
}
else
menu = MAINSUB;
break;
case XBEE:
if(menus == BACKMAIN)
{
menu=MAINSUB;
menulen=MAINLEN;
currmenu = MAIN;
}
else
menu = XBEESUB;
break;
case BT:
if(menus == BACKMAIN)
{
menu=MAINSUB;
menulen=MAINLEN;
currmenu = MAIN;
}
else
menu = BTSUB;
break;
default:
break;
}
menusel = 0;
}

void loop()
{
uint8_t button;

lcd.clear();
//simple menu here
for(int i=0; i {
lcd.setCursor(1,i);
lcd.print((__FlashStringHelper *)MenuTable[index[i+menu]]);

}

//draw select
lcd.setCursor(0,menusel);
lcd.write(SELECT);
#ifdef DEBUG
lcd.print((char)(menu + ‘0’));
lcd.print(‘ ‘);
lcd.print((char)(menusel + ‘0’));
#endif
button = buttonWait(A5,NO,YES);

if(button == BTND)
{
if(index[menu] – currmenu + HMENU-1 < menulen) //space to potentially go down
menu++;//increment menu
else if(index[menusel + menu] – currmenu < menulen)
menusel++;
else
{
//at bottom of menu, go back to top
menu = currmenu;
menusel = 0;
}
}
if(button == BTNB)
select(currmenu, index[menusel+menu]-currmenu);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s