From 24f3d5c021bfa7d0a99870d363d86e4e17e4ca63 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 26 Dec 2012 21:37:50 +0000 Subject: Verified USB HID KBD driver encoding of special characters; apps/examples/hidkbd now decodes encoded keyboar characters. git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5463 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/hidkbd/hidkbd_main.c | 106 ++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) (limited to 'apps/examples/hidkbd/hidkbd_main.c') diff --git a/apps/examples/hidkbd/hidkbd_main.c b/apps/examples/hidkbd/hidkbd_main.c index 8c9f6fa95..8a632bab7 100644 --- a/apps/examples/hidkbd/hidkbd_main.c +++ b/apps/examples/hidkbd/hidkbd_main.c @@ -46,10 +46,18 @@ #include #include #include +#include +#include +#include #include #include +#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED +# include +# include +#endif + /**************************************************************************** * Definitions ****************************************************************************/ @@ -83,10 +91,23 @@ # define CONFIG_EXAMPLES_HIDKBD_DEVNAME "/dev/kbda" #endif +#if !defined(CONFIG_HIDKBD_ENCODED) || !defined(CONFIG_LIB_KBDCODEC) +# undef CONFIG_EXAMPLES_HIDKBD_ENCODED +#endif + /**************************************************************************** * Private Types ****************************************************************************/ +#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED +struct hidbkd_instream_s +{ + struct lib_instream_s stream; + FAR char *buffer; + ssize_t nbytes; +}; +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -98,9 +119,84 @@ static struct usbhost_driver_s *g_drvr; ****************************************************************************/ /**************************************************************************** - * Public Functions + * Name: hidkbd_getstream + * + * Description: + * Get one character from the keyboard. + * ****************************************************************************/ +#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED +static int hidkbd_getstream(FAR struct lib_instream_s *this) +{ + FAR struct hidbkd_instream_s *kbdstream = (FAR struct hidbkd_instream_s *)this; + + DEBUGASSERT(kbdstream && kbdstream->buffer); + if (kbdstream->nbytes > 0) + { + kbdstream->nbytes--; + kbdstream->stream.nget++; + return (int)*kbdstream->buffer++; + } + + return EOF; +} +#endif + +/**************************************************************************** + * Name: hidkbd_decode + * + * Description: + * Decode encoded keyboard input + * + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED +static void hidkbd_decode(FAR char *buffer, ssize_t nbytes) +{ + struct hidbkd_instream_s kbdstream; + struct kbd_getstate_s state; + uint8_t ch; + int ret; + + /* Initialize */ + + memset(&state, 0, sizeof(struct kbd_getstate_s)); + kbdstream.stream.get = hidkbd_getstream; + kbdstream.stream.nget = 0; + kbdstream.buffer = buffer; + kbdstream.nbytes = nbytes; + + /* Loop until all of the bytes have been consumed. We implicitly assume + * that the the escaped sequences do not cross buffer boundaries. That + * might be true if the read buffer were small or the data rates high. + */ + + for (;;) + { + /* Decode the next thing from the buffer */ + + ret = kbd_get((FAR struct lib_instream_s *)&kbdstream, &state, &ch); + if (ret == KBD_ERROR) + { + break; + } + + /* Normal data? Or special key? */ + + if (ret == KBD_NORMAL) + { + printf("Data: %c [%02x]\n", isprint(ch) ? ch : '.', ch); + } + else + { + DEBUGASSERT(ret == KBD_SPECIAL); + printf("Special: %d\n", ch); + } + } +} +#endif + /**************************************************************************** * Name: hidkbd_waiter * @@ -140,6 +236,10 @@ static int hidkbd_waiter(int argc, char *argv[]) return 0; } +/**************************************************************************** + * Public Functions + ****************************************************************************/ + /**************************************************************************** * Name: hidkbd_main ****************************************************************************/ @@ -217,7 +317,11 @@ int hidkbd_main(int argc, char *argv[]) { /* On success, echo the buffer to stdout */ +#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED + hidkbd_decode(buffer, nbytes); +#else (void)write(1, buffer, nbytes); +#endif } } while (nbytes > 0); -- cgit v1.2.3