Timer controlled process, influenced by "simultaneous" (decoupled)keyboard keypresses

Discussion in 'C Programming' started by John Reye, Nov 5, 2012.

  1. John Reye

    John Reye Guest

    Hello,

    how can one create a program, that runs some "timer controlled
    process", which is influenced by keypresses?

    Example:
    ============
    The program reads a file like:
    1.5 car
    3.4 kitchen
    10.7 fish

    and then accordingly the program will :
    * prints "car", waits for 1.5 seconds
    * prints "kitchen", waits for 3.4 seconds
    etc.

    And while this is running, I want the user to be able to skip forward
    or backwards by hitting the keyboard keys
    * hit 'n' for next -- immediately print the next word
    * hit 'p' for previous -- print the previous word (or the same word
    again, if we're already on the first word)

    ===============

    My questions:
    1) can this be done is standard c, or will the code be platform-
    specific? (I'm running Linux)
    2) does one need threads, in order to handle the keypress-detection
    running in parallel with the "timer controlled process", or can it be
    done without threads?
    3) Can one harness some interrupt mechanism (or related mechanism) to
    solve this? E.g. can the keypresses be detected as interrupts in a
    userland linux program?

    Thanks for hints and your help.
    Pointers to related code-snippets are welcome. :)

    J.
    John Reye, Nov 5, 2012
    #1
    1. Advertising

  2. John Reye

    John Reye Guest

    Re: Timer controlled process, influenced by "simultaneous"(decoupled) keyboard keypresses

    I just realized that I'm basically asking something like "how does one
    realize tetris with keyboard control in C".
    Right?
    John Reye, Nov 5, 2012
    #2
    1. Advertising

  3. Re: Timer controlled process, influenced by "simultaneous"(decoupled) keyboard keypresses

    On Monday, 5 November 2012 17:06:30 UTC-5, John Reye wrote:
    > Hello,
    >
    >
    >
    > how can one create a program, that runs some "timer controlled
    >
    > process", which is influenced by keypresses?
    >
    >
    >
    > Example:
    >
    > ============
    >
    > The program reads a file like:
    >
    > 1.5 car
    >
    > 3.4 kitchen
    >
    > 10.7 fish
    >
    >
    >
    > and then accordingly the program will :
    >
    > * prints "car", waits for 1.5 seconds
    >
    > * prints "kitchen", waits for 3.4 seconds
    >
    > etc.
    >
    >
    >
    > And while this is running, I want the user to be able to skip forward
    >
    > or backwards by hitting the keyboard keys
    >
    > * hit 'n' for next -- immediately print the next word
    >
    > * hit 'p' for previous -- print the previous word (or the same word
    >
    > again, if we're already on the first word)
    >
    >
    >
    > ===============
    >
    >
    >
    > My questions:
    >
    > 1) can this be done is standard c, or will the code be platform-
    >
    > specific? (I'm running Linux)
    >
    > 2) does one need threads, in order to handle the keypress-detection
    >
    > running in parallel with the "timer controlled process", or can it be
    >
    > done without threads?
    >
    > 3) Can one harness some interrupt mechanism (or related mechanism) to
    >
    > solve this? E.g. can the keypresses be detected as interrupts in a
    >
    > userland linux program?
    >
    >
    >
    > Thanks for hints and your help.
    >
    > Pointers to related code-snippets are welcome. :)
    >
    >
    >
    > J.


    The timer part, can be done using ansi-c.
    look at difftime(3) function:
    http://linux.die.net/man/3/difftime

    The keypress part would require platform dependent
    library.
    On linux, you could do it
    using ncurses library:
    http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/intro.html#WHATIS
    Prathamesh Kulkarni, Nov 5, 2012
    #3
  4. John Reye

    Eric Sosman Guest

    On 11/5/2012 5:06 PM, John Reye wrote:
    > Hello,
    >
    > how can one create a program, that runs some "timer controlled
    > process", which is influenced by keypresses?
    >
    > Example:
    > ============
    > The program reads a file like:
    > 1.5 car
    > 3.4 kitchen
    > 10.7 fish
    >
    > and then accordingly the program will :
    > * prints "car", waits for 1.5 seconds
    > * prints "kitchen", waits for 3.4 seconds
    > etc.
    >
    > And while this is running, I want the user to be able to skip forward
    > or backwards by hitting the keyboard keys
    > * hit 'n' for next -- immediately print the next word
    > * hit 'p' for previous -- print the previous word (or the same word
    > again, if we're already on the first word)
    >
    > ===============
    >
    > My questions:
    > 1) can this be done is standard c, or will the code be platform-
    > specific? (I'm running Linux)


    I don't think it can be done at all in unaided C99 or before.
    The I/O functions are all "blocking," in the sense that they don't
    return until their operation finishes or fails. If you've called
    a function to read a key and the next deadline arrives, too bad:
    The function will just sit there waiting for a key, deadline be
    damned. Meanwhile, the only Standard delay mechanism is to busy-
    wait until a call to time() indicates that the deadline has arrived.

    You might be able to do it in C11 with a pair of threads: One
    would read keystrokes and add them to a queue, while the other
    would drain the queue and act on the keystrokes. The magical bit
    is that the consumer could use cnd_timedwait() instead of plain
    cnd_wait(), with a timeout that awakens at the next deadline time
    even if no keystrokes have appeared on the queue. (Even with C11
    you're out of luck if __STDC_NO_THREADS__ is defined.)

    In any version of C, you'll probably need platform-specific
    magic to read an isolated keystroke without needing ENTER or the
    equivalent. See the FAQ.

    Also, the resolution of time() and of C11's struct timespec
    are unspecified. In most implementations time() has a resolution
    of one second, but as far as the Standard is concerned it might
    have a resolution of forty fortnights. C11's struct timespec
    can *express* a time in nanoseconds, but again: The resolution
    actually achieved by the functions that use it is unspecified.

    > 2) does one need threads, in order to handle the keypress-detection
    > running in parallel with the "timer controlled process", or can it be
    > done without threads?


    I can't think of a way to do it in (semi-)Standard C without
    using threads. If you're willing to use POSIX facilities, you can
    do it without threads. See comp.unix.programmer for advice.

    > 3) Can one harness some interrupt mechanism (or related mechanism) to
    > solve this? E.g. can the keypresses be detected as interrupts in a
    > userland linux program?


    Not in Standard C. In POSIX, yes -- but there are easier ways.

    --
    Eric Sosman
    d
    Eric Sosman, Nov 5, 2012
    #4
  5. Re: Timer controlled process, influenced by "simultaneous"(decoupled) keyboard keypresses

    This is a stripped down version
    of your program that only handles
    the timer part:
    http://pastebin.com/1ZRtnT2i

    I have taken a few liberties:
    1] I assume that the text that follows
    the time does not contain whitespaces
    and has a maximum of length 100
    look at:
    fscanf(fp, "%lf%100s", &wait_time, buf);


    2] Used error() function present
    in glibc, but not in ansi c.
    Prathamesh Kulkarni, Nov 5, 2012
    #5
  6. Re: Timer controlled process, influenced by "simultaneous"(decoupled) keyboard keypresses

    On Monday, 5 November 2012 18:04:39 UTC-5, Prathamesh Kulkarni wrote:
    > This is a stripped down version
    >
    > of your program that only handles
    >
    > the timer part:
    >
    > http://pastebin.com/1ZRtnT2i
    >
    >
    >
    > I have taken a few liberties:
    >
    > 1] I assume that the text that follows
    >
    > the time does not contain whitespaces
    >
    > and has a maximum of length 100
    >
    > look at:
    >
    > fscanf(fp, "%lf%100s", &wait_time, buf);
    >
    >
    >
    >
    >
    > 2] Used error() function present
    >
    > in glibc, but not in ansi c.


    I have slightly modified the code:
    (added ferror() at end)
    http://pastebin.com/dcpJ2fv5
    Prathamesh Kulkarni, Nov 5, 2012
    #6
  7. John Reye

    Adam Wysocki Guest

    Re: Timer controlled process, influenced by "simultaneous" (decoupled) keyboard keypresses

    John Reye <> wrote:

    > 1) can this be done is standard c, or will the code be platform-
    > specific? (I'm running Linux)
    > 2) does one need threads, in order to handle the keypress-detection
    > running in parallel with the "timer controlled process", or can it be
    > done without threads?
    > 3) Can one harness some interrupt mechanism (or related mechanism) to
    > solve this? E.g. can the keypresses be detected as interrupts in a
    > userland linux program?
    >
    > Thanks for hints and your help.


    Two ways came to my mind.

    1. What latency do you accept? Maybe you can slice timeslots to slots of
    maximum 50ms (example) and handle keys in between?

    2. You can use select() to poll on fd 0 with a specified timeout. It is
    platform-specific, but it will work.

    In both ways you have to configure the terminal input (directly descriptor,
    without using stdio), so it does not buffer the input. You can use stty raw
    (and then stty cooked) or appropriate tcsetattr (disabling ICANON flag).

    See this link:

    http://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar

    --
    Gof
    http://www.chmurka.net/
    Adam Wysocki, Nov 7, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scaramanga
    Replies:
    6
    Views:
    382
    Roedy Green
    Dec 5, 2005
  2. begin_ner
    Replies:
    0
    Views:
    448
    begin_ner
    Jan 3, 2008
  3. shocks
    Replies:
    6
    Views:
    390
    mdipierro
    Dec 9, 2009
  4. Alec Taylor
    Replies:
    0
    Views:
    150
    Alec Taylor
    Jun 3, 2012
  5. Alec Taylor
    Replies:
    0
    Views:
    192
    Alec Taylor
    Dec 7, 2012
Loading...

Share This Page