User Tools

Site Tools


code:timers

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)):

  addTimer (10, new Rts2Event (EVENT_MY_TIMER, this));

Where 10 is time in seconds for timer activations and EVENT_MY_TIMER is defined as

#define EVENT_MY_TIMER     EVENT_LOCAL + 102

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:

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);
}

If you do not want to have event resheduled, remove addTimer call. To reshedule, you can also do:

    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;

which will spare a few CPU cycles.

code/timers.txt · Last modified: 2009/07/19 00:00 (external edit)