How do I wait for keypress without loop?

keckstar

New member
Hello. I've written some C++ to control the keys for my 635, but I do not know how to wait for a keypress without being in a loop. It works except it takes up 90+% of the CPU.

Hopefully someone can show me how to get rid of this problem! Thanks!
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 
In general, there are two (or three) ways to wait for an event:

1. polling in a loop

2. respond to an interrupt

(3. wake up from sleep)

Why is polling a problem? If your cpu has nothing else to do while waiting for a keypress, then 90%+ cpu usage is not an issue. If power consumption is important, then use a cpu wait/stop/etc mode, and interrupt to wake up.

If you supply more info, a better answer might be given.
 

CF Tech

Administrator
Try putting a Sleep(50) in your loop. Thit will allow you to poll at about 20Hz. The Sleep(50) gives control back to the OS, so the CPU usage shoulf go down.
 

Guinness

New member
I have the same problem and I have tried sleep(1) in my loop.
With that configuration I poll every second if a key has been pressed.

If yes, I switch to usleep(100000) and then I poll every 100ms until the user has exit my menu. Then i go back to sleep(1).
That all works fine but I get problems when I listen for keys, fanspeeds and temperature at the same time.

When all of that stuff is enabled and I poll with sleep(1), temp and fans are doing pretty well but it takes sometimes up to 5 seconds or multiple press of the same key until a pressed key is recognized and that's far to much!

Why is fan/temp reporting slowing down the system that much? Are there any solutions or what am I doing wrong?


Ralf
 
Originally posted by Guinness I have the same problem and I have tried sleep(1) in my loop.
With that configuration I poll every second if a key has been pressed.
Seems to me that would result in a one millisecond sleep (1000 polls per second), not one second.
 

Guinness

New member
Hi,

I am using C and Fedora Core 5, int the product we will use FC4.

@cosmicvoid:
If I use sleep(1) it is a second and sleep(50) --> 50 seconds. That's why I have to use usleep() to get a timespan smaller than 1 second.
Maybe that's because I use Linux?!

The LCD will be built in a decoder, because of that a cpu load of more than 1-3% for polling the keys is unacceptable.
Interrupting the decoder is also a bad idea because it is decoding a video stream in realtime...that all makes it so difficult!
 
Last edited:

CF Tech

Administrator
My understanding is that "Sleep()" is Windows, in mS, and "sleep()" is Linux / unix in S.

Can you hook the RS-232 incoming character interrupt? If so, just run "check_for_packet()" in that ISR, or have that ISR somehow trigger a "foreground" call to "check_for_packet()" to look for the keys.

If a key packet is 5 bytes, it would be no big deal to call check_for_packet() 5 times (once for every incoming byte), on the last call it will return the packet. This is nearly how the module's firmware does it.
 
Top