Joe Parduecanbereachedatwww.smileymicros.com
break;
case 2:
state02();
break;
// Cases 3 through 16 deleted to save space
case 17:
state17();
break;
default:
// TODO: DEAL WITH ERROR
break;
function for the current menu state. We can write this as:
void state03()
{
}
}
Note that this function is not related to any particular
joystick button; it simply calls the state function for the
current state if there is joystick input. The state function
that gets called will look at the global variable 'joystickInput'
value and act accordingly.
We will navigate the menu states by using the joystick
buttons: UP, DOWN, LEFT, and RIGHT. We will decide if
an action other than moving about the menu states should
be done by looking at the joystick button PUSH. So, if we
are in a given menu state and we get any of the movement
states, we move if there is another state in that direction
(and change the menu state to the new state) or we ignore
it if there is no valid state in that direction (no change to
the menu state). If we see that the joystick button is PUSH,
we keep the state the same, but process the PUSH action
specified for that state. If, at this point, your personal state
is thinking about tearing up the magazine and finding a
French waiter to punch out, try to bear with this for a while.
Read the code snippets below and reread this section if
necessary. It may take a while to get your head around
these concepts. We will also keep track of whether the
joystick button has been pressed to the center, which we
will use to take actions not related to menu state changes:
}
switch(joystickInput)
{
case KEY_UP:
// Do nothing
break;
case KEY_DOWN:
// Change to menu state07
menuState = 7;
menuState07Func();
break;
case KEY_LEFT:
// Change to menu state02
menuState = 2;
menuState02Func();
break;
case KEY_RIGHT:
// Change to menu state04
menuState = 4;
menuState04Func();
break;
case KEY_PUSH:
// Call this menu state funcion
// with keyPress =
keyPush = 1; //true
// take action for this state
menuState03Func();
break;
default:
// Do nothing
break;
}
joystickInput = KEY_INVALID;
// do we need to something?
volatile uint8_t keyPush;
To repeat: When we get a new joystick button state,
we can do one of two things: change the menu state or
take an action for the current menu state.
Let's look at a single state and see what can happen.
Look at state03 (CLOCK) in Figure 2 and think about what
happens next when the joystick button changes to:
1. Up - Do nothing.
2. Down - Change to state07.
3. Left - Change to state02.
4. Right - Change to state04.
5. Push - Set keyPush equal true.
When we change menu states, we will call a function
with the actions for that new state. If the button is not a
menu state change but is PUSH, we will call the action
84 January 2009
As usual, this is just an overview of the actual code.
You can play with it by getting Workshop6.zip from
www.smileymicros.com or the Nuts & Volts website at
www.nutsvolts.com. We are doing something different
this time. Instead of using a library for the LCD and joystick
functions, we are using an object module (smws6.o) that
must be located in the C:\Menu Test\default directory so
that the Menu Test project can find it.I avoided mentioning
'state machine' up to this point since these two words —
when connected — tend to send folks running for the exits.
But, guess what? You just studied a state machine and
your head didn't explode (I hope). Yes, state machines are
often the topics of computer science doctoral dissertations,
but they can also be as simple as a switch statement.
We have three more workshops that will complete our
study of introductory C syntax, then we will assume the
reader already knows enough C and move on to some
hardware oriented projects. Next month, we will continue
with more C syntax and build a Butterfly alarm clock. NV