Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Timer controlled process, influenced by "simultaneous" (decoupled)keyboard keypresses

Reply
Thread Tools

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

 
 
John Reye
Guest
Posts: n/a
 
      11-05-2012
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.
 
Reply With Quote
 
 
 
 
John Reye
Guest
Posts: n/a
 
      11-05-2012
I just realized that I'm basically asking something like "how does one
realize tetris with keyboard control in C".
Right?
 
Reply With Quote
 
 
 
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      11-05-2012
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-Progra...ro.html#WHATIS



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-05-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      11-05-2012
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.


 
Reply With Quote
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      11-05-2012
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
 
Reply With Quote
 
Adam Wysocki
Guest
Posts: n/a
 
      11-07-2012
John Reye <(E-Mail Removed)> 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/1...th-any-getchar

--
Gof
http://www.chmurka.net/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
keypresses and signal handling Rich C Programming 29 09-27-2010 10:31 AM
intercepting keypresses, mouse movements, joystick movements. bryan rasmussen Python 2 10-09-2006 07:11 PM
How do I scan for keypresses in console ruby? Myrddin Emrys Ruby 1 07-24-2006 06:04 PM
Re: JTable in Applet doesn't receive keypresses under Win2000 (XP is Ok) Dag Sunde Java 0 12-30-2004 04:16 PM
Detecting multiple keypresses kstahl C Programming 7 05-13-2004 05:43 PM



Advertisments