Browse Source

making a serial embeded terminal

phyto 6 months ago
parent
commit
40e1d434ff
4 changed files with 160 additions and 1 deletions
  1. 12
    0
      ui/Makefile
  2. 1
    1
      ui/t.c
  3. 141
    0
      ui/terminal.c
  4. 6
    0
      ui/terminal.h

+ 12
- 0
ui/Makefile View File

@@ -0,0 +1,12 @@
1
+
2
+CC	= gcc
3
+CCOPT	= -Wall -g -DDEBUG_LEVEL=1
4
+CLIB	= core/libdd2m-core.a viz/libdd2m-viz.a
5
+
6
+OSERIAL = ../serial/serial.o ../serial/funcs.o
7
+
8
+terminal.o:	terminal.c Makefile terminal.h
9
+	$(CC) ${CCOPT} $< -c 
10
+
11
+t:		t.c terminal.o
12
+	${CC} ${CCOPT} $< terminal.o ${OSERIAL} -lncurses -o $@

+ 1
- 1
ui/t.c View File

@@ -58,7 +58,7 @@ if (serial_in < 0) {
58 58
 sleep(1);
59 59
 
60 60
 foo = essai_terminal(serial_in, device, K);
61
-
61
+fprintf(stderr, "essai terminal -> %d\n", foo);
62 62
 return 0;
63 63
 }
64 64
 /* ---------------------------------------------------------------- */

+ 141
- 0
ui/terminal.c View File

@@ -0,0 +1,141 @@
1
+/*
2
+ *		emulateur de terminal
3
+ */
4
+ 
5
+#include  <stdio.h>
6
+#include  <string.h>
7
+#include <unistd.h>
8
+#include <stdlib.h>
9
+#include <sys/select.h>
10
+
11
+#include <ncurses.h>
12
+
13
+#include  "../serial/serial.h"
14
+#include  "terminal.h"
15
+
16
+extern int	verbosity;
17
+
18
+/* ---------------------------------------------------------------- */
19
+static void bordure(WINDOW * w, char *texte, int type)
20
+{
21
+if (type)
22
+	box(w, 0, 0);
23
+else
24
+	wborder(w, '|', '|', '-', '-', '+', '+', '+', '+');
25
+wstandout(w); mvwaddstr(w, 0, 3, texte); wstandend(w);
26
+wrefresh(w);
27
+}
28
+/* ---------------------------------------------------------------- */
29
+static int interactive(WINDOW *glass, int fd_local, int fd_remote)
30
+{
31
+int		flag_exit = 0;
32
+long		tstart, tcur;
33
+char		ligne[100];
34
+
35
+/* --- variables for select */
36
+struct timeval	tv;
37
+fd_set		rfds;
38
+int		retval;
39
+
40
+#if DEBUG_LEVEL
41
+fprintf(stderr, ">>> %s ( %p %d %d )\n", __func__,
42
+				glass, fd_local, fd_remote);
43
+#endif
44
+
45
+tstart = time(NULL);	
46
+
47
+wclear(glass);		wrefresh(glass);
48
+
49
+do	{
50
+	tcur = time(NULL);
51
+
52
+	FD_ZERO(&rfds);
53
+	FD_SET(fd_local, &rfds);	/* stdin */
54
+	FD_SET(fd_remote, &rfds);	/* teletype */
55
+	tv.tv_sec = tv.tv_sec = 0;	/* no timeout */
56
+
57
+	retval = select(2, &rfds, NULL, NULL, &tv);	
58
+
59
+
60
+	wrefresh(glass);	usleep(155*1000);
61
+
62
+	} while (! flag_exit);
63
+
64
+sleep(1);
65
+
66
+return -1;
67
+}
68
+/* ---------------------------------------------------------------- */
69
+int run_the_terminal(int fd_serial, char *title, WINDOW *win)
70
+{
71
+WINDOW		*terminal, *ecran;
72
+int		wid_term, hei_term, lin_term, col_term;
73
+int		foo;
74
+char		ligne[100];
75
+unsigned char	byte;
76
+int		fd_stdin;
77
+
78
+lin_term = col_term = 4;			/* position */
79
+wid_term = 40;
80
+hei_term = 25;
81
+
82
+fd_stdin = fileno(stdin);	/* for select or pool */
83
+
84
+terminal = newwin(hei_term, wid_term, lin_term, col_term);
85
+if ( NULL==terminal )	return -1;
86
+bordure(terminal, title, 1);
87
+
88
+ecran = subwin(terminal, hei_term-2, wid_term-2, lin_term+1, col_term+1);
89
+if ( NULL==ecran )	return -2;
90
+scrollok(ecran, 1);
91
+
92
+if (verbosity) {
93
+	foo = mvwaddstr(ecran, 2, 14, "SCREEN READY\n");
94
+#if DEBUG_LEVEL > 1
95
+	fprintf(stderr, "in '%s', mvwaddstr -> %d\n", __func__, foo);
96
+#endif
97
+	wrefresh(ecran);	sleep(2);
98
+	}
99
+
100
+foo = interactive(ecran, fd_stdin, fd_serial);
101
+
102
+sleep(1);
103
+
104
+
105
+delwin(terminal);
106
+touchwin(stdscr);			wrefresh(stdscr);
107
+
108
+return 0;
109
+}
110
+/* ---------------------------------------------------------------- */
111
+int essai_terminal(int fd, char *txt, int k)
112
+{
113
+int		lig, col;
114
+int		foo, retv;
115
+
116
+#if DEBUG_LEVEL
117
+fprintf(stderr, ">>> %s ( %d '%s' %d )\n", __func__,
118
+				fd, txt, k );
119
+#endif
120
+
121
+/*** initialisation de curses */				
122
+initscr();
123
+nonl();         cbreak();       noecho();
124
+
125
+for (lig=0; lig<LINES; lig++) {
126
+	for (col=0; col<COLS; col++) {
127
+		mvaddch(lig, col, "o+OX"[rand()%4]);	
128
+		}
129
+	}
130
+wrefresh(stdscr);
131
+
132
+/*** passons a la partie active */
133
+retv = run_the_terminal(fd, txt, stdscr);
134
+
135
+/*** terminaison de curses */
136
+endwin();       
137
+fprintf(stderr, "terminal return %d\n", retv);
138
+
139
+return 0;
140
+}
141
+/* ---------------------------------------------------------------- */

+ 6
- 0
ui/terminal.h View File

@@ -0,0 +1,6 @@
1
+/*** terminal emulation */
2
+
3
+int run_the_terminal(int fd, char *title, WINDOW *win);
4
+int essai_terminal(int fd, char *txt, int k);
5
+
6
+

Loading…
Cancel
Save