View previous topic :: View next topic |
Author |
Message |
Nordic Guest
|
Switch case and case in case |
Posted: Sun Nov 01, 2009 1:42 pm |
|
|
I got problem in understanding.
I have one standard switch-case solution.
Code: |
char value;
switch (value)
{
case ENTER:
lcd_putc ("Enter something:");
break;
case CHANGE:
lcd_putc ("Change something:");
break;
}
|
So far so good. Now I want to add user interaction (keypad) into the existing case but I can't get it to work. The key scanning is made with kbd_getc() function.
How to make this ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 01, 2009 2:49 pm |
|
|
This example program shows how to poll the keypad driver in a while()
loop. You could put the while() loop code into your case statement.
c:\program files\picc\examples\ex_lcdkb.c
A better way would be to poll the keypad in a periodic interrupt routine
and put the keys into a buffer, similar to the Ex_Sisr.c example.
But, the method I gave above will work and it's quicker to implement. |
|
|
Nordic Guest
|
|
Posted: Mon Nov 02, 2009 4:28 am |
|
|
Yes i can put while loop, but how can i then jump out from the while and get back to case ? |
|
|
Ttelmah Guest
|
|
Posted: Mon Nov 02, 2009 6:26 am |
|
|
That is down to choosing your exit conditions for the while.
Code: |
case x:
do {
//whatever you want here
while (condition to stay in the while is met);
break;
|
It is also worth considering the more structured approach to the whole thing. Handle your hardware task independantly to what any loop is doing, using interrupts. Have (perhaps) a 'tick' at a regular interval, and use this to handle actually scanning keys etc.. Similarly handle serial I/O to/from buffers with interrupts. Then your main code need only worry about being in the state to actually 'do' the operations you want, and can test the buffer contents etc., at sensible points.
You could (for instance) have the interrupt routine, work out what key has actually been pressed, and set a flag, if this is your system's equivalent to the 'break' key, setting a flag when this is seen. You can then sit in the while loop, 'while' this key is not seen.
This is the second solution mentioned by PCM programmer.
Best Wishes |
|
|
JohnCA Guest
|
|
Posted: Sat Nov 07, 2009 4:54 am |
|
|
This is partly different but the problem is the same.
Code: | startMenu();
// i need to be here or above.
while(TRUE)
{
k=kbd_getc();
if (k)
{
if (state == enterNumber)
{
if(k >='0' && k <= '9'){
number = number*10 + k-'0';
lcd_putc(k);
}
// how to pull out the value of k and set this to case 1
switch(k)
{
case '*':
lcd_putc("I am in case *");
switch (flagStar)
{
case 0:
lcd_putc("enter value:");
flagStar = 1;
break;
case 1:
lcd_putc("Your number is:", k);
flagA=2;
break;
break;
case '#':
break;
}
}
}
else
startAct(k);
} |
from other include file:
case enterNumber:
lcd_putc("Section for add number");
break;
So what is happening:
You get main menu with two options.
Option1
Option2
Option1 has one sub option called enterNumber.
When you hit * on option1 you landed in enterNumber state.
The "Section for add number" is displayed.
When you hit again * you landed in enter value part.
Then you can enter value and after you press * again you get message:
Your number is: the value that user has entered.
When done, you have to press the * button again.
Here is the problem... In this last part i have to "save the data" and jump to root of the menu ( OPTION1, OPTION2 ).
I can't get this part to work.
How to make this ?
John |
|
|
JohnCA Guest
|
|
Posted: Sun Nov 08, 2009 9:12 am |
|
|
Hi guys.
Can someone share some light on the problem described above.
It's kind of tricky for me but this should not be problem for you.
Any suggestion is welcome.
John |
|
|
|