
//Includes
#include <stdio.h>

//------------------Defines------------------
#define BOOL    char
#define FALSE   0
#define TRUE    (!FALSE)

//Number of menu entry
#define STATE_MAINMENU       	10
#define STATE_SUB1MENU       	20
#define STATE_SUB1SUB1MENU   	21
#define STATE_SUB2MENU       	30

//Key numbers
#define KEY_NULL    			0
#define KEY_ENTER   			1
#define KEY_VORWAERTS    		2
#define KEY_RUECKWAERTS    		3

//------------------Vars------------------
unsigned char state;

//Text for the entries
char ITEM_MAINMENU[] = "Hauptmenue\n";
char ITEM_SUB1MENU[] = "1. Untermenue\n";
char ITEM_SUB1SUB1MENU[] = "1. Untermenue im Untermenue\n";
char ITEM_SUB2MENU[] = "2. Untermenue\n";

//---------Function prototypes------------
unsigned char StateMachine(char state, unsigned char stimuli);
char Submenu1(char input);
char Submenu2(char input);
char Sub1submenu1(char input);



typedef struct
{
    unsigned char state;
    unsigned char input;
    unsigned char nextstate;
} MENU_NEXTSTATE;


typedef struct
{
    unsigned char state;
    char *pText;
    char (*pFunc)(char input);
} MENU_STATE;




MENU_NEXTSTATE menu_nextstate[] = {
//  STATE          		  INPUT       	NEXT STATE
{STATE_MAINMENU, 		KEY_ENTER, 		STATE_SUB1MENU},
{STATE_SUB1MENU, 		KEY_ENTER, 		STATE_SUB1SUB1MENU},
{STATE_SUB1MENU, 		KEY_VORWAERTS, 	STATE_SUB2MENU},
{STATE_SUB1MENU, 		KEY_RUECKWAERTS, STATE_MAINMENU},
{STATE_SUB2MENU, 		KEY_RUECKWAERTS, STATE_SUB1MENU},
{STATE_SUB1SUB1MENU, 	KEY_RUECKWAERTS, STATE_SUB1MENU},
{0,                         0,          	0},
};


MENU_STATE menu_state[] = {
//  STATE               STATE TEXT                  STATE_FUNC
{STATE_MAINMENU,       ITEM_MAINMENU,              NULL},
{STATE_SUB1MENU,       ITEM_SUB1MENU,              Submenu1},
{STATE_SUB1SUB1MENU,   ITEM_SUB1SUB1MENU,          Sub1submenu1},
{STATE_SUB2MENU,       ITEM_SUB2MENU,              Submenu2},
{0,                    NULL,                       NULL},
};





void main(void)

{

    unsigned char nextstate;
    static char *statetext;
    char (*pStateFunc)(char);
    char input=0;
    char i;
    char buttons;
    char last_buttons;


    // Initial state variables
    state = nextstate = STATE_MAINMENU;
    statetext = ITEM_MAINMENU;
    pStateFunc = NULL;


    for (;;)            // Main loop
    {

            // Plain menu text
            if (statetext)
            {
				printf(statetext);

                statetext = NULL;
            }


            scanf( "%d", &input);


            if (pStateFunc)
            {
                // When in this state, we must call the state function
                nextstate = pStateFunc(input);


            }
            else if (input != KEY_NULL)
            {
                // Plain menu, clock the state machine
                nextstate = StateMachine(state, input);
            }

            if (nextstate != state)
            {
                state = nextstate;
                for (i=0; menu_state[i].state; i++)
                {
                    if (menu_state[i].state == state)
                    {
                        statetext =  menu_state[i].pText;
                        pStateFunc = menu_state[i].pFunc;
                        break;
                    }

                }
            }
        }



}


unsigned char StateMachine(char state, unsigned char stimuli)
{
    unsigned char nextstate = state;    // Default stay in same state
    unsigned char i;

    for (i=0; menu_nextstate[i].state; i++)
    {
        if (menu_nextstate[i].state == state && menu_nextstate[i].input == stimuli)
        {
            // This is the one!
            nextstate = menu_nextstate[i].nextstate;
            break;
        }
    }

    return nextstate;
}



char Submenu1(char input){

	static char enter = 1;

    if(enter)
    {
        enter = 0;

   printf("la le lu - Untermenu 1");
    }
    else if (input == KEY_RUECKWAERTS)
    {
        enter = 1;
        return STATE_MAINMENU;
    }

    return STATE_SUB1MENU;


}


 char Submenu2(char input)
 {

	static char enter = 1;

    if(enter)
    {
        enter = 0;

   printf("lu lu lu - Untermenu 2");
    }
    else if (input == KEY_RUECKWAERTS)
    {
        enter = 1;
        return STATE_SUB1MENU;
    }

    return STATE_SUB2MENU;


	 }

 char Sub1submenu1(char input)
 {


	static char enter = 1;

    if(enter)
    {
        enter = 0;

   printf("nur der mann im Mond - Unter untermenu 3");
    }
    else if (input == KEY_RUECKWAERTS)
    {
        enter = 1;
        return STATE_SUB1MENU;
    }

    return STATE_SUB1SUB1MENU;

	 }
