[En-Nut-Discussion] getch/kbhit

Brett Abbott Brett.Abbott at digital-telemetry.com
Thu May 20 01:30:30 CEST 2004


Originally posted May 2003.  Support to test for incoming characters 
waiting is kind of there.  This may help you out

Oliver - Could the test part of this be usefully added to nutos as 
kbhit? - Looks ugly but is pretty straightforward to use.

Regards
Brett

Hi

The following snippet of code can be used to check for the existance of 
incoming characters without having to actually receive them.  Useful for 
"Press the Return key or wait 10 seconds to reload last known 
configuration" type messages.  Improved versions of version portable 
code welcomed.

It uses the uart library.  Is there a more generic stream or file 
function call I could use?  I chose this approach rather than write a 
custom function to hopefully support future and past nutos releases.

Hopefully this will save someone some effort.

Cheers
Brett

// Globals
FILE *uart;
u_long UartStatus;
u_long *UartStatus_ptr;

// Sample Code to check for key press, Assumes uart file already open. 
Does not block if no characters were received.

  // Were any keys pressed?
  result=UartAvrIOCtl(((NUTFILE *) (_fileno(uart)))->nf_dev, 
UART_GETSTATUS, UartStatus_ptr);
  if((UartStatus & UART_RXBUFFEREMPTY)==0) // One or more keys were pressed
       // Now go get it
       if((rlen = _read(_fileno(uart), rxbuff_ptr, RXBUFF_LEN)) > 0) // 
Get the characters
          if(strchr(rxbuff_ptr, '\r') || strchr(rxbuff_ptr, '\n'))    // 
Did the user hit <CR> or <LF> ?
              UserEnterNewConfig();  // Yes? go to Configure mode

-----------------------------------------------------------------
Brett Abbott, Managing Director, Digital Telemetry Limited
Email: Brett.Abbott at digital-telemetry.com
PO Box 24 036 Manners Street, Wellington, New Zealand
Phone +64 (4) 5666-860  Mobile +64 (21) 656-144
------------------- Commercial in confidence --------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20040520/ddaf7212/attachment.html 


More information about the En-Nut-Discussion mailing list