CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

pointers to functions as a state machine

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

pointers to functions as a state machine
PostPosted: Mon Oct 23, 2006 10:26 pm     Reply with quote

I’ll show a rudimentary pointer-to-function based state machine as a compared to a rudimentary nested switch state machine. Imagine a device that has a set of states (STATE1, STATE2, etc) and a set of messages (MSG1, MSG2, etc).

Rudimentary nested switch state machine
Code:

int g_iState;

void AllStateHandlers(int iMessage)
{
    switch (g_iState)  // first layer of nested switch statement
    {
    case STATE1:
        switch (iMessage)   // second layer of nested switch statement
        {
        case MSG1:
            // do something1
            break;
        case MSG2:
            // do something2
            g_iState = STATE2; // transition to a different state (for a change)
            break;
        // ... other message handlers
        }       
        break;

    case STATE2:
        switch (iMessage)
        {
            // message handlers for STATE2
        }
        break

    case STATE3:
        // ...

    // more states
    }
}

#int_rtcc
void isr_rtcc   // an example source of a message, that needs state-dependent handling
{
    AllStateHandlers(MSG2); // Call a message handler, which will navigate through the switch statement to the current current state
}

void main()
{
    g_iState = STATE1;  // go to some initial state on startup
}

Pointer-to-function based state machine
Code:

typedef void (*State) (int iMessage);   // a typedef for a pointer-to-function

// Current state of the device is a pointer-to-function. 
// Note that there are no state flags.
State g_pfState;

void State1(int iMessage)  // Note that the name of the function is the same as the name of the state
{
    switch (iMessage)   // 1-layer of switch.  No nested switch.
    {
    case MSG1:
        // do something1
        break;
    case MSG2:
        // do something2
        g_pfState = &State2; // to transition to a different state we need to change the pointer
        break;
    // ... other message handlers
    }       
}

void State2(int iMessage)
{
    switch (iMessage)
    {
        // message handlers
    }
}

#int_rtcc
void isr_rtcc   // an example source of a message, that needs state-dependent handling
{
    g_pfState(MSG2); // Call a message handler through the pointer-to-function
}

void main()
{
    g_iState = &State1;  // go to some initial state on startup
}


Advantages of the pointer-to-function approach
There’s no nested switch, so the code is more readable, more modular and executes somewhat faster.

Disadvantages of the pointer-to-function approach
The flags are replaced by a pointer-to-function. The pointer-to-function doesn’t have a “physical” meaning, which makes it more difficult to understand what state the device is in.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group