avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] state machine.


From: Herb Peyerl
Subject: [avr-gcc-list] state machine.
Date: Wed, 31 Dec 2003 17:07:20 -0700

So the project I'm currently building has a state diagram with 10
states, 7 inputs, and 4 outputs.  Each state has from 2 to 7 
entry/exit points.

Now, I know how I would implement this in asm, and I know how I
would implement this in C on a big CPU with an operating system.

But I'm torn with how to implement this on an AVR under gcc. So I
wanted to solicit opinions from the greater intelligence...

I'm thinking each state will be its own function, with a single
input that is an integer describing which state was responsible
for calling the function, and the return is an integer describing
which state to call next.

so we have:

int state1..10(int entry_state)
{
        .
        .
        .
        return(next_state);
}


and in main() I figure I have two choices:

1 -             ret = 0
                while(1)
                        switch(ret) {
                                case 1:
                                        ret = state1(ret);
                                        break;
                                case 2:
                                        ret = state1(ret);
                                        break;
                                .
                                .
                        }


2 - Define an array of function pointers where each function pointer
    points to the relevant state.  Then my main() just loops infinitely
    calling the returned state as an index into the array.

My thoughts are that (1) is rather inelegant but will probably produce
the cleanest output... I'm not sure what (2) will translate to in terms
of asm output but would be probably the most elegant.  Will gcc produce
a neat little jump table if I do (2)?

What does everyone else do?

---
Beheading servers since 1999 -- The PC Weasel! http://www.realweasel.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]