Getting Started

(lily) #1

Chapter 10: C Structures


menu_nextstate[1].state is defined as ST_AVRBF
nput is defined as KEY_NEXT
tstate[1].nextstate is defined as ST_AVRBF_REV

e can search this array of structures to find out what we need to do if we are in

w all we have to do is
nd out what we need to do if our next state is ST_AVRBF_REV, which we can
ntry. We see
at there is a function defined for this state, and equate the function pointer to it.
The


part of which, depending on the
ate d to search the menu_state array. The following code snippit compares
e tate with the next state [if (nextstate != state)] and, being very
are the same. If the states differ, main() sets the
tate variable and then accesses the menu_state
ru y to change the current state to the next state.:

f te != state)

for (i=0; pgm_read_byte(&menu_state[i].state); i++)
{
.state) == state)


=(PGM_P)pgm_read_word(&menu_state[i].pText);
c = (PGM_VOID_P)pgm_read_word(&menu_state[i].pFunc);

}
}


_REV to be the next state, so searching the array
uses the snippet to make the function pointer, pStateFunc, point to the Revision
nction.

menu_nextstate[1].i
menu_nex


W
the ST_AVRBF state and the input is KEY_NEXT, we find that particular
structure and see that the next state is ST_AVRBF_REV. No
fi
do by searching the menu_state array to find the ST_AVRBF_REV e
th
n we can call that function.


Clear so far?


The main() function slorps into an infinite loop,
st , is use
th present s
reasonable, does nothing if they
global state variable to the nexts
st cture arra


i (nextsta
{
state = nextstate;


if (pgm_read_byte(&menu_state[i]
{
statetext
pStateFun
break;
}


Since we took ST_AVRBF
ca
fu

Free download pdf