Integrations (PTC products, 3rd party products and code) > Code integration (Ada, ARINC 653, C, C#, C++, IDL, Java, SQL and VB) > C Code > Generating C Code > Overview of generating C code (C code) > Overview of modeling C code (C code) > Overview of modeling C code (C code) > State diagram mappings - introduction (C code)
  
State diagram mappings - introduction (C code)
When you choose to generate code for State Diagrams, ACS generates attributes, typedefs and operations in the code to implement the State Diagrams owned by Classes (through State Machines) that are being generated.
When ACS generates an attribute, typedef or operation in a code file, that attribute, typedef or operation is not associated with an Attribute, Typedef or Operation item in the model. To help you identify these code elements in the code files, ACS prefixes code elements generated for State Diagrams with 'Rts', except for the constructor and destructor operations.
This topic uses an example to provide an overview of the Attributes, Typedefs and Operations that are generated from a simple State Diagram.
For entering the state machine
ACS generates a constructor operation to initialize the Class.
void cBarCodeReader(struct cBarCodeReader* this)
{
this->m_SimFuncs = &classSpecificSimFuncs;
classSpecificSimFuncs.RtsGetAttributeValue = &RtsGetAttributeValue;
classSpecificSimFuncs.RtsSetAttributeValue = &RtsSetAttributeValue;
classSpecificSimFuncs.RtsInjectEvent = &RtsInjectEvent;
classSpecificSimFuncs.RtsTimerSet = &TimerSet;
RtsNotifySynchronous(CREATE,"3b13e89f-b99b-42d1-9770-f368f078131a",0, NULL);
RtsCreateSemaphore(this->RtsBusy);
RtsLock(this->RtsBusy);
/* Initializing State Vectors to NotIn */
this->RtsCurrent_cBarCodeReader=RtscBarCodeReader_States_NotIn_cBarCodeReader;
while(RtsRunToCompletion(this));
RtsUnlock(this->RtsBusy);
}
For setting and reading the current state of the state machine:
ACS generates a current state attribute to store the current state of the Class.
enum RtscBarCodeReader_States
The preceding attribute can be set to Idle, Reading or NotIn_sdBCReader. NotIn_sdBCReader indicates that the state machine is not active.
enum RtscBarCodeReader_States
{
RtscBarCodeReader_States_Idle,
RtscBarCodeReader_States_Reading,
RtscBarCodeReader_States_NotIn_cBarCodeReader
};
For entering each state
ACS generates an enter state operation to initialize each State (Idle and Reading) on entry.
void RtsEnter_Idle(struct cBarCodeReader* this)
{
/* Simulation support : Notification function call for entering state : Idle */
RtsNotify(ENTER,"682cee54-01d5-46e9-b5d1-7bf579f3cf5b",0,NULL);
this->RtsCurrent_sdBCReader = RtscBarCodeReader_States_Idle;
}
void RtsEnter_Reading(struct cBarCodeReader* this)
{
/* Simulation support : Notification function call for entering state : Reading */
RtsNotify(ENTER,"380d33eb-71c5-4ee8-816e-d2895a5f35db",0,NULL);
this->RtsCurrent_sdBCReader = RtscBarCodeReader_States_Reading;
}
For exiting from each state
ACS generates an exit state operation to run code on exit from each State (Idle and Reading).
void RtsExit_Idle(struct cBarCodeReader* this)
{
if (this->RtsCurrent_sdBCReader == RtscBarCodeReader_States_Idle)
{
/* Simulation support : Notification function call for exiting state : Idle */
RtsNotify(EXIT,"682cee54-01d5-46e9-b5d1-7bf579f3cf5b",0,NULL);
}
}
void RtsExit_Reading(struct cBarCodeReader* this)
{
if (this->RtsCurrent_sdBCReader == RtscBarCodeReader_States_Reading)
{
/* Simulation support : Notification function call for exiting state : Reading */
RtsNotify(EXIT,"380d33eb-71c5-4ee8-816e-d2895a5f35db",0,NULL);
}
}
For handling each event
ACS generates a signal event handler operation to handle the event.
void RtscBarCodeReader BarCodeDetected(struct cBarCodeReader* this)
{
RtsLock(this->RtsBusy);
RtscBarCodeReaderBarCodeDetected_In_cBarCodeReader(this);
while(RtsRunToCompletion(this));
RtsUnlock(this->RtsBusy);
}
For ensuring that calls are not executed when the state machine is busy
ACS generates a busy attribute to lock the state machine and each state when it is busy.
Rts RtsBusy
For handling transitions that do not require further triggers
ACS generates a run to completion operation to handle all transitions that do not require further triggers.
BOOL RtsRunToCompletion(struct cBarCodeReader* this)
{
if (this->RtsCurrent_cBarCodeReader == RtscBarCodeReader_States_NotIn_cBarCodeReader)
{
/* Simulation support : Notification function calls for Entry and Exit of Initial state */
RtsNotify(ENTER,"48a4501a-6421-41fd-b4f1-f12eede32e8a",0,NULL);
RtsNotify(EXIT,"48a4501a-6421-41fd-b4f1-f12eede32e8a",0,NULL);
/* Simulation support : Notification function calls for transition */
RtsNotify(TRANSITION,"9f1afa70-ba8b-41ab-8246-ef7881a646b1",0,NULL);
RtsEnter_Idle(this);
return TRUE;
}
if (this->RtsCurrent_cBarCodeReader == RtscBarCodeReader_States_Reading)
{
RtsExit_Reading(this);
/* Simulation support : Notification function calls for transition */
RtsNotify(TRANSITION,"f21162fb-cce3-4a12-a9d2-3b37c07d4be5",0,NULL);
this->RtsCurrent_cBarCodeReader = RtscBarCodeReader_States_NotIn_cBarCodeReader;
/* Simulation support : Notification function call for entering final state */
RtsNotify(ENTER,"a4a2ff52-45dc-45da-baa7-cc78c62642d9",0,NULL);
return FALSE;
}
return FALSE;
}
For exiting the state machine
A Destructor Operation to destroy the Class.
void _cBarCodeReader(struct cBarCodeReader* this)
{
RtsDeleteSemaphore(this->RtsBusy);
}
For simulation purposes only
ACS generates the following operations for simulation purposes only - RtsGetAttributeValue, RtsSetAttributeValue and RtsInjectEvent.
ACS can create additional code elements that are not included in the preceding example.
ACS generates attributes for the following purposes:
Storing the current state of each Sequential State and Concurrent State (current state attribute).
Storing the last current state of a Sequential State or Concurrent State (history state attribute).
Storing the handle for a timer (timer attribute).
ACS generates operations for the following purposes:
Determining which State a Sequential State or Concurrent State starts in (default entry operation).
Setting the default entry state of each Sequential State and Concurrent State (history state operation).
Reading the current state of each Sequential State and Concurrent State (current state operation).
Call events (Call Event Operation) and Time events (timer event handler operation).
Setting the current State of a State to that recorded in a History State.