ステートマシン図からC言語のソースコードを出力するサンプル:ソースコード

ヘッダファイル

/****************************************************
 *  sample.h
 *  Created on: 2013/02/25 16:12:33
 *  Implementation of the Class sample
 *  Original author: kouno
 ****************************************************/

#if !defined(EA_27F21136_1C9C_47c4_9F1B_9EAFAC8AFD45__INCLUDED_)
#define EA_27F21136_1C9C_47c4_9F1B_9EAFAC8AFD45__INCLUDED_


#ifdef	__cplusplus
extern "C" {
#endif



/* Begin - EA generated code for StateMachine */
enum Event{
	E_NOEVENT = 0,
	E_right,
	E_left,
	E_light_on,
	E_light_off
};

void initializeStateMachine();
void performEvent(enum Event e);

/* End - EA generated code for StateMachine */


void InitializeAction();
void TurnLeftActions();
void TurnRightAction();

#ifdef	__cplusplus
}
#endif


#endif /*!defined(EA_27F21136_1C9C_47c4_9F1B_9EAFAC8AFD45__INCLUDED_)*/
 

ソースファイル

/****************************************************
 *  sample.c
 *  Created on: 2013/02/25 16:12:33
 *  Implementation of the Class sample
 *  Original author: kouno
 ****************************************************/

#include 
#include "sample.h"

int ableTurnRight = 1;
int initialLeft;



void InitializeAction()
{
	/*implementation of Initialize*/

}

void TurnLeftActions()
{
	/*implementation of Turn Left*/
}

void TurnRightAction()
{
	/*implementation of Turn Right*/
}


/* Begin - EA generated code for StateMachine */

enum StateType
{
	ST_NOSTATE = 0,
	ST_sampleStateMachine_turning_left,
	ST_sampleStateMachine_turning_left_OFF,
	ST_sampleStateMachine_turning_left_ON,
	ST_sampleStateMachine_initialize,
	ST_sampleStateMachine_turning_right
};

#define BOOL int
#define TRUE 1
#define FALSE 0

enum StateType currState;
enum StateType nextState;
BOOL externalTransition;
BOOL transcend;


enum CommandType
{
	Do,
	Entry,
	Exit
};


void sampleStateMachine_turning_left(enum CommandType command, enum Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			TurnLeftActions();
			// State's Transitions
			if (e == E_right && (ableTurnRight == 1))
			{
				nextState = ST_sampleStateMachine_turning_right;
			}
			else
			if (e == E_left)
			{
				nextState = ST_sampleStateMachine_turning_left_OFF;
				transcend = TRUE;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleStateMachine_turning_left_OFF(enum CommandType command, enum Event e)
{
	if (transcend == TRUE || command == Do)
	{
		sampleStateMachine_turning_left(command, e);
		if (currState != nextState)
		{
			transcend = TRUE;
		}
	}

	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_light_on)
			{
				nextState = ST_sampleStateMachine_turning_left_ON;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleStateMachine_turning_left_ON(enum CommandType command, enum Event e)
{
	if (transcend == TRUE || command == Do)
	{
		sampleStateMachine_turning_left(command, e);
		if (currState != nextState)
		{
			transcend = TRUE;
		}
	}

	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_light_off)
			{
				nextState = ST_sampleStateMachine_turning_left_OFF;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleStateMachine_initialize(enum CommandType command, enum Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_right && (ableTurnRight == 1 && initialLeft == 0))
			{
				nextState = ST_sampleStateMachine_turning_right;
			}
			else
			if (e == E_left)
			{
				nextState = ST_sampleStateMachine_turning_left_OFF;
				transcend = TRUE;
			}

			break;
		}
		case Entry:
		{
			//Entry Behaviors..
			InitializeAction();
			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleStateMachine_turning_right(enum CommandType command, enum Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			TurnRightAction();
			// State's Transitions
			if (e == E_right && (ableTurnRight == 1))
			{
				nextState = ST_sampleStateMachine_turning_right;
				externalTransition = TRUE;
			}
			else
			if (e == E_left)
			{
				nextState = ST_sampleStateMachine_turning_left_OFF;
				transcend = TRUE;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void StatesProc(enum StateType currState, enum CommandType command, enum Event e)
{
	switch(currState)
	{
		case ST_sampleStateMachine_turning_left:
		{
			sampleStateMachine_turning_left(command, e);
			break;
		}

		case ST_sampleStateMachine_turning_left_OFF:
		{
			sampleStateMachine_turning_left_OFF(command, e);
			break;
		}

		case ST_sampleStateMachine_turning_left_ON:
		{
			sampleStateMachine_turning_left_ON(command, e);
			break;
		}

		case ST_sampleStateMachine_initialize:
		{
			sampleStateMachine_initialize(command, e);
			break;
		}

		case ST_sampleStateMachine_turning_right:
		{
			sampleStateMachine_turning_right(command, e);
			break;
		}
		default:
			break;
	}
}

void initializeStateMachine()
{
	currState = ST_sampleStateMachine_initialize;
	nextState = ST_NOSTATE;
	StatesProc(currState, Entry, E_NOEVENT);
}

void performEvent(enum Event e)
{
	if ( currState == ST_NOSTATE )
	{
		return;
	}

	externalTransition = FALSE;
	transcend = FALSE;

	nextState = currState;
	StatesProc(currState, Do, e);

	if ( nextState == ST_NOSTATE )
	{
		StatesProc(currState, Exit, e);
		currState = ST_NOSTATE;
		return;
	}

	if ( currState != nextState || externalTransition == TRUE)
	{
		StatesProc(currState, Exit, e);
		currState = nextState;
		StatesProc(nextState, Entry, e);
	}
}
/* End - EA generated code for StateMachine */