apps/examples:Add keyboard example

Signed-off-by: liuhongchao <liuhongchao@xiaomi.com>
This commit is contained in:
liuhongchao 2023-08-07 16:53:51 +08:00 committed by Xiang Xiao
parent 512af7fbd7
commit a3dfef117b
4 changed files with 236 additions and 0 deletions

20
examples/keyboard/Kconfig Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/input/keyboard.h>
/****************************************************************************
* 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;
}