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

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

Re: [avr-gcc-list] state machine.


From: Ralph Mason
Subject: Re: [avr-gcc-list] state machine.
Date: Thu, 01 Jan 2004 19:27:47 +1300
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4

Herb Peyerl wrote:

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?

I use a hierarchical state machine framework.

See www.quantum-leaps.com,

And before anyone gets on my case for the size of it all, I can easily produce a state machine that I know is correct And the implementation will work exactly as the design.

In a little twist on the book I use xml to store my state machines and generate the code using XSL. The XML very cleanly shows the hierarchy of the state machine. The XSL does all the dirty work that I would otherwise forget to do or mess up manually.
Ralph



reply via email to

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