2016-06-28 40 views
1

我想設置定時器輸入鍵和一個定時器來關閉背光,但它使用第一個定時器I set,我不能設置多個定時器和第二個定時器沒有工作。如何在verifone中設置多個定時器vx520

我用下面的

int timer1, timer2; 
long events; 
timer1 = set_timer(8000, EVT_TIMER); 
timer2 = set_timer(5000, EVT_TIMER); 
while(1){ 
events = wait_event(); 
if(events & EVT_KBD){ 
clr_timer(timer1); 
break; 
} 
else if (events & EVT_TIMER) 
{ 
printf("TIME_OUT"); 
break; 
} 

while(1){ 
events = wait_event(); 
if(events & EVT_KBD){ 
clr_timer(timer2); 
break; 
} 
else if (events & EVT_TIMER) 
{ 
printf("TIME_OUT2"); 
break; 
} 
} 

回答

1

的代碼需要,如果你想捕捉它們作爲不同的事件使用不同的事件掩碼(EVT_TIMER)。棘手的是你需要小心你使用哪個,因爲它可能會觸發其他操作。這些事件在svc.h中定義(請注意,掩碼是long,而long被定義爲32位,因此在使用所有標準事件後,您確實沒有任何東西可以離開)。

好消息是set_timer返回一個ID(這就是你的代碼中的timer1timer2)。然後,您可以使用SVC_TICKS API來確定哪個計時器已過期。我寫了一個名爲「timeRemains」的包裝來幫助我。

//First, define "timeRemains" 
char timeRemains(long* timer) 
{ 
    return SVC_TICKS(0, timer); 
} 

//Here's what your code may look like: 
if(!timeRemains(&timer1)) 
{ 
    //timer1 has expired. Do whatever you wanted to do when that happens. 
    //NOTE: you don't know the state of timer2--it may also have expired, 
    //  so you will need to deal with that 
} 

if(!timeRemains(&timer2)) 
{ 
    //timer2 has expired. Do whatever you wanted to do when that happens. 
    //NOTE: even though we are PRETTY sure that timer1 has not yet expired, 
    // you can't just forget about it. If you are going to exit this polling loop, 
    // be sure to clear it first. 
} 
0

另一種方法是,讓您的計時器在你自己的數據結構(比如由定時器到期時間排序排序列表),並只使用一個系統計時器的第一計時器到期(即先在排序列表)。

當您收到EVT_TIMER系統事件時,將觸發所有到期時間過去的定時器(從排序列表中刪除它們)。

如果列表中還有任何計時器,則爲新的第一個計時器啓動一個新的系統計時器以使其到期。

有(至少)兩件事情需要注意的:

  • 添加一個新的計時器,你必須檢查它不會成爲第一個計時器到期時

    。如果是這樣,您必須用clr_timer()取消現有的系統計時器,併爲新的第一個計時器設置一個新的系統計時器(新加入的計時器現在是排序列表中的第一個計時器)。添加一個新的計時器時,一個空列表,如果你使用read_ticks()調用來計算計時器到期時(如應該沒有系統計時器現在活動)

  • 跳過clr_timer()調用(或其他任何東西)確保以處理數值溢出回零(每49.7天發生一次)