Skip to content
Snippets Groups Projects
Commit b2007c8e authored by patacongo's avatar patacongo
Browse files

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
parent d39a78a9
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -50,6 +50,11 @@
#include <nuttx/input/keypad.h>
#ifdef CONFIG_EXAMPLES_KEYPADTEST_ENCODED
# include <nuttx/streams.h>
# include <nuttx/input/kbd_codec.h>
#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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment