summaryrefslogtreecommitdiffstats
path: root/apps/examples/hidkbd
diff options
context:
space:
mode:
Diffstat (limited to 'apps/examples/hidkbd')
-rw-r--r--apps/examples/hidkbd/Kconfig31
-rw-r--r--apps/examples/hidkbd/hidkbd_main.c106
2 files changed, 136 insertions, 1 deletions
diff --git a/apps/examples/hidkbd/Kconfig b/apps/examples/hidkbd/Kconfig
index 503d9d9d9c..81f817ef9b 100644
--- a/apps/examples/hidkbd/Kconfig
+++ b/apps/examples/hidkbd/Kconfig
@@ -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
diff --git a/apps/examples/hidkbd/hidkbd_main.c b/apps/examples/hidkbd/hidkbd_main.c
index 8c9f6fa95f..8a632bab75 100644
--- a/apps/examples/hidkbd/hidkbd_main.c
+++ b/apps/examples/hidkbd/hidkbd_main.c
@@ -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
*
@@ -141,6 +237,10 @@ static int hidkbd_waiter(int argc, char *argv[])
}
/****************************************************************************
+ * 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);