| An event is an action recognized by an Object. Event-driven applications execute programmer written code in response to an event. The ooPIC’s oEvent Object is the only Object that has the capability to execute code in response to an event action. If any instance of an event Object receives a signal to activate its event procedure, The ooPIC application invokes the subprocedure with then name of the oEvent Object instance plus the phrase "_CODE". Although the oEvent Object is the only Object in the ooPIC language that has event capabilities, other Objects can still indirectly trigger events. Other Objects can be linked to the oEvent Object thereby giving any Object event capabilities. When you want your code to respond to an event, you first create an instance of an oEvent Object with the Dim command, write code for that event and point the oEvent Object to that code, and lastly "connect" the oEvent Object to the Object that is triggering the event. The following example shows how a voltage change on one of the ooPIC’s I/O Lines can call an event using an oDio1 Object connected to an event Object to invoke the basic syntax code subprocedure "MYEVENT".
Visual-Basic Syntax | C and Java Syntax |
Dim MyEvent As New oEvent
Dim ThingA As New oDIO1
Dim Wire As New oWire
Sub Main()
ThingA.IOLine = 1
ThingA.Direction = cvInput
Wire.Input.Link(ThingA)
Wire.Output1.Link(MyEvent.Operate)
End Sub
Sub MyEvent_Code
'event code goes here'
End Sub | oEvent MyEvent = New oEvent;
oDIO1 ThingA = New oDIO1;
oFanOut Wire = New oWire;
Void Main(Void){
ThingA.IOLine = 1;
ThingA.Direction = cvInput;
Wire.Input.Link(ThingA);
Wire.Output1.Link(MyEvent.Operate);
}
Void MyEvent_Code(Void){
//event code goes here
} |
After executing these lines within Sub Main, the applications program flow is idle and you could add code that is free to go and do other things. A "Link" was made when the Input of the oWire Object was linked to the oDio1 Object and the Output of the oWire Object was linked to the Operate property of the oEvent Object. Now, if the voltage on the I/O line specified by the oDio1 Object’s property changes from 0 to +5 the State property of the Object named "ThingA" will change from 0 to 1, and thusly, The subprocedure named "MyEvent_Code" will be executed. When the End Sub statement is encountered at the end of the subprocedure’s code, the program will continue where it left off previously at the time of the event. Which, in this case, would be nothing because the main subprocedure has already ended.
Event-Driven vs. Traditional Programming
In a traditional or "procedural" application, the application itself rather than an event controls the portions of code that execute. Execution starts with the first line of the program and follows a defined path through the application, branching and calling subprocedures as needed. In comparison with the example above, a traditional application must continuously check the state of the I/O Line in order to determine when to call the subprocedure "MYEVENT_CODE". This takes up processing time for your application and can complicate the program’s flow control. In event-driven programs, changes in the hardware’s state can cause a subprocedure to be executed even when the program flow was not expecting to do so. Therefore, the order in which your code executes depends on which events occur, which in turn depends on what the hardware does. This is the essence of event-driven programming: your code responds when it needs to. Because the your program’s flow control does not dictate when an event will occur, your code must make a few assumptions about the "state of the world" when it executes. Your code may trigger additional events in response to its own interaction with the hardware and so, you should try to structure you application so those possibilities are anticipated. Note: Avoid performing too many events at once. This could cause the ooPIC program stack to overflow.
Code that starts at power up.
In ooPIC Basic Syntax, the subprocedure named "MAIN" in your program is designated as the starting point. When your application powers up or is reset, the program will start executing at the first line within this subprocedure. If you want to stop the program and wait for an event to occur, simply drop out of the "MAIN" subprocedure. Another way to stop the program is by setting the ooPIC Object’s Operate property to 0 (cvOff). This will also cause the ooPIC to shut down all events and go into a low-power mode. Prior to the "MAIN" subprocedure executing, When the ooPIC is powered up or is reset, all of the application's Object will be created and their values initialized allowing the "MAIN" subprocedure to access them.
Ending an Event-Driven program.
An Event-Driven ooPIC application stops running when all its oEvent Objects are disconnected from their triggers and no code is executing. Because no code is running and no events will occur that will run a subprocedure, there is no way to re-start the execution of the program code. Even when no code is running, the Objects will still operate in the background until the power is shut off. Since the Operate property can be set to 0 by another Object, or even by another computer via the I2C network, the code does not need to be running to shut down the power. |