An event shared element is used to indicate that a primitive module can accept asynchronous events. Timer modules use the Event to specify with which other timer modules they interact.
Events provide links to primitives or modules which manage timing events, such as StartTimer, ServiceTimer and CancelTimer. Events can be used only in timed domains, such as DE domain and in FSM modules.
To add an event to the model use the tool button Add Event, the menu item Edit → Add Event or the hot key E. The following elements can be defined for an event:
The scope of an event can be External or Internal. An Event cannot be External in a system. Primitives which have an event element (either internal or external) have an event entry point associated with the event. When an event occurs, the code associated with this event is executed. If you want specific code to run when an event occurs, then you have to put the code in the defevent item in the primitive source code. A defevent item is generated for every event element defined in a primitive:
defevent { name { EventName } scope { External } type { Root } desc { "Event description goes here" } code { /* user code goes here */ } }
The type of the event is a data structure (for more information about data structures see Data Structure Management). You can link an event from a primitive to an event from a module or system only if the type of the first event is the same as (or a base type for) the type of the second event. For an external event you can set the default value, and for an internal event you can set the initial value. The following methods can be used to access the data associated with an event:
Type* readData () const; void writeData (Type* pValue); void writeData (int pValue); void writeData (float pValue);
The events can be used to send data between primitives. In this case you have to set the data using one of the writeData(...) methods before schedule the event. To schedule an event the emitEvent method should be called.
void emitEvent (double pTime, int pEventID);
The arguments represent the time at which the event will occur and the event ID, respectively. The event ID is used to identify the event, for example you can cancel an event with a specific ID using the cancel method.
void cancel (int pEventID);
This removes all entries with pEventID from the queue of events. The queue of events contains the scheduled times at which various events occur and the location in the block diagram where the execution resumes once the event occurs. Entries in the queue of events are stored in ascending order by event time, so the first entry in the queue is the next event to occur. Whenever an event is to be processed, the simulation clock is advanced to the time of occurrence of the event. Using the getResidualTime method you get the difference between the time at which a event should occur and the current time of the simulation. If there is no entry in the queue of events with the given ID, the method returns -1.0.
double getResidualTime (int pEventID);