mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
examples/lvglterm: add USB HID keyboard input with cursor scrolling
Rework the terminal input selection into an explicit three-way Kconfig
choice so the format each keyboard delivers is picked up front:
- On-screen keyboard (touch) - default, unchanged behaviour.
- Matrix / upper-half keyboard - reads struct keyboard_event_s events
(for example the M5Stack Cardputer matrix on /dev/kbd0).
- USB HID keyboard - reads a byte stream (for example /dev/kbda with
CONFIG_USBHOST_HIDKBD).
The physical-keyboard variant now polls the device non-blocking from the
LVGL thread (no dedicated task), mirroring the touch variant. The USB
path decodes the stream with the keyboard codec, so with a driver built
for CONFIG_HIDKBD_ENCODED the Up/Down cursor keys scroll the terminal;
a plain-ASCII stream still works as ordinary key presses.
Signed-off-by: Jorge Guzman <jorge.gzm@gmail.com>
This commit is contained in:
parent
5971d9c19f
commit
dd24407c71
5 changed files with 167 additions and 121 deletions
|
|
@ -21,10 +21,10 @@
|
||||||
# ##############################################################################
|
# ##############################################################################
|
||||||
|
|
||||||
if(CONFIG_EXAMPLES_LVGLTERM)
|
if(CONFIG_EXAMPLES_LVGLTERM)
|
||||||
if(CONFIG_EXAMPLES_LVGLTERM_INPUT_KBD)
|
if(CONFIG_EXAMPLES_LVGLTERM_INPUT_TOUCH)
|
||||||
set(lvglterm_input lvglterm_kbd.c)
|
|
||||||
else()
|
|
||||||
set(lvglterm_input lvglterm_touch.c)
|
set(lvglterm_input lvglterm_touch.c)
|
||||||
|
else()
|
||||||
|
set(lvglterm_input lvglterm_kbd.c)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
nuttx_add_application(
|
nuttx_add_application(
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,9 @@ choice
|
||||||
default EXAMPLES_LVGLTERM_INPUT_TOUCH
|
default EXAMPLES_LVGLTERM_INPUT_TOUCH
|
||||||
---help---
|
---help---
|
||||||
Select how the terminal receives keystrokes. Only one input source
|
Select how the terminal receives keystrokes. Only one input source
|
||||||
is built at a time.
|
is built at a time. The physical-keyboard options differ in the data
|
||||||
|
the keyboard device delivers on read(), so pick the one that matches
|
||||||
|
the hardware.
|
||||||
|
|
||||||
config EXAMPLES_LVGLTERM_INPUT_TOUCH
|
config EXAMPLES_LVGLTERM_INPUT_TOUCH
|
||||||
bool "On-screen keyboard (touch)"
|
bool "On-screen keyboard (touch)"
|
||||||
|
|
@ -26,24 +28,36 @@ config EXAMPLES_LVGLTERM_INPUT_TOUCH
|
||||||
command line is typed and submitted with Enter. This is the default
|
command line is typed and submitted with Enter. This is the default
|
||||||
and matches the original behaviour.
|
and matches the original behaviour.
|
||||||
|
|
||||||
config EXAMPLES_LVGLTERM_INPUT_KBD
|
config EXAMPLES_LVGLTERM_INPUT_KBD_MATRIX
|
||||||
bool "Physical keyboard (/dev/kbdN)"
|
bool "Matrix / upper-half keyboard (keyboard events)"
|
||||||
depends on INPUT_KEYBOARD
|
depends on INPUT_KEYBOARD
|
||||||
---help---
|
---help---
|
||||||
Input comes from a physical keyboard registered as a /dev/kbdN
|
Physical keyboard registered through the INPUT_KEYBOARD upper half,
|
||||||
device; keystrokes are streamed to the shell and the output fills
|
whose read() returns struct keyboard_event_s events (for example the
|
||||||
the whole screen.
|
M5Stack Cardputer matrix keyboard on /dev/kbd0). Fn Up/Down scroll
|
||||||
|
the terminal.
|
||||||
|
|
||||||
|
config EXAMPLES_LVGLTERM_INPUT_KBD_USB
|
||||||
|
bool "USB HID keyboard (byte stream)"
|
||||||
|
depends on USBHOST_HIDKBD
|
||||||
|
select LIBC_KBDCODEC
|
||||||
|
---help---
|
||||||
|
USB HID keyboard (for example on /dev/kbda with CONFIG_USBHOST_HIDKBD).
|
||||||
|
read() returns a byte stream that is decoded with the keyboard codec:
|
||||||
|
normal keys go to the shell and, when the driver is built with
|
||||||
|
CONFIG_HIDKBD_ENCODED, the Up/Down cursor keys scroll the terminal.
|
||||||
|
|
||||||
endchoice
|
endchoice
|
||||||
|
|
||||||
config EXAMPLES_LVGLTERM_KBD_DEV
|
config EXAMPLES_LVGLTERM_KBD_DEV
|
||||||
string "Keyboard device path"
|
string "Keyboard device path"
|
||||||
|
default "/dev/kbda" if EXAMPLES_LVGLTERM_INPUT_KBD_USB
|
||||||
default "/dev/kbd0"
|
default "/dev/kbd0"
|
||||||
depends on EXAMPLES_LVGLTERM_INPUT_KBD
|
depends on EXAMPLES_LVGLTERM_INPUT_KBD_MATRIX || EXAMPLES_LVGLTERM_INPUT_KBD_USB
|
||||||
---help---
|
---help---
|
||||||
Keyboard device the terminal reads key events from. Can also be
|
Keyboard device the terminal reads from. Can also be overridden at
|
||||||
overridden at run time by passing the path as the first command-line
|
run time by passing the path as the first command-line argument
|
||||||
argument (useful when more than one keyboard is present).
|
(useful when more than one keyboard is present).
|
||||||
|
|
||||||
choice
|
choice
|
||||||
prompt "LVGL Terminal font"
|
prompt "LVGL Terminal font"
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ MODULE = $(CONFIG_EXAMPLES_LVGLTERM)
|
||||||
|
|
||||||
MAINSRC = lvglterm.c
|
MAINSRC = lvglterm.c
|
||||||
|
|
||||||
ifeq ($(CONFIG_EXAMPLES_LVGLTERM_INPUT_KBD),y)
|
ifneq ($(CONFIG_EXAMPLES_LVGLTERM_INPUT_TOUCH),y)
|
||||||
CSRCS = lvglterm_kbd.c
|
CSRCS = lvglterm_kbd.c
|
||||||
else
|
else
|
||||||
CSRCS = lvglterm_touch.c
|
CSRCS = lvglterm_touch.c
|
||||||
|
|
|
||||||
|
|
@ -304,7 +304,7 @@ static int create_terminal(int argc, FAR char *argv[])
|
||||||
if (pipe(g_nsh_stdin) < 0 || pipe(g_nsh_stdout) < 0 ||
|
if (pipe(g_nsh_stdin) < 0 || pipe(g_nsh_stdout) < 0 ||
|
||||||
pipe(g_nsh_stderr) < 0)
|
pipe(g_nsh_stderr) < 0)
|
||||||
{
|
{
|
||||||
_err("pipe failed: %d\n", errno);
|
fprintf(stderr, "pipe failed: %d\n", errno);
|
||||||
return ERROR;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -324,7 +324,7 @@ static int create_terminal(int argc, FAR char *argv[])
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
int errcode = errno;
|
int errcode = errno;
|
||||||
_err("posix_spawn failed: %d\n", errcode);
|
fprintf(stderr, "posix_spawn failed: %d\n", errcode);
|
||||||
return -errcode;
|
return -errcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,21 @@
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/* Physical-keyboard input variant of the LVGL terminal: a task reads key
|
/* Physical-keyboard input variant of the LVGL terminal: the keyboard device
|
||||||
* events from a /dev/kbdN keyboard device and streams them to NSH; the shell
|
* is polled (non-blocking) from the LVGL thread and key presses are streamed
|
||||||
* output fills the whole screen. Works on any board with a keyboard driver;
|
* to NSH; the shell output fills the whole screen. Works on any board with
|
||||||
* the device defaults to CONFIG_EXAMPLES_LVGLTERM_KBD_DEV and can be
|
* a keyboard driver; the device defaults to CONFIG_EXAMPLES_LVGLTERM_KBD_DEV
|
||||||
* overridden by the first command-line argument.
|
* and can be overridden by the first command-line argument.
|
||||||
|
*
|
||||||
|
* Two device flavours are supported, selected at build time:
|
||||||
|
*
|
||||||
|
* - Upper-half keyboards (INPUT_KEYBOARD, e.g. the M5Stack Cardputer
|
||||||
|
* matrix on /dev/kbd0) deliver struct keyboard_event_s events; the Fn
|
||||||
|
* Up/Down keys are handled locally as scroll requests.
|
||||||
|
* - USB HID keyboards (EXAMPLES_LVGLTERM_INPUT_KBD_USB, e.g. /dev/kbda)
|
||||||
|
* deliver a byte stream decoded with the NuttX keyboard codec: normal
|
||||||
|
* keys are forwarded to the shell and, when the driver is built with
|
||||||
|
* CONFIG_HIDKBD_ENCODED, the Up/Down cursor keys scroll the terminal.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
@ -34,13 +44,20 @@
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sched.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
#include <nuttx/input/keyboard.h>
|
#ifdef CONFIG_EXAMPLES_LVGLTERM_INPUT_KBD_MATRIX
|
||||||
|
# include <nuttx/input/keyboard.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLES_LVGLTERM_INPUT_KBD_USB
|
||||||
|
# include <nuttx/streams.h>
|
||||||
|
# include <nuttx/input/kbd_codec.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <lvgl/lvgl.h>
|
#include <lvgl/lvgl.h>
|
||||||
|
|
||||||
|
|
@ -67,8 +84,7 @@
|
||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int g_echo[2]; /* Keyboard task -> screen (local echo) */
|
static int g_kfd = -1; /* Keyboard device fd (opened O_NONBLOCK) */
|
||||||
static volatile int g_scroll; /* Pending scroll (lines), applied by LVGL */
|
|
||||||
static FAR const char *g_kbddev; /* Keyboard device path (/dev/kbdN) */
|
static FAR const char *g_kbddev; /* Keyboard device path (/dev/kbdN) */
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
@ -76,87 +92,46 @@ static FAR const char *g_kbddev; /* Keyboard device path (/dev/kbdN) */
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: kbd_task
|
* Name: feed_char
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Read key presses from the keyboard device and forward them to NSH stdin
|
* Forward one character to NSH stdin and, for printable characters and
|
||||||
* (and to the echo pipe so the user sees what is typed). The Fn Up/Down
|
* newline, echo it on screen. Control keys such as backspace are echoed
|
||||||
* keys are handled locally as scroll requests.
|
* by the shell's own line editor (via its stdout) to avoid handling them
|
||||||
|
* twice. Carriage return is normalised to newline. Runs in the LVGL
|
||||||
|
* thread, so it may touch the LVGL widgets directly.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int kbd_task(int argc, FAR char *argv[])
|
static void feed_char(char ch)
|
||||||
{
|
{
|
||||||
struct keyboard_event_s evt;
|
if (ch == '\r')
|
||||||
ssize_t nread;
|
|
||||||
int kfd;
|
|
||||||
char ch;
|
|
||||||
|
|
||||||
kfd = open(g_kbddev, O_RDONLY);
|
|
||||||
if (kfd < 0)
|
|
||||||
{
|
{
|
||||||
gerr("ERROR: open %s failed: %d\n", g_kbddev, errno);
|
ch = '\n';
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; ; )
|
write(g_nsh_stdin[WRITE_PIPE], &ch, 1);
|
||||||
|
|
||||||
|
if (ch == '\n' || ((uint8_t)ch >= 0x20 && (uint8_t)ch < 0x7f))
|
||||||
{
|
{
|
||||||
nread = read(kfd, &evt, sizeof(evt));
|
lvglterm_add_output(&ch, 1);
|
||||||
if (nread != (ssize_t)sizeof(evt))
|
|
||||||
{
|
|
||||||
if (nread <= 0)
|
|
||||||
{
|
|
||||||
usleep(10000);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (evt.type != KEYBOARD_PRESS)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fn navigation keys are handled locally: Up/Down scroll the terminal
|
|
||||||
* (applied by the LVGL thread) and are not sent to the shell.
|
|
||||||
* Left/Right are reserved and simply swallowed for now.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (evt.code >= KEY_UP && evt.code <= KEY_RIGHT)
|
|
||||||
{
|
|
||||||
if (evt.code == KEY_UP)
|
|
||||||
{
|
|
||||||
g_scroll -= 1;
|
|
||||||
}
|
|
||||||
else if (evt.code == KEY_DOWN)
|
|
||||||
{
|
|
||||||
g_scroll += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ch = (char)evt.code;
|
|
||||||
if (ch == '\r')
|
|
||||||
{
|
|
||||||
ch = '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Always feed the shell. Echo only printable characters and newline
|
|
||||||
* locally; control keys such as backspace are echoed by the shell's
|
|
||||||
* own line editor (via its stdout) to avoid handling them twice.
|
|
||||||
*/
|
|
||||||
|
|
||||||
write(g_nsh_stdin[WRITE_PIPE], &ch, 1);
|
|
||||||
|
|
||||||
if (ch == '\n' || ((uint8_t)ch >= 0x20 && (uint8_t)ch < 0x7f))
|
|
||||||
{
|
|
||||||
write(g_echo[WRITE_PIPE], &ch, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
close(kfd);
|
/****************************************************************************
|
||||||
return EXIT_SUCCESS;
|
* Name: scroll_terminal
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Scroll the output text area up (older output) or down by one step. Runs
|
||||||
|
* in the LVGL thread.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void scroll_terminal(bool up)
|
||||||
|
{
|
||||||
|
int32_t dy = up ? SCROLL_STEP : -SCROLL_STEP;
|
||||||
|
|
||||||
|
lv_obj_scroll_by(g_output, 0, dy, LV_ANIM_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
|
@ -168,7 +143,8 @@ static int kbd_task(int argc, FAR char *argv[])
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Resolve the keyboard device (first command-line argument, else the
|
* Resolve the keyboard device (first command-line argument, else the
|
||||||
* configured default) and start the task that streams key presses to NSH.
|
* configured default) and open it non-blocking so that it can be polled
|
||||||
|
* from the LVGL thread.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
@ -176,53 +152,109 @@ void lvglterm_input_create(int argc, FAR char *argv[])
|
||||||
{
|
{
|
||||||
g_kbddev = (argc > 1) ? argv[1] : CONFIG_EXAMPLES_LVGLTERM_KBD_DEV;
|
g_kbddev = (argc > 1) ? argv[1] : CONFIG_EXAMPLES_LVGLTERM_KBD_DEV;
|
||||||
|
|
||||||
if (pipe(g_echo) < 0)
|
g_kfd = open(g_kbddev, O_RDONLY | O_NONBLOCK);
|
||||||
|
if (g_kfd < 0)
|
||||||
{
|
{
|
||||||
gerr("ERROR: echo pipe failed: %d\n", errno);
|
gerr("ERROR: open %s failed: %d\n", g_kbddev, errno);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The read end is polled by the LVGL timer and must not block */
|
|
||||||
|
|
||||||
fcntl(g_echo[READ_PIPE], F_SETFL,
|
|
||||||
fcntl(g_echo[READ_PIPE], F_GETFL) | O_NONBLOCK);
|
|
||||||
|
|
||||||
task_create("lvgltermkbd", SCHED_PRIORITY_DEFAULT, 2048, kbd_task, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: lvglterm_input_poll
|
* Name: lvglterm_input_poll
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Apply any pending scroll and drain the local echo pipe onto the screen.
|
* Drain any pending key presses from the keyboard device and forward them
|
||||||
* Runs in the LVGL thread.
|
* to NSH (echoing what is typed on screen). Runs in the LVGL thread from
|
||||||
|
* the terminal's periodic timer, so the read must not block.
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
void lvglterm_input_poll(void)
|
void lvglterm_input_poll(void)
|
||||||
{
|
{
|
||||||
char buf[64];
|
if (g_kfd < 0)
|
||||||
int n;
|
{
|
||||||
int s;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Apply any pending scroll requested by the keyboard task (Up/Down).
|
#ifdef CONFIG_EXAMPLES_LVGLTERM_INPUT_KBD_USB
|
||||||
* A negative value scrolls up (towards older output), positive scrolls
|
/* USB HID keyboard: read() returns a byte stream that is decoded with the
|
||||||
* down. Consume only what we read so concurrent keypresses are not lost.
|
* keyboard codec. Normal keys are fed to the shell; the Up/Down cursor
|
||||||
|
* keys (only emitted when the driver is built with CONFIG_HIDKBD_ENCODED)
|
||||||
|
* scroll the terminal. On a plain-ASCII stream every byte simply decodes
|
||||||
|
* to a normal key press, so this also works without encoding.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
s = g_scroll;
|
struct lib_meminstream_s stream;
|
||||||
if (s != 0)
|
struct kbd_getstate_s state;
|
||||||
|
char buf[64];
|
||||||
|
ssize_t nread;
|
||||||
|
uint8_t ch;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
nread = read(g_kfd, buf, sizeof(buf));
|
||||||
|
if (nread <= 0)
|
||||||
{
|
{
|
||||||
g_scroll -= s;
|
return;
|
||||||
lv_obj_scroll_by(g_output, 0, -s * SCROLL_STEP, LV_ANIM_OFF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lvglterm_has_input(g_echo[READ_PIPE]))
|
memset(&state, 0, sizeof(state));
|
||||||
|
lib_meminstream(&stream, buf, nread);
|
||||||
|
|
||||||
|
for (; ; )
|
||||||
{
|
{
|
||||||
n = read(g_echo[READ_PIPE], buf, sizeof(buf));
|
ret = kbd_decode((FAR struct lib_instream_s *)&stream, &state, &ch);
|
||||||
if (n > 0)
|
if (ret == KBD_ERROR)
|
||||||
{
|
{
|
||||||
lvglterm_add_output(buf, n);
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == KBD_PRESS)
|
||||||
|
{
|
||||||
|
feed_char((char)ch);
|
||||||
|
}
|
||||||
|
else if (ret == KBD_SPECPRESS)
|
||||||
|
{
|
||||||
|
if (ch == KEYCODE_UP)
|
||||||
|
{
|
||||||
|
scroll_terminal(true);
|
||||||
|
}
|
||||||
|
else if (ch == KEYCODE_DOWN)
|
||||||
|
{
|
||||||
|
scroll_terminal(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
/* Upper-half keyboard: read() returns keyboard_event_s events */
|
||||||
|
|
||||||
|
struct keyboard_event_s evt;
|
||||||
|
|
||||||
|
while (read(g_kfd, &evt, sizeof(evt)) == (ssize_t)sizeof(evt))
|
||||||
|
{
|
||||||
|
if (evt.type != KEYBOARD_PRESS)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fn navigation keys are handled locally: Up/Down scroll the terminal;
|
||||||
|
* Left/Right are reserved and simply swallowed for now.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (evt.code >= KEY_UP && evt.code <= KEY_RIGHT)
|
||||||
|
{
|
||||||
|
if (evt.code == KEY_UP)
|
||||||
|
{
|
||||||
|
scroll_terminal(true);
|
||||||
|
}
|
||||||
|
else if (evt.code == KEY_DOWN)
|
||||||
|
{
|
||||||
|
scroll_terminal(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
feed_char((char)evt.code);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue