forked from mkschreder/avr-vt100
-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.cpp
574 lines (501 loc) · 14.3 KB
/
demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
/**
This is a demo for the vt100 functionality.
To do the tests, you may have to comment out some of them because all
the strings take up a lot of ram. :)
Copyright: Martin K. Schröder ([email protected]) 2014/10/27
*/
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "uart.h"
#include "ili9340.h"
#include "vt100.h"
/**
Tests following commands:
Cursor movement (<n>=how many chars or lines), cursor stop at margin.
Up
Esc [ <n> A
033 133 101
Down
Esc [ <n> B
033 133 102
Right
Esc [ <n> C
033 133 103
Left
Esc [ n D
033 133 104
Cursor position (<n1>=y,<n2>=x, from top of screen or scroll region)
Esc [ <n1> ; <n2> H
033 133 073 110
Or Esc [ <n1> ; <n2> f
033 133 073 146
Index (cursor down with scroll up when at margin)
Esc D
033 104
Reverse index (cursor up with scroll down when at margin)
Esc M
033 115
Next line (CR+Index)
Esc E
033 105
Save cursor and attribute
Esc 7
033 067
Restore cursor and attribute
Esc 8
033 070
*/
void test_cursor(){
char buf[16];
// clear screen
vt100_puts("\e[c\e[2J\e[m\e[r\e[?6l\e[1;1H");
// draw a line of "*"
for(int c = 0; c < VT100_WIDTH; c++){
vt100_putc('*');
}
// draw left and right border
for(int c = 0; c < VT100_HEIGHT; c++){
sprintf(buf, "\e[%d;1H*\e[%d;%dH*", c + 1, c + 1, VT100_WIDTH);
vt100_puts(buf);
}
// draw bottom line
sprintf(buf, "\e[%d;1H", VT100_HEIGHT);
vt100_puts(buf);
for(int c = 0; c < VT100_WIDTH; c++){
vt100_putc('*');
}
// draw inner border of +
vt100_puts("\e[2;2H");
// draw a line of "*"
for(int c = 0; c < VT100_WIDTH - 2; c++){
vt100_putc('+');
}
// draw left and right border
for(int c = 1; c < VT100_HEIGHT - 1; c++){
sprintf(buf, "\e[%d;2H+\e[%d;%dH+", c + 1, c + 1, VT100_WIDTH - 1);
vt100_puts(buf);
}
// draw bottom line
sprintf(buf, "\e[%d;2H", VT100_HEIGHT - 1);
vt100_puts(buf);
for(int c = 0; c < VT100_WIDTH - 2; c++){
vt100_putc('+');
}
// draw middle window
// EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
// E E
// E This must be an unbroken a E
// E rea of text with 1 free bo E
// E rder around the text. E
// E E
// EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
vt100_puts("\e[10;6H");
for(int c = 0; c < 30; c++){
vt100_putc('E');
}
// test normal movement
vt100_puts("\e[11;6H");
// test cursor store and restore...
vt100_puts("\e7\e[35;10H\e8");
vt100_puts("E\e[11;35HE");
// goto 12;6, print E, move cursor 29 (already moved +1) to right and print E
vt100_puts("\e[12;6HE\e[28CE");
// move cursor 31 to left, 1 down, print E, move 30 right, print E
vt100_puts("\e[30D\e[BE\e[28CE");
vt100_puts("\e[15;6H\e[AE\e[28CE");
vt100_puts("\e[15;6HE\e[15;35HE");
vt100_puts("\e[16;6H");
for(int c = 0; c < 30; c++){
vt100_putc('E');
}
const char *text[] = {"This must be an unbroken a", "rea of text with 1 free bo", "rder around the text. "};
for(int c = 0; c < 3; c++){
sprintf(buf, "\e[%d;8H", c + 12);
vt100_puts(buf);
vt100_puts(text[c]);
}
// now lets draw two parallel columns of Es
vt100_puts("\e[20;19H");
for(int c = 0; c < 10; c++){
// draw E (cursor moves right), step one right, draw F, step 3 left and 1 down
vt100_puts("E\e[1CF\e[3D\e[B");
}
// Test index (escD - down with scroll)
// Test reverse index (escM)
// next line (escE) - cr + index
// save and restore cursor
// move to last line and scroll down 8 lines
vt100_puts("\e[40;1H");
for(int c = 0; c < 7; c++){
vt100_puts("\eD");
}
_delay_ms(100);
// now scroll same number of lines back and then back again (to test up scroll)
vt100_puts("\e[1;1H");
for(int c = 0; c < 7; c++){
vt100_puts("\eM");
}
_delay_ms(100);
vt100_puts("\e[40;1H");
for(int c = 0; c < 7; c++){
vt100_puts("\eD");
}
// we now have the Es at the third line (or we SHOULD have)
// refill the top border and clear bottom borders
for(int c = 1; c < VT100_WIDTH - 1; c++){
// we print * then move down and left, print + and go back right and top
// (good way to test cursor navigation keys)
sprintf(buf, "\e[1;%dH*\e[B\e[D+\e[A", c + 1);
vt100_puts(buf);
}
// clear the border that scrolled up
for(int c = 2; c < VT100_WIDTH - 2; c++){
// space, down, left, space, up
sprintf(buf, "\e[32;%dH \e[B\e[D \e[A", c + 1);
vt100_puts(buf);
}
// redraw left and right border
for(int c = 1; c < VT100_HEIGHT; c++){
sprintf(buf, "\e[%d;1H*+\e[%d;%dH+*", c + 1, c + 1, VT100_WIDTH - 1);
vt100_puts(buf);
}
// fill border at the bottom
for(int c = 1; c < VT100_WIDTH - 1; c++){
sprintf(buf, "\e[39;%dH+\e[B\e[D*\e[A", c + 1);
vt100_puts(buf);
}
// draw the explanation string
vt100_puts("\e[30;6HShould see two columns of E F");
vt100_puts("\e[31;6HText box must start at line 3");
}
/**
Tests setting scroll region and moving the cursor inside the scroll region.
*/
void test_scroll(){
char buf[16];
// reset terminal and clear screen. Cursor at 1;1.
vt100_puts("\e[c\e[2J\e[m\e[r\e[?6l\e[1;1H");
// set top margin 3 lines, bottom margin 5 lines
vt100_puts("\e[4;36r");
// draw top and bottom windows
vt100_puts("\e[1;1H#\e[2;1H#\e[3;1H#\e[1;40H#\e[2;40H#\e[3;40H#");
vt100_puts("\e[36;1H#\e[37;1H#\e[38;1H#\e[39;1H#\e[40;1H#");
vt100_puts("\e[36;40H#\e[37;40H#\e[38;40H#\e[39;40H#\e[40;40H#");
vt100_puts("\e[1;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
vt100_puts("\e[3;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
vt100_puts("\e[36;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
vt100_puts("\e[40;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
// print some text that should not move
vt100_puts("\e[2;4HThis is top text (should not move)");
vt100_puts("\e[38;3HThis is bottom text (should not move)");
// set origin mode and print border around the scroll region
vt100_puts("\e[?6h");
vt100_puts("\e[1;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('!');
// origin mode should snap 99 to last line in scroll region
vt100_puts("\e[99;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('!');
for(int y = 0; y < VT100_HEIGHT; y++){
//sprintf(buf, "\e[%d;1H!\e[%d;%dH!", y+1, y+1, VT100_WIDTH);
sprintf(buf, "\e[%d;1H", y+1);
vt100_puts(buf);
for(int c = 0; c < VT100_WIDTH; c++){
vt100_putc('!');
}
}
// scroll the scroll region
vt100_puts("\e[99;1H\eD\eD");
vt100_puts("\e[1;1H\eM\eM");
vt100_puts("\e[99;1H\eD");
// clear out an area in the middle and draw text
for(int y = 0; y < 5; y++){
sprintf(buf, "\e[%d;6H", y+10);
vt100_puts(buf);
for(int c = 0; c < 30; c++){
vt100_putc(' ');
}
}
vt100_puts("\e[11;10HMust be ! filled with 2");
vt100_puts("\e[12;10H empty lines at");
vt100_puts("\e[13;10H top and bottom! ");
}
/**
Erasing
Erase in line
End of line (including cursor position)
Esc [ K
033 133 113
Or Esc [ 0 K
033 133 060 113
Beginning of line (including cursor position)
Esc [ 1 K
033 133 061 113
Complete line
Esc [ 2 K
033 133 062 113
Erase in display
End of screen (including cursor position)
Esc [ J
033 133 112
Or Esc [ 0 J
033 133 060 112
Beginning of screen (including cursor position)
Esc [ 1 J
033 133 061 112
Complete display
Esc [ 2 J
033 133 062 112
Computer editing
Delete characters (<n> characters right from cursor
Esc [ <n> P
033 133 120
Insert line (<n> lines)
Esc [ <n> L
033 133 114
Delete line (<n> lines)
Esc [ <n> M
033 133 115
*/
void test_edit(){
char buf[32];
// clear screen
vt100_puts("\e[c\e[2J\e[m\e[r\e[?6l\e[1;1H");
// enable auto wrap mode
vt100_puts("\e[?7h");
// fill entire screen with 'x'
int size = (VT100_WIDTH * VT100_HEIGHT);
for(int c = 0; c < size; c++){
vt100_putc('x');
}
// clear bottom and top halves (will remove all 'x' s)
vt100_puts("\e[20;1H");
vt100_puts("\e[J");
_delay_ms(1000);
vt100_puts("\e[1J");
vt100_puts("\e[?7l");
_delay_ms(1000);
// draw left and right borders using erase function
for(int c = 29; c < VT100_HEIGHT; c+=2){
sprintf(buf, "\e[%d;1H", c + 1);
vt100_puts(buf);
// draw two lines of *
for(int j = 0; j < VT100_WIDTH; j++){
vt100_puts("*\e[B\e[D*\e[A");
}
// erase end of first line and beginning of second line
// goto c;3, erase end, goto c;(w-1), write **
sprintf(buf, "\e[%d;3H\e[0K\e[%d;%dH**",
c + 1, c + 1, VT100_WIDTH - 1);
vt100_puts(buf);
// goto (c+1);(width-2), erase beginning of line, goto (c+2);1, write **
sprintf(buf, "\e[%d;%dH\e[1K\e[%d;1H**",
c + 2, VT100_WIDTH - 2, c + 2);
vt100_puts(buf);
}
// fill border at the bottom
for(int c = 2; c < VT100_WIDTH - 2; c++){
sprintf(buf, "\e[30;%dH*\e[B\e[D*\e[A", c + 1);
vt100_puts(buf);
}
// fill border at the bottom
for(int c = 2; c < VT100_WIDTH - 2; c++){
sprintf(buf, "\e[39;%dH*\e[B\e[D*\e[A", c + 1);
vt100_puts(buf);
}
// draw text
vt100_puts("\e[35;4HYou should see border and NO x:s");
}
/**
Tests terminal colors
(i just copy the scroll example here and add some colors)
Esc[Value;...;Valuem Set Graphics Mode:
Calls the graphics functions specified by the following values. These specified functions remain active until the next occurrence of this escape sequence. Graphics mode changes the colors and attributes of text (such as bold and underline) displayed on the screen.
Text attributes
0 All attributes off
1 Bold on (we currently don't support bold on this limited display)
4 Underscore (not supported)
5 Blink on (not supported)
7 Reverse video on (not supported)
8 Concealed on (not supported)
Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
Parameters 30 through 47 meet the ISO 6429 standard.
*/
void test_colors(){
char buf[32];
// reset terminal and clear screen. Cursor at 1;1.
// reset all modes
vt100_puts("\e[c\e[2J\e[m\e[r\e[?6l\e[1;1H");
// set top margin 3 lines, bottom margin 5 lines
vt100_puts("\e[4;36r");
// set bg color to red and text to white
vt100_puts("\e[41;37m");
// draw top and bottom windows
vt100_puts("\e[1;1H#\e[2;1H#\e[3;1H#\e[1;40H#\e[2;40H#\e[3;40H#");
vt100_puts("\e[1;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
vt100_puts("\e[3;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
// background blue
vt100_puts("\e[44;37m");
vt100_puts("\e[36;1H#\e[37;1H#\e[38;1H#\e[39;1H#\e[40;1H#");
vt100_puts("\e[36;40H#\e[37;40H#\e[38;40H#\e[39;40H#\e[40;40H#");
vt100_puts("\e[36;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
vt100_puts("\e[40;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('#');
// foreground white
vt100_puts("\e[37;40m");
// print some text that should not move
vt100_puts("\e[2;4HThis is top text (should not move)");
vt100_puts("\e[38;3HThis is bottom text (should not move)");
// green background, black text
vt100_puts("\e[42;30m");
// set origin mode and print border around the scroll region
vt100_puts("\e[?6h");
vt100_puts("\e[1;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('!');
// origin mode should snap 99 to last line in scroll region
vt100_puts("\e[99;1H"); for(int x = 0; x < VT100_WIDTH; x++) vt100_putc('!');
for(int y = 0; y < VT100_HEIGHT; y++){
//sprintf(buf, "\e[%d;1H!\e[%d;%dH!", y+1, y+1, VT100_WIDTH);
sprintf(buf, "\e[%d;1H", y+1);
vt100_puts(buf);
for(int c = 0; c < VT100_WIDTH; c++){
vt100_putc('!');
}
}
// scroll the scroll region
vt100_puts("\e[99;1H\eD\eD");
vt100_puts("\e[1;1H\eM\eM");
vt100_puts("\e[99;1H\eD");
// black background, yellow text
vt100_puts("\e[33;40m");
// clear out an area in the middle and draw text
for(int y = 0; y < 5; y++){
sprintf(buf, "\e[%d;6H", y+10);
vt100_puts(buf);
for(int c = 0; c < 30; c++){
vt100_putc(' ');
}
}
vt100_puts("\e[11;10HMust be ! filled with 2");
vt100_puts("\e[12;10H empty lines at");
vt100_puts("\e[13;10H top and bottom! ");
}
int main(int argc, char **argv){
uart_init(UART_BAUD_SELECT(38400, F_CPU));
ili9340_init();
ili9340_setRotation(0);
auto respond = [=](char *str){
uart_puts(str);
};
vt100_init(respond);
// just clear the screen initially
for(int c = 0; c < 320; c++){
ili9340_drawFastHLine(0, c, 320, 0x0000);
}
sei();
// reset terminal and disable auto wrap
vt100_puts("\ec\e[?7l");
/*
while(1){
test_colors();
_delay_ms(5000);
test_cursor();
_delay_ms(5000);
test_edit();
_delay_ms(5000);
test_scroll();
_delay_ms(5000);
}*/
/*
while(1){
// clear display
for(int c = 0; c < 320; c++){
ili9340_drawFastHLine(0, c, 320, 0x0000);
}
// draw 40 lines
for(int l = 0; l < 40; l++){
for(int c = 0; c < 40; c++){
vt100_putc('0'+l);
}
if(l < 39) vt100_puts("\r\n");
}
_delay_ms(2000);
// set scroll region starting at line 4 to 37
vt100_puts("\e[4;40r\e[38;1HWE ARE HERE\r");
for(int c = 0; c < 10; c++){
vt100_putc('0'+c);
vt100_puts("NOW HERE!\r\n");
_delay_ms(2000);
}
vt100_puts("\e[5;1H");
for(int c = 0; c < 40; c++){
vt100_putc('#');
}
_delay_ms(2000);
}
while(1){
ili9340_setScrollStart(0);
ili9340_setScrollMargins(0, 0);
// clear display
for(int c = 0; c < 320; c++){
ili9340_drawFastHLine(0, c, 320, 0x0000);
}
// draw 40 lines
for(int l = 0; l < 39; l++){
for(int c = 0; c < 40; c++){
uint16_t x = c * 6;
uint16_t y = l * 8;
ili9340_drawChar(x, y, '0'+l);
}
}
_delay_ms(2000);
ili9340_setScrollMargins(32, 32);
_delay_ms(2000);
for(int c = 5; c < 7; c++){
ili9340_setScrollStart(c * 8);
_delay_ms(2000);
}
for(int c = 0; c < 40; c++){
uint16_t x = c * 6;
// y = line * char_height + scroll_start - margin_top
uint16_t y = 4 * 8 + 6 * 8 - 32;
ili9340_drawChar(x, y, '#');
}
_delay_ms(2000);
}*/
while(1){
unsigned int data = uart_getc();
if(data == UART_NO_DATA) continue;
if(data == 0xb4){ // ´ key on my kb
test_colors();
_delay_ms(5000);
test_cursor();
_delay_ms(5000);
test_edit();
_delay_ms(5000);
test_scroll();
_delay_ms(5000);
}
vt100_putc(data);
//uart_putc(data);
}
return 0;
}