Prob with delay timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lynology

    Prob with delay timer

    My program takes in a key pressed value from the main routine and
    based on the key pressed, it selects the command to be executed. The
    problem I have is in creating a delay timer so that a message appears
    on my screen for only one second. The condensed code is as follows:

    #include <sys\timeb.h>
    #define DELAY_1SEC 1000 // in millisec

    void Scan_Menu_Keys( int key_press)
    { switch(key_pres s){
    case 1: Execute1stComma nd(); break;
    case 2: Start_Timer();
    while (!Timer_Expired (DELAY_1SEC))
    strcpy(CG_Scree nKeyboard.Scree n, "Display Message");
    Clear_Screen();
    break;
    default: break;
    }
    }

    void Start_Timer(){
    ftime(&start_ti me); // ftime is function defined in sys\timeb
    }

    int Timer_Expired() {
    ftime(&current_ time);
    time_diff = (int) (1000.0*(curren t_time.time - start_time.time ) +
    (current_time.m illitm - start_time.mill itm));
    return (time_diff >= DELAY_1SEC);
    }

    The problem I ran into was that the timer would wait for one second
    before displaying the message "Display Message". Effectively, that
    meant the message never displayed because when it got out of the 1
    second delay loop, the screen was cleared. I would love to get
    suggestions on why this delay loop is not be executing the command
    within the loop and any alternative methods to achieving the same
    results.

    Thanks in advance!

    LYN
  • John Harrison

    #2
    Re: Prob with delay timer


    "lynology" <yaoneng.lee@lm co.com> wrote in message
    news:1b517d6b.0 408020444.4310b 15e@posting.goo gle.com...[color=blue]
    > My program takes in a key pressed value from the main routine and
    > based on the key pressed, it selects the command to be executed. The
    > problem I have is in creating a delay timer so that a message appears
    > on my screen for only one second. The condensed code is as follows:
    >
    > #include <sys\timeb.h>
    > #define DELAY_1SEC 1000 // in millisec
    >
    > void Scan_Menu_Keys( int key_press)
    > { switch(key_pres s){
    > case 1: Execute1stComma nd(); break;
    > case 2: Start_Timer();
    > while (!Timer_Expired (DELAY_1SEC))
    > strcpy(CG_Scree nKeyboard.Scree n, "Display Message");
    > Clear_Screen();
    > break;
    > default: break;
    > }
    > }
    >
    > void Start_Timer(){
    > ftime(&start_ti me); // ftime is function defined in sys\timeb
    > }
    >
    > int Timer_Expired() {
    > ftime(&current_ time);
    > time_diff = (int) (1000.0*(curren t_time.time - start_time.time ) +
    > (current_time.m illitm - start_time.mill itm));
    > return (time_diff >= DELAY_1SEC);
    > }[/color]

    Think about the above function. What happens if (say)

    current_time.ti me = 1000000
    current_time.mi llitm = 10

    start_time.time = 999999
    start_time.mill itm = 100

    I can't answer that question because you are using non-standard C++ (ftime),
    but your method of working out the difference between two times may be
    incorrect (I think it depends on whether millitm is a signed or unsigned).

    john


    Comment

    • Ralph D. Ungermann

      #3
      Re: Prob with delay timer

      lynology wrote:[color=blue]
      > The
      > problem I have is in creating a delay timer so that a message appears
      > on my screen for only one second.[/color]

      [color=blue]
      > case 2: Start_Timer();
      > while (!Timer_Expired (DELAY_1SEC))
      > strcpy(CG_Scree nKeyboard.Scree n, "Display Message");
      > Clear_Screen();[/color]

      Timer_Expired() is called with one argument. Whether this compiles,
      depends on a previous declaration (which you `condensed out'). But
      surely, it won't link with your definition below.
      [color=blue]
      > I would love to get
      > suggestions on why this delay loop is not be executing the command
      > within the loop[/color]

      Presumimng, that the while-condition is true for exactly one second,
      there will be many, many strcpy() executions in this time (depending on
      your CPU speed). But I can't see a single _output_ statement. Is there
      some hidden magic in writing to CG_ScreenKeyboa rd.Screen?
      [color=blue]
      > and any alternative methods to achieving the same
      > results.[/color]

      Please consult the documentation of CG_ScreenKeyboa rd. This is not a
      standard C++ class, so I'm afraid, that nobody here can help you.


      Ralph

      Comment

      Working...