From 32464a54e18c12622724ed1a5d93f31d31eff409 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 23 Jan 2013 23:11:13 +0000 Subject: Add option to used keyboard CODEC in apps/examples/keypadtest git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5554 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/keypadtest/Kconfig | 8 ++ apps/examples/keypadtest/keypadtest_main.c | 115 ++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 1 deletion(-) (limited to 'apps') diff --git a/apps/examples/keypadtest/Kconfig b/apps/examples/keypadtest/Kconfig index 9dee80633..98c6d348e 100644 --- a/apps/examples/keypadtest/Kconfig +++ b/apps/examples/keypadtest/Kconfig @@ -18,4 +18,12 @@ if EXAMPLES_KEYPADTEST The name of the keypad device that will be opened in order to perform the keypad test. Default: "/dev/keypad" +config EXAMPLES_KEYPADTEST_ENCODED + bool "Use Keyboard CODEC" + default n + ---help--- + Use the keyboard encoded/decoder to pass control information from + the keypad driver to the keypad test. This is the keyboard CODEC + defined in nuttx/input/kbd_codec.h. + endif diff --git a/apps/examples/keypadtest/keypadtest_main.c b/apps/examples/keypadtest/keypadtest_main.c index afdc3e08a..dd7369c3e 100644 --- a/apps/examples/keypadtest/keypadtest_main.c +++ b/apps/examples/keypadtest/keypadtest_main.c @@ -50,6 +50,11 @@ #include +#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED +# include +# include +#endif + /**************************************************************************** * Definitions ****************************************************************************/ @@ -67,6 +72,15 @@ * Private Types ****************************************************************************/ +#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED +struct keypad_instream_s +{ + struct lib_instream_s stream; + FAR char *buffer; + ssize_t nbytes; +}; +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -75,6 +89,102 @@ * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: keypad_getstream + * + * Description: + * Get one character from the keyboard. + * + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED +static int keypad_getstream(FAR struct lib_instream_s *this) +{ + FAR struct keypad_instream_s *kbdstream = (FAR struct keypad_instream_s *)this; + + DEBUGASSERT(kbdstream && kbdstream->buffer); + if (kbdstream->nbytes > 0) + { + kbdstream->nbytes--; + kbdstream->stream.nget++; + return (int)*kbdstream->buffer++; + } + + return EOF; +} +#endif + +/**************************************************************************** + * Name: keypad_decode + * + * Description: + * Decode encoded keyboard input + * + ****************************************************************************/ + +#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED +static void keypad_decode(FAR char *buffer, ssize_t nbytes) +{ + struct keypad_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 = keypad_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_decode((FAR struct lib_instream_s *)&kbdstream, &state, &ch); + if (ret == KBD_ERROR) + { + break; + } + + /* Normal data? Or special key? */ + + switch (ret) + { + case KBD_PRESS: /* Key press event */ + printf("Normal Press: %c [%02x]\n", isprint(ch) ? ch : '.', ch); + break; + + case KBD_RELEASE: /* Key release event */ + printf("Normal Release: %c [%02x]\n", isprint(ch) ? ch : '.', ch); + break; + + case KBD_SPECPRESS: /* Special key press event */ + printf("Special Press: %d\n", ch); + break; + + case KBD_SPECREL: /* Special key release event */ + printf("Special Release: %d\n", ch); + break; + + case KBD_ERROR: /* Error or end-of-file */ + printf("EOF: %d\n", ret); + break; + + default: + printf("Unexpected: %d\n", ret); + break; + } + } +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -125,8 +235,11 @@ int keypadtest_main(int argc, char *argv[]) if (nbytes > 0) { /* On success, echo the buffer to stdout */ - +#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED + keypad_decode(buffer, nbytes); +#else (void)write(1, buffer, nbytes); +#endif } } while (nbytes >= 0); -- cgit v1.2.3