This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e01c4311 examples/hidcombo: Add USB HID keyboard + mouse combo example
9e01c4311 is described below

commit 9e01c43112f60aa7fceb9598f7ca6fd1904cff49
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Fri Oct 23 15:21:49 2026 -0300

    examples/hidcombo: Add USB HID keyboard + mouse combo example
    
    Add an example that reads a USB HID keyboard and a USB HID mouse at the
    same time from a single task using poll().  It is meant to test devices
    that expose both a keyboard and a mouse, like the wireless keyboard +
    touchpad receivers, and uses less memory than running hidkbd and tc
    side by side, which matters on small boards (e.g. stm32f103-minimum).
    
    Signed-off-by: Alan C. Assis <[email protected]>
    Assisted-by: Claude Code Opus 5.5
---
 examples/hidcombo/CMakeLists.txt  |  34 ++++++++
 examples/hidcombo/Kconfig         |  40 +++++++++
 examples/hidcombo/Make.defs       |  24 ++++++
 examples/hidcombo/Makefile        |  33 ++++++++
 examples/hidcombo/hidcombo_main.c | 166 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 297 insertions(+)

diff --git a/examples/hidcombo/CMakeLists.txt b/examples/hidcombo/CMakeLists.txt
new file mode 100644
index 000000000..87afafbbc
--- /dev/null
+++ b/examples/hidcombo/CMakeLists.txt
@@ -0,0 +1,34 @@
+# 
##############################################################################
+# apps/examples/hidcombo/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may 
not
+# use this file except in compliance with the License.  You may obtain a copy 
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+
+if(CONFIG_EXAMPLES_HIDCOMBO)
+  nuttx_add_application(
+    NAME
+    ${CONFIG_EXAMPLES_HIDCOMBO_PROGNAME}
+    PRIORITY
+    ${CONFIG_EXAMPLES_HIDCOMBO_PRIORITY}
+    STACKSIZE
+    ${CONFIG_EXAMPLES_HIDCOMBO_STACKSIZE}
+    MODULE
+    ${CONFIG_EXAMPLES_HIDCOMBO}
+    SRCS
+    hidcombo_main.c)
+endif()
diff --git a/examples/hidcombo/Kconfig b/examples/hidcombo/Kconfig
new file mode 100644
index 000000000..c52fc698d
--- /dev/null
+++ b/examples/hidcombo/Kconfig
@@ -0,0 +1,40 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_HIDCOMBO
+       tristate "USB HID keyboard + mouse combo example"
+       default n
+       depends on USBHOST_HIDKBD && USBHOST_HIDMOUSE && !HIDMOUSE_TSCIF
+       ---help---
+               Read a USB HID keyboard and a USB HID mouse at the same time 
from
+               a single task using poll().  Useful to test keyboard + touchpad
+               receivers, which expose both devices on the same USB device.
+
+if EXAMPLES_HIDCOMBO
+
+config EXAMPLES_HIDCOMBO_PROGNAME
+       string "Program name"
+       default "hidcombo"
+       ---help---
+               This is the name of the program that will be used when the NSH 
ELF
+               program is installed.
+
+config EXAMPLES_HIDCOMBO_PRIORITY
+       int "Task priority"
+       default 100
+
+config EXAMPLES_HIDCOMBO_STACKSIZE
+       int "Task stack size"
+       default DEFAULT_TASK_STACKSIZE
+
+config EXAMPLES_HIDCOMBO_KBDDEV
+       string "Keyboard device"
+       default "/dev/kbda"
+
+config EXAMPLES_HIDCOMBO_MOUSEDEV
+       string "Mouse device"
+       default "/dev/mouse0"
+
+endif # EXAMPLES_HIDCOMBO
diff --git a/examples/hidcombo/Make.defs b/examples/hidcombo/Make.defs
new file mode 100644
index 000000000..8714bb0e3
--- /dev/null
+++ b/examples/hidcombo/Make.defs
@@ -0,0 +1,24 @@
+############################################################################
+# apps/examples/hidcombo/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+ifneq ($(CONFIG_EXAMPLES_HIDCOMBO),)
+CONFIGURED_APPS += $(APPDIR)/examples/hidcombo
+endif
diff --git a/examples/hidcombo/Makefile b/examples/hidcombo/Makefile
new file mode 100644
index 000000000..45ee1a6b8
--- /dev/null
+++ b/examples/hidcombo/Makefile
@@ -0,0 +1,33 @@
+############################################################################
+# apps/examples/hidcombo/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+include $(APPDIR)/Make.defs
+
+# USB HID keyboard + mouse combo example
+
+PROGNAME  = $(CONFIG_EXAMPLES_HIDCOMBO_PROGNAME)
+PRIORITY  = $(CONFIG_EXAMPLES_HIDCOMBO_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_HIDCOMBO_STACKSIZE)
+MODULE    = $(CONFIG_EXAMPLES_HIDCOMBO)
+
+MAINSRC = hidcombo_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/examples/hidcombo/hidcombo_main.c 
b/examples/hidcombo/hidcombo_main.c
new file mode 100644
index 000000000..2edc39458
--- /dev/null
+++ b/examples/hidcombo/hidcombo_main.c
@@ -0,0 +1,166 @@
+/****************************************************************************
+ * apps/examples/hidcombo/hidcombo_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <nuttx/input/keyboard.h>
+#include <nuttx/input/mouse.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define HIDCOMBO_NEVENTS  40     /* Default number of events to wait for */
+#define HIDCOMBO_TIMEOUT  30000  /* Give up after 30 seconds without events */
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static FAR const char *hidcombo_keytype(uint32_t type)
+{
+  switch (type)
+    {
+      case KEYBOARD_PRESS:
+        return "press";
+
+      case KEYBOARD_RELEASE:
+        return "release";
+
+      case KEYBOARD_SPECPRESS:
+        return "special press";
+
+      case KEYBOARD_SPECREL:
+        return "special release";
+
+      default:
+        return "unknown";
+    }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * hidcombo_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  struct keyboard_event_s key;
+  struct mouse_report_s mouse;
+  struct pollfd fds[2];
+  int nevents = HIDCOMBO_NEVENTS;
+  int nkeys = 0;
+  int nmouse = 0;
+  int ret;
+
+  if (argc > 1)
+    {
+      nevents = atoi(argv[1]);
+    }
+
+  fds[0].fd = open(CONFIG_EXAMPLES_HIDCOMBO_KBDDEV, O_RDONLY | O_NONBLOCK);
+  if (fds[0].fd < 0)
+    {
+      printf("ERROR: Failed to open %s: %d\n",
+             CONFIG_EXAMPLES_HIDCOMBO_KBDDEV, errno);
+      return EXIT_FAILURE;
+    }
+
+  fds[1].fd = open(CONFIG_EXAMPLES_HIDCOMBO_MOUSEDEV, O_RDONLY | O_NONBLOCK);
+  if (fds[1].fd < 0)
+    {
+      printf("ERROR: Failed to open %s: %d\n",
+             CONFIG_EXAMPLES_HIDCOMBO_MOUSEDEV, errno);
+      close(fds[0].fd);
+      return EXIT_FAILURE;
+    }
+
+  fds[0].events = POLLIN;
+  fds[1].events = POLLIN;
+
+  printf("hidcombo: waiting for %d events from %s and %s\n", nevents,
+         CONFIG_EXAMPLES_HIDCOMBO_KBDDEV, CONFIG_EXAMPLES_HIDCOMBO_MOUSEDEV);
+
+  while (nkeys + nmouse < nevents)
+    {
+      fds[0].revents = 0;
+      fds[1].revents = 0;
+
+      ret = poll(fds, 2, HIDCOMBO_TIMEOUT);
+      if (ret < 0)
+        {
+          printf("ERROR: poll failed: %d\n", errno);
+          break;
+        }
+      else if (ret == 0)
+        {
+          printf("hidcombo: no events for %d seconds\n",
+                 HIDCOMBO_TIMEOUT / 1000);
+          break;
+        }
+
+      /* Read all of the buffered key events */
+
+      if ((fds[0].revents & POLLIN) != 0)
+        {
+          while (read(fds[0].fd, &key, sizeof(key)) == sizeof(key))
+            {
+              nkeys++;
+              printf("KEY   %-15s code=0x%02lx '%c'\n",
+                     hidcombo_keytype(key.type), (unsigned long)key.code,
+                     key.type <= KEYBOARD_RELEASE &&
+                     key.code >= 0x20 && key.code < 0x7f ?
+                     (int)key.code : '.');
+            }
+        }
+
+      /* The mouse driver keeps only the latest sample */
+
+      if ((fds[1].revents & POLLIN) != 0 &&
+          read(fds[1].fd, &mouse, sizeof(mouse)) == sizeof(mouse))
+        {
+          nmouse++;
+          printf("MOUSE buttons=%02x x=%d y=%d\n",
+                 mouse.buttons, mouse.x, mouse.y);
+        }
+    }
+
+  printf("hidcombo: %d key events, %d mouse events\n", nkeys, nmouse);
+
+  close(fds[1].fd);
+  close(fds[0].fd);
+  return EXIT_SUCCESS;
+}

Reply via email to