// ============================================================================ //
// T i m _ t s t . c p p //
// ============================================================================ //
#include "TIMcalls.h"
#include <stdio.h>// for printf
static bool pM = true; long timer_id[]= {0,0,0,0};
// ============================================================================ //
// 'cbr' is a 13sec repeating function, called by the 'ticker' thread //
// ============================================================================ //
static void cbr (void*prm) {printf("\nFunction cbr fires, ictr %5d, prm %d",TIM_ictr(),(long)prm);}
// ============================================================================ //
// 'one' is a one-shot function, called by the 'ticker' thread, which //
// calls itself on 5sec boundaries while TIM_ictr < 30 seconds //
// ============================================================================ //
static void one (void*prm)
{
printf("\nFunction one fires, ictr %5d, prm %d",TIM_ictr(),(long)prm);
if (TIM_ictr() < 30000)
{
TIM_make_tmrF(5000-(TIM_ictr()%5000),&timer_id[2],one,(void*)(TIM_ictr()/1000),false);
if (timer_id[0] > 0 && TIM_mod_tmr(timer_id[0],20000-TIM_ictr()) != TIM_SUCCESS)
timer_id[0] = 0;//make_tmr has expired, clear ID
printf("\nIDs %d,%d,%d,%d",timer_id[0],timer_id[1],timer_id[2],timer_id[3]);
}
}
// ============================================================================ //
// 'thr' is an 18sec repeating thread, spun by 'ticker-ticker' thread //
// ============================================================================ //
static void thr (void*prm) {printf("\nThread thr fires, ictr %5d, prm %d",TIM_ictr(),(long)prm);}
// ============================================================================ //
// ============================================================================ //
void main (int argc, char * argv[])
{
HANDLE flg_han;
unsigned long sts;
TIM_make_tmr (20000,&timer_id[0],"",false);
TIM_make_tmrF(13000,&timer_id[1],cbr,(void*)123,true);
TIM_make_tmrF(11000,&timer_id[2],one,(void*)456,false);
TIM_make_tmrT(18000,&timer_id[3],thr,(void*)789,true);
flg_han = TIM_get_handle(timer_id[0]);
printf("\nIDs %d,%d,%d,%d",timer_id[0],timer_id[1],timer_id[2],timer_id[3]);
while(pM)
{
Sleep(1000-(TIM_ictr()%1000));// next second boundary
sts = WaitForSingleObject(flg_han,0);// check for flag set
if (sts != WAIT_OBJECT_0)
printf("\nFlag not yet set, ictr %5d",TIM_ictr());
else{printf("\nFlag set, ictr %5d",TIM_ictr()); pM=false;}
}
Sleep(57000-TIM_ictr());// 57second boundary
printf("\nAbout to reset TIM sub-system");
long ictr = TIM_ictr();
TIM_reset();
printf("\n ...TIM_reset, about to exit");
Sleep(80000-ictr);// 80second boundary (i.e.23secs) to read the screen
}