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, | ||
+ | </ | ||
+ | |||
+ | Where 10 is time in seconds for timer activations and EVENT_MY_TIMER is defined as | ||
+ | |||
+ | <code c++> | ||
+ | #define EVENT_MY_TIMER | ||
+ | </ | ||
+ | |||
+ | Of course try to keep numbers unique in the program, and do use EVENT_LOCAL prefix to declare them. | ||
+ | |||
+ | After timer time expires, // | ||
+ | |||
+ | <code c++> | ||
+ | void MyRts2ObjectDescendant:: | ||
+ | { | ||
+ | switch (event-> | ||
+ | { | ||
+ | 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, | ||
+ | break; | ||
+ | } | ||
+ | // do not forget to call parent postEvent, which will delete event | ||
+ | // replace Parent with class from which you inherit - Rts2Device, Rts2Sensor, | ||
+ | Parent:: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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; | ||
+ | </ | ||
+ | |||
+ | which will spare a few CPU cycles. |
code/timers.txt · Last modified: 2009/07/19 00:00 (external edit)