diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-01-18 23:28:02 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-01-18 23:28:02 +0000 |
commit | 901cd90faa9354b7cd0017fe9bc719091bee4fd0 (patch) | |
tree | 14b79503021a71d4adb97e8e0ac72087b8521fd4 | |
parent | 0d199a1296adae52e01c1404657e55c34df34283 (diff) | |
download | nuttx-901cd90faa9354b7cd0017fe9bc719091bee4fd0.tar.gz nuttx-901cd90faa9354b7cd0017fe9bc719091bee4fd0.tar.bz2 nuttx-901cd90faa9354b7cd0017fe9bc719091bee4fd0.zip |
Complete HID keyboard test
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3259 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r-- | nuttx/drivers/usbhost/usbhost_hidkbd.c | 22 | ||||
-rw-r--r-- | 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 <nuttx/config.h> +#include <sys/types.h> +#include <sys/stat.h> + #include <stdio.h> #include <unistd.h> +#include <fcntl.h> #include <sched.h> #include <nuttx/usb/usbhost.h> @@ -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; |