User Tools

Site Tools


code:timers

Differences

This shows you the differences between two versions of the page.


Previous revision
code:timers [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 +====== Starting timer ======
  
 +To start a timer, call from Rts2Block or its descendant (all device and service drivers, monitor - usually the main beast in RTS2 programme, which run method is called from C //main (int argc, char**argv)//):
 +<code c++>
 +  addTimer (10, new Rts2Event (EVENT_MY_TIMER, this));
 +</code>
 +
 +Where 10 is time in seconds for timer activations and EVENT_MY_TIMER is defined as
 +
 +<code c++>
 +#define EVENT_MY_TIMER     EVENT_LOCAL + 102
 +</code>
 +
 +Of course try to keep numbers unique in the program, and do use EVENT_LOCAL prefix to declare them.
 +
 +After timer time expires, //Rts2Object// specified as second argument to //Rts2Event// method postEvent will be called, with pointer to //Rts2Event// being send. To catch it, add (//virtual public//) method postEvent, which body is bellow:
 +
 +<code c++>
 +void MyRts2ObjectDescendant::postEvent (Rts2Event *event)
 +{
 +  switch (event->getType ())
 +  {
 +    case EVENT_MY_TIMER:
 +      // do the work - even better, call method to do the work
 +      ...
 +      // do not forget to requeu us (if needed), so we will be called again
 +      addTimer (10, new Rts2Event (EVENT_MY_TIMER, this));
 +      break;
 +  }
 +  // do not forget to call parent postEvent, which will delete event
 +  // replace Parent with class from which you inherit - Rts2Device, Rts2Sensor,..
 +  Parent::postEvent (event);
 +}
 +</code>
 +
 +If you do not want to have event resheduled, remove addTimer call. To reshedule, you can also do:
 +
 +<code c++>
 +    case EVENT_MY_TIMER:
 +      // do the work - even better, call method to do the work
 +      ...
 +      // do not forget to requeu us (if needed), so we will be called again
 +      addTimer (10, event);
 +      // do not delete event pointer
 +      return;
 +</code>
 +
 +which will spare a few CPU cycles.
code/timers.txt · Last modified: 2009/07/19 00:00 (external edit)