From 40e1d434ff3387fb5f1bc649ba6b33557e6ab3ac Mon Sep 17 00:00:00 2001 From: phyto Date: Sat, 18 May 2019 17:58:44 +0200 Subject: [PATCH] making a serial embeded terminal --- ui/Makefile | 12 +++++ ui/t.c | 2 +- ui/terminal.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ ui/terminal.h | 6 +++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 ui/Makefile create mode 100644 ui/terminal.c create mode 100644 ui/terminal.h diff --git a/ui/Makefile b/ui/Makefile new file mode 100644 index 0000000..715a043 --- /dev/null +++ b/ui/Makefile @@ -0,0 +1,12 @@ + +CC = gcc +CCOPT = -Wall -g -DDEBUG_LEVEL=1 +CLIB = core/libdd2m-core.a viz/libdd2m-viz.a + +OSERIAL = ../serial/serial.o ../serial/funcs.o + +terminal.o: terminal.c Makefile terminal.h + $(CC) ${CCOPT} $< -c + +t: t.c terminal.o + ${CC} ${CCOPT} $< terminal.o ${OSERIAL} -lncurses -o $@ diff --git a/ui/t.c b/ui/t.c index 8151a99..a6a662e 100644 --- a/ui/t.c +++ b/ui/t.c @@ -58,7 +58,7 @@ if (serial_in < 0) { sleep(1); foo = essai_terminal(serial_in, device, K); - +fprintf(stderr, "essai terminal -> %d\n", foo); return 0; } /* ---------------------------------------------------------------- */ diff --git a/ui/terminal.c b/ui/terminal.c new file mode 100644 index 0000000..8d8a35b --- /dev/null +++ b/ui/terminal.c @@ -0,0 +1,141 @@ +/* + * emulateur de terminal + */ + +#include +#include +#include +#include +#include + +#include + +#include "../serial/serial.h" +#include "terminal.h" + +extern int verbosity; + +/* ---------------------------------------------------------------- */ +static void bordure(WINDOW * w, char *texte, int type) +{ +if (type) + box(w, 0, 0); +else + wborder(w, '|', '|', '-', '-', '+', '+', '+', '+'); +wstandout(w); mvwaddstr(w, 0, 3, texte); wstandend(w); +wrefresh(w); +} +/* ---------------------------------------------------------------- */ +static int interactive(WINDOW *glass, int fd_local, int fd_remote) +{ +int flag_exit = 0; +long tstart, tcur; +char ligne[100]; + +/* --- variables for select */ +struct timeval tv; +fd_set rfds; +int retval; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__, + glass, fd_local, fd_remote); +#endif + +tstart = time(NULL); + +wclear(glass); wrefresh(glass); + +do { + tcur = time(NULL); + + FD_ZERO(&rfds); + FD_SET(fd_local, &rfds); /* stdin */ + FD_SET(fd_remote, &rfds); /* teletype */ + tv.tv_sec = tv.tv_sec = 0; /* no timeout */ + + retval = select(2, &rfds, NULL, NULL, &tv); + + + wrefresh(glass); usleep(155*1000); + + } while (! flag_exit); + +sleep(1); + +return -1; +} +/* ---------------------------------------------------------------- */ +int run_the_terminal(int fd_serial, char *title, WINDOW *win) +{ +WINDOW *terminal, *ecran; +int wid_term, hei_term, lin_term, col_term; +int foo; +char ligne[100]; +unsigned char byte; +int fd_stdin; + +lin_term = col_term = 4; /* position */ +wid_term = 40; +hei_term = 25; + +fd_stdin = fileno(stdin); /* for select or pool */ + +terminal = newwin(hei_term, wid_term, lin_term, col_term); +if ( NULL==terminal ) return -1; +bordure(terminal, title, 1); + +ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1); +if ( NULL==ecran ) return -2; +scrollok(ecran, 1); + +if (verbosity) { + foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n"); +#if DEBUG_LEVEL > 1 + fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo); +#endif + wrefresh(ecran); sleep(2); + } + +foo = interactive(ecran, fd_stdin, fd_serial); + +sleep(1); + + +delwin(terminal); +touchwin(stdscr); wrefresh(stdscr); + +return 0; +} +/* ---------------------------------------------------------------- */ +int essai_terminal(int fd, char *txt, int k) +{ +int lig, col; +int foo, retv; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__, + fd, txt, k ); +#endif + +/*** initialisation de curses */ +initscr(); +nonl(); cbreak(); noecho(); + +for (lig=0; lig