From 901cd90faa9354b7cd0017fe9bc719091bee4fd0 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 18 Jan 2011 23:28:02 +0000 Subject: Complete HID keyboard test git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3259 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/drivers/usbhost/usbhost_hidkbd.c | 22 +++++++-------- nuttx/examples/hidkbd/hidkbd_main.c | 51 ++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/nuttx/drivers/usbhost/usbhost_hidkbd.c b/nuttx/drivers/usbhost/usbhost_hidkbd.c index bf1dd6386..c8d8c419f 100644 --- a/nuttx/drivers/usbhost/usbhost_hidkbd.c +++ b/nuttx/drivers/usbhost/usbhost_hidkbd.c @@ -1698,7 +1698,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len { FAR struct inode *inode; FAR struct usbhost_state_s *priv; - size_t remaining; + size_t nbytes; unsigned int tail; int ret; @@ -1729,22 +1729,22 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len { /* Read data from our internal buffer of received characters */ - for (tail = priv->tailndx; - tail != priv->headndx && remaining > 0; - tail++, remaining--) + for (tail = priv->tailndx, nbytes = 0; + tail != priv->headndx && nbytes < len; + nbytes++) { + /* Copy the next keyboard character into the user buffer */ + + *buffer += priv->buffer[tail]; + /* Handle wrap-around of the tail index */ - if (tail >= CONFIG_USBHID_BUFSIZE) + if (++tail >= CONFIG_USBHID_BUFSIZE) { tail = 0; } - - /* Copy the next keyboard character into the user buffer */ - - *buffer += priv->buffer[tail]; - remaining--; } + ret = nbytes; /* Update the tail index (pehaps marking the buffer empty) */ @@ -1752,7 +1752,7 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len } usbhost_givesem(&priv->exclsem); - return 0; /* Return EOF for now */ + return (ssize_t)ret; } /**************************************************************************** diff --git a/nuttx/examples/hidkbd/hidkbd_main.c b/nuttx/examples/hidkbd/hidkbd_main.c index 7f9c8dbfe..1c5e92098 100644 --- a/nuttx/examples/hidkbd/hidkbd_main.c +++ b/nuttx/examples/hidkbd/hidkbd_main.c @@ -39,8 +39,12 @@ #include +#include +#include + #include #include +#include #include #include @@ -69,10 +73,15 @@ #ifndef CONFIG_EXAMPLES_HIDKBD_DEFPRIO # define CONFIG_EXAMPLES_HIDKBD_DEFPRIO 50 #endif + #ifndef CONFIG_EXAMPLES_HIDKBD_STACKSIZE # define CONFIG_EXAMPLES_HIDKBD_STACKSIZE 1024 #endif +#ifndef CONFIG_EXAMPLES_HIDKBD_DEVNAME +# define CONFIG_EXAMPLES_HIDKBD_DEVNAME "/dev/kbda" +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -144,7 +153,10 @@ void user_initialize(void) int user_start(int argc, char *argv[]) { + char buffer[256]; pid_t pid; + ssize_t nbytes; + int fd; int ret; /* First, register all of the USB host HID keyboard class driver */ @@ -181,9 +193,44 @@ int user_start(int argc, char *argv[]) for (;;) { + /* Open the keyboard device. Loop until the device is successfully + * opened. + */ + + do + { + printf("Opening device %s\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME); + fflush(stdout); + fd = open(CONFIG_EXAMPLES_HIDKBD_DEVNAME, O_RDONLY); + if (fd < 0) + { + sleep(3); + } + } + while (fd < 0); + + printf("Device %s opened\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME); + fflush(stdout); + + /* Loop until there is a read failure */ + + do + { + /* Read a buffer of data */ + + nbytes = read(fd, buffer, 256); + if (nbytes > 0) + { + /* On success, echo the buffer to stdout */ + + (void)write(1, buffer, nbytes); + } + } + while (nbytes >= 0); + + printf("Closing device %s\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME); fflush(stdout); - sleep(5); - printf("user_start: Still running...\n"); + close(fd); } } return 0; -- cgit v1.2.3