diff --git a/examples/keyboard/Kconfig b/examples/keyboard/Kconfig new file mode 100644 index 000000000..208907e42 --- /dev/null +++ b/examples/keyboard/Kconfig @@ -0,0 +1,20 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_KEYBOARD + tristate "Keyboard example" + default n + ---help--- + Enable the keyboard example + +if EXAMPLES_KEYBOARD + +config EXAMPLES_KEYBOARD_DEVPATH + string "Keyboard device path" + default "/dev/kbd0" + ---help--- + The path to the keyboard device. Default: "/dev/kbd0" + +endif diff --git a/examples/keyboard/Make.defs b/examples/keyboard/Make.defs new file mode 100644 index 000000000..0310f739e --- /dev/null +++ b/examples/keyboard/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/keyboard/Make.defs +# +# 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_KEYBOARD),) +CONFIGURED_APPS += $(APPDIR)/examples/keyboard +endif diff --git a/examples/keyboard/Makefile b/examples/keyboard/Makefile new file mode 100644 index 000000000..8010d71a1 --- /dev/null +++ b/examples/keyboard/Makefile @@ -0,0 +1,34 @@ +############################################################################ +# apps/examples/keyboard/Makefile +# +# 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 + +# Keyboard built-in application info + +PROGNAME = kbd +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_KEYBOARD) + +# Keyboard Example + +MAINSRC = kbd_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/keyboard/kbd_main.c b/examples/keyboard/kbd_main.c new file mode 100644 index 000000000..a1624e844 --- /dev/null +++ b/examples/keyboard/kbd_main.c @@ -0,0 +1,159 @@ +/**************************************************************************** + * apps/examples/keyboard/kbd_main.c + * + * 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 + +#include + +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: kbd_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + struct keyboard_event_s sample; + ssize_t nbytes; + long nsamples = 0; + int fd; + int errval = 0; + + if (argc > 1) + { + nsamples = strtol(argv[1], NULL, 10); + } + + printf("kbd_main: nsamples: %ld\n", nsamples); + + /* Open the keyboard device for reading */ + + printf("kbd_main: Opening %s\n", CONFIG_EXAMPLES_KEYBOARD_DEVPATH); + fd = open(CONFIG_EXAMPLES_KEYBOARD_DEVPATH, O_RDONLY); + if (fd < 0) + { + printf("kbd_main: open %s failed: %d\n", + CONFIG_EXAMPLES_KEYBOARD_DEVPATH, errno); + errval = 2; + goto errout; + } + + /* Now loop the appropriate number of times, displaying the collected + * keyboard samples. + */ + + for (; ; ) + { + /* Flush any output before the loop entered or from the previous pass + * through the loop. + */ + + fflush(stdout); + + /* Read one sample */ + + iinfo("Reading...\n"); + nbytes = read(fd, &sample, sizeof(struct keyboard_event_s)); + iinfo("Bytes read: %zd\n", nbytes); + + /* Handle unexpected return values */ + + if (nbytes < 0) + { + errval = errno; + if (errval != EINTR) + { + printf("kbd_main: read %s failed: %d\n", + CONFIG_EXAMPLES_KEYBOARD_DEVPATH, errval); + errval = 3; + goto errout_with_dev; + } + + printf("kbd_main: Interrupted read...\n"); + } + else if (nbytes != sizeof(struct keyboard_event_s)) + { + printf("kbd_main: Unexpected read size=%zd, expected=%zd, " + "Ignoring\n", + nbytes, sizeof(struct keyboard_event_s)); + } + + /* Print the sample data on successful return */ + + else + { + printf("Sample :\n"); + printf(" code : %d\n", sample.code); + printf(" type : %d\n", sample.type); + } + + if (nsamples && --nsamples <= 0) + { + break; + } + } + +errout_with_dev: + close(fd); + +errout: + printf("Terminating!\n"); + fflush(stdout); + return errval; +}