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 in 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 alternative 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 executed 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)