Differences between revisions 1 and 2
Revision 1 as of 2008-10-21 10:04:28
Size: 1040
Editor: dyn177231
Comment:
Revision 2 as of 2008-11-21 15:39:01
Size: 2392
Editor: JunHu
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
A good strategy in practice is that the callback functions for input events only store the input data and do nothing else. This will ensure timely release the Serial thread for other events. The response to the input events can take place in another thread, usually the draw() function. The draw function runs in a thread in parallel with the Serial thread hence it won't delay the Serial input event handling. A good strategy in practice is that the callback functions for input events only store the input data and do nothing else. This will ensure timely release the Serial thread for other events. The response to the input events can take place in another thread. This can be done the draw() function. The draw function runs in a thread in parallel with the Serial thread hence it won't delay the Serial input event handling.

A better way to do it is to use "execute()" function of AdMoVeo. For example in the inputAvailable:
{{{
#!java
void inputAvailable(Sensor sensor, int oldValue, int newValue){
    if(sensor == admoveo.getLeftDistanceSensor()){
     admoveo.execute("changeBlueLed", AdMoVeo.NOW);
    }
}

}}}
It asks admoveo to execute a task "changeBlueLed". The first parameter gives the name of the callback function in your Processing code that is to be called in a separated thread. The second parameter defines the priority, either AdMoVeo.NOW, or AdMoVeo.LATER. Tasks with the priority AdMoVeo.LATER will not be excuted until all the AdMoVeo.NOW tasks are executed.

Then in your processing code, you should define the function "changeBlueLed":
{{{
#!java
void changeBlueLed(SensorStatus s){
  bgcolor = s.get(admoveo.getLeftDistanceSensor())/4;
  admoveo.getBlueLed().setPower(bgcolor);
}
}}}

Such a function should always defined with a parameter with a type of "SensorStatus". This parameter carries a snapshot of the sensor values at the time the task was scheduled. You may "get" the sensor values from the SensorStatus with the sensor as the parameter. The sensor value should be usually greater than or equal to 0. If the value got is -1, it means at the moment of task being scheduled, the value has never changed yet.

Input Event Handling

In creapro processing library, the classes Arduino and AdMoVeo use callback functions (digitalAvailable(), analogAvaialbe() and inputAvailable()) for handling input events. One should keep in mind that the event handling takes place in the thread of Serial, which takes care of both input and output through the serial port. Once it enters the callback function, the thread is occupied until the callback function finishes. While the Serial thread is occupied by the call back function, all other input or output events will queue up -- this often results in delay in processing the input events.

A good strategy in practice is that the callback functions for input events only store the input data and do nothing else. This will ensure timely release the Serial thread for other events. The response to the input events can take place in another thread. This can be done the draw() function. The draw function runs in a thread in parallel with the Serial thread hence it won't delay the Serial input event handling.

A better way to do it is to use "execute()" function of AdMoVeo. For example in the inputAvailable:

   1 void inputAvailable(Sensor sensor, int oldValue, int newValue){
   2     if(sensor == admoveo.getLeftDistanceSensor()){
   3         admoveo.execute("changeBlueLed", AdMoVeo.NOW);
   4     }
   5 }

It asks admoveo to execute a task "changeBlueLed". The first parameter gives the name of the callback function in your Processing code that is to be called in a separated thread. The second parameter defines the priority, either AdMoVeo.NOW, or AdMoVeo.LATER. Tasks with the priority AdMoVeo.LATER will not be excuted until all the AdMoVeo.NOW tasks are executed.

Then in your processing code, you should define the function "changeBlueLed":

   1 void changeBlueLed(SensorStatus s){
   2   bgcolor = s.get(admoveo.getLeftDistanceSensor())/4;
   3   admoveo.getBlueLed().setPower(bgcolor);
   4 }

Such a function should always defined with a parameter with a type of "SensorStatus". This parameter carries a snapshot of the sensor values at the time the task was scheduled. You may "get" the sensor values from the SensorStatus with the sensor as the parameter. The sensor value should be usually greater than or equal to 0. If the value got is -1, it means at the moment of task being scheduled, the value has never changed yet.

CreaPro: InputEventHandling (last edited 2009-02-05 10:57:34 by JunHu)