Getting Started

(lily) #1

Chapter 10: C Structures


hoa, there I was clicking right along and suddenly, I lost it; maybe its time to

all the states and transitions.


  • We have code for searching each of these arrays. One finds the function
    d with a given state and the other finds the next state to use given
    nt state and the inputs.


uint8_t i; // char i;

input = getkey();


W

review what I’ve said so far?



  • We have two data structures, one for storing the text and function
    associated with a state and one for finding the next state given the current
    state and the input.

  • We have two structure arrays, one for each of the structures, which define


associate
the curre

Now how should we use this? We could sit in an infinite loop checking the inputs
and then looking at the current state and seeing if a transition to a new state is
called for. Hey, sounds like a plan. We could write our main() function as follows,
hopefully commented to crystal clarity:


unsigned char state; // holds the current state, according to
“menu.h”


int main(void)
{
// Define our local copies of the state variables
unsigned char nextstate;
PGM_P statetext;
char (*pStateFunc)(char);
char input;


// Define a loop counter


// Initial state variables
state = nextstate = ST_AVRBF;
statetext = MT_AVRBF;
pStateFunc = NULL;


for (;;) // Main loop
{
// read the joystick buttons

Free download pdf