Andrew Hooper wrote:
> 
> What is the best way to check the keyboard?, I want the programme
> to pause if P is pressed, Halt if ESC or put the emergency break on if
> space is pressed.
> 
> Would it be best to do this with interupts?, was using scanf, and getch
> but both seem to require a CR after the character has been pressed.
> 
> I remember doing this in TC and using the kbhit() function but that dont
> work in linux, is there a similar way?
> 
> TIA
> Regards
> Andrew

Hi Andrew,

This is off topic, and I must strongly advise against having software in
the loop for emergency breaks.  IMHO, the emergency stop must be some
direct hardware interlock.  Having said that, here's some code that will
let you check if keys are pressed without waiting for a CR.  Note this
is user space code, and so it can be locked out by other processes, so
again use it at your peril (hardware is your friend for safety stuff).

Regards, Stuart


static void key_in()
{
    static init = 0;
    static struct termios tty, otty;
    char c[2]   = { 0, 0 };

    if(init == 0) {
        tcgetattr(0, &tty);
        otty = tty;
        tty.c_lflag &= ~ICANON;
        tcsetattr(0, TCSANOW, &tty);
        fcntl(0, F_SETFL, O_NONBLOCK);
        init = 1;
        return;
    }

    if( (read(0, c, 1) == 1 ) ) {
        switch (c[0]) {
            case 'v':
                // do some stuff for the v key
                break;
            case 'r':
                break;
            default:
                printf("unknown key\n");
        }
    }
}


--- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
----
For more information on Real-Time Linux see:
http://www.rtlinux.org/~rtlinux/

Reply via email to