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
This commit is contained in:
patacongo 2012-12-26 21:37:50 +00:00
parent f890e1c504
commit f18028f1bc
4 changed files with 152 additions and 3 deletions

View File

@ -453,4 +453,6 @@
in place early in the dependency generation phase to avoid warnings.
It is not important if they are only stubbed out header files at
this build phase.
* apps/examples/hidbkd: Now supports decoding of encoded special keys
if CONFIG_EXAMPLES_HIDKBD_ENCODED is defined.

View File

@ -540,9 +540,21 @@ examples/hidkbd
This is a simple test to debug/verify the USB host HID keyboard class
driver.
CONFIG_EXAMPLES_HIDKBD_DEFPRIO - Priority of "waiter" thread.
CONFIG_EXAMPLES_HIDKBD_STACKSIZE - Stacksize of "waiter" thread.
CONFIG_EXAMPLES_HIDKBD_DEFPRIO - Priority of "waiter" thread. Default:
50
CONFIG_EXAMPLES_HIDKBD_STACKSIZE - Stacksize of "waiter" thread. Default
1024
CONFIG_EXAMPLES_HIDKBD_DEVNAME - Name of keyboard device to be used.
Default: "/dev/kbda"
CONFIG_EXAMPLES_HIDKBD_ENCODED - Decode special key press events in the
user buffer. In this case, the example coded will use the interfaces
defined in include/nuttx/input/kbd_codec.h to decode the returned
keyboard data. These special keys include such things as up/down
arrows, home and end keys, etc. If this not defined, only 7-bit print-
able and control ASCII characters will be provided to the user.
Requires CONFIG_HIDKBD_ENCODED && CONFIG_LIB_KBDCODEC
endif
examples/igmp
^^^^^^^^^^^^^

View File

@ -10,4 +10,35 @@ config EXAMPLES_HIDKBD
Enable the USB HID keyboard example
if EXAMPLES_HIDKBD
config EXAMPLES_HIDKBD_DEFPRIO
int "Waiter Thread Priority"
default 50
---help---
Priority of "waiter" thread. Default: 50
config EXAMPLES_HIDKBD_STACKSIZE
int "Waiter Thread Stack Size"
default 1024
---help---
Stacksize of "waiter" thread. Default 1024
config EXAMPLES_HIDKBD_DEVNAME
string "Keyboard Device Name"
default "/dev/kbda"
---help---
Name of keyboard device to be used. Default: "/dev/kbda"
config EXAMPLES_HIDKBD_ENCODED
bool "Encode Special Keys"
default y
depends on HIDKBD_ENCODED && LIB_KBDCODEC
---help---
Decode special key press events in the user buffer. In this case,
the example coded will use the interfaces defined in
include/nuttx/input/kbd_codec.h to decode the returned keyboard
data. These special keys include such things as up/down arrows,
home and end keys, etc. If this not defined, only 7-bit print-able
and control ASCII characters will be provided to the user.
endif

View File

@ -46,10 +46,18 @@
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/usb/usbhost.h>
#ifdef CONFIG_EXAMPLES_HIDKBD_ENCODED
# include <nuttx/streams.h>
# include <nuttx/input/kbd_codec.h>
#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);