【问题标题】:how to set more than one timer in verifone vx520如何在verifone vx520中设置多个定时器
【发布时间】:2016-10-30 16:52:37
【问题描述】:

我想设置一个用于输入按键的定时器和一个用于关闭背光的定时器,但它使用我设置的第一个定时器,我不能设置多个定时器,第二个定时器不起作用。

我使用下面的代码

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

【问题讨论】:

    标签: point-of-sale verifone


    【解决方案1】:

    如果要将它们捕获为不同的事件,则需要使用不同的事件掩码 (EVT_TIMER)。棘手的是您需要小心使用哪个,因为它可能会触发其他操作。这些事件在svc.h 中定义(注意掩码是longlong 被定义为 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.
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是将您的计时器保存在您自己的数据结构中(例如按计时器到期时间排序的排序列表),并仅使用一个系统计时器来让第一个计时器到期(即排序列表中的第一个)。

      当您收到EVT_TIMER 系统事件时,您会触发所有过期时间已过的计时器(将它们从排序列表中删除)。

      如果列表中还有任何计时器,则启动一个新的系统计时器以使新的第一个计时器到期。

      有(至少)两件事需要注意:

      • 添加新计时器时,您必须检查它是否不是第一个到期的计时器。如果是这样,您必须使用clr_timer() 取消现有系统计时器,并为新的第一个计时器(新添加的计时器现在在排序列表中的第一个)设置一个新的系统计时器。将新计时器添加到空列表时跳过clr_timer() 调用(因为现在应该没有系统计时器处于活动状态)

      • 如果您使用 read_ticks() 调用来计算计时器到期时间(或用于其他任何事情),请确保处理其值溢出回零的情况(每 49.7 天发生一次)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-17
        • 2022-06-13
        • 2015-08-09
        • 2016-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多