Sample code for menu Navigation of mobile

Status
Not open for further replies.

misnomerfrenzy

Junior Member level 1
Joined
May 15, 2012
Messages
15
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Visit site
Activity points
1,417
Hello,

I am developing a mobile application on a GSM trainer kit having LPC2148 on it.

My application should do...

Calling
Receive call

Send SMS
Read SMS
Draft SMS
Delete SMS

Save contact info on Phonebook (only no. and name.)


I dont understand how to write code for navigating these menu items through keypad . I am simply out of ideas.

If possible please send me a sample code, which I can refer.
Or atleast give me the link, where I can understand the philosophy of implementing menu navigation.
 

Look at using a "Finite State Machine" - Google "Writing Finite State Machine" for details. Your Menu handler code changes your application's state (based on inputs and current state). Typically a change of state triggers a process or function that does whatever that state requires. This may in turn cause another change of state.

Here's pseudo-code for a simple example:
Code:
[LIST]
[*]At initialization, set state to OFF

[*]Menu Handler code
if KEY=RUN And MOTOR=OFF
[INDENT]Turn motor On
Set state to RUNNING
[/INDENT]
else if KEY=STOP And MOTOR=RUNNING
[INDENT]Turn motor Off
Set state to OFF
[/INDENT]
[/LIST]
 
Sir, that is one solution, I thought of it... taking a simple switch case or if else for all possible conditions.

But, here is twist...

For ex, I have main menu Item as 'SMS' in which the submenu items are "Inbox, Write, Draft, Delete"
Further selecting any of these will lead to further deepening of menu.

In this case simple switch case or if else will be too much complex.

One of my friend suggested me to use two dimensional array with one dimension keeping a track of menu item on screen while the other dimension telling which key even has occurred. Unfortunately, I didnt understand this philosophy
 
Last edited:

There are many ways of implementing Finite State Machines (FSM) - if-else, switch statements, tables of pointers; the list goes on. The important thing is to get a good grasp of how FSM's work. Each state of an FSM can be an FSM itself. So for your application, each menu sub-option can be an FSM.

A simple, somewhat contrived, example follows. There are 3 FSM's; one each for the application, send, and receive.
Code:
typedef enum _APP_STATES {APP_IDLE,  APP_SEND, APP_RECEIVE} APP_STATES;
typedef enum _SEND_STATES {SEND_PROCESS_KEY, SEND_HEADER, SEND_MESSAGE, SEND_TERMINATOR} SEND_STATES;
typedef enum _RECEIVE_STATES {RECEIVE_PROCESS_KEY, RECEIVE_HEADER, RECEIVE_MESSAGE} RECEIVE_STATES;

void main(void)
{
	char key;
	APP_STATES app_state = APP_IDLE;
	SEND_STATES send_state = SEND_PROCESS_KEY;
	RECEIVE_STATES = receive_state = RECEIVE_PROCESS_KEY;

	while(1)
	{
		switch(app_state)
		{
			case APP_IDLE:
				key = GetKey();
				switch(key)
				{
					case 'S':
						app_state = APP_SEND;
						break;
							
					case 'R'
						app_state = APP_RECEIVE;
						break;
				}
				break;
				
			case APP_SEND:
				ProcessSendState();
				break;			

			case APP_RECEIVE:
				ProcessReceiveState();
				break;			
		}				
	}
}


void ProcessSendState(void)
{
	switch(send_state)
	{
		case SEND_PROCESS_KEY:
			key = GetKey();
			switch(key)
			{
				case 'H':	// Send Header
					send_state = SEND_HEADER;
					break;
						
				case 'M'	// Send Nessage
					send_state = SEND_MESSAGE;
					break;

				case 'T'	// Send Terminator
					send_state = SEND_TERMINATOR;
					break;
					
				case 'Q'	// Done sending
					app_state = APP_IDLE;  // Set App stat eto Idle
					break;

			}	
			break;
			
		case SEND_HEADER:
			SendHeader();
			send_state = SEND_PROCESS_KEY;
			break;
			
		case SEND_MESSAGE:
			SendMessage();
			send_state = SEND_PROCESS_KEY;
			break;
			
		case SEND_TERMINATOR	
			SendTerminator();
			send_state = SEND_PROCESS_KEY;
			break;
	}
}
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…