-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.cpp
170 lines (150 loc) · 2.74 KB
/
screen.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
#include <Arduino.h>
#include <ports.h>
#include <memory.h>
#include <CPU.h>
#include <display.h>
#include <serial_dsp.h>
#include <hardware.h>
#include "config.h"
#include "screen.h"
#include "io.h"
#define ROWS 24
#define COLS 80
static unsigned r, c;
static unsigned rows, cols;
static char buf[ROWS][COLS];
void screen::clear() {
r = c = 0;
for (unsigned j = 0; j < ROWS; j++)
for (unsigned i = 0; i < COLS; i++)
buf[j][i] = ' ';
Display::clear();
}
void screen::reset() {
_esc = _ansi = false;
_line = _value = 0;
Display::begin(BG_COLOUR, FG_COLOUR, ORIENT);
rows = screenHeight() / charHeight();
if (rows > ROWS) rows = ROWS;
cols = screenWidth() / charWidth();
if (cols > COLS) cols = COLS;
DBG(printf("screen %ux%u\r\n", cols, rows));
Display::setScreen(cols * charWidth(), rows * charHeight());
clear();
}
void screen::draw(char ch, unsigned i, unsigned j) {
if (buf[j][i] != ch) {
char s[] = { ch, 0 };
Display::drawString(s, i * charWidth(), j * charHeight());
buf[j][i] = ch;
}
}
void screen::write(uint8_t b) {
char ch = (char)b;
switch(ch) {
case 0x08: // '\b'
draw(' ', c, r);
if (c-- == 0) {
r--;
c = cols-1;
}
break;
case 0x0d: // '\r'
draw(' ', c, r);
c = 0;
return; // no prompt
case 0x0a: // '\n'
r++;
break;
case 0x1b: // esc
_esc = true;
return;
default:
if (_esc) {
_esc = false;
if (ch == '[') {
_ansi = true;
return;
}
}
if (_ansi) {
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
_value = _value * 10 + ch - '0';
return;
case ';':
_line = _value;
_value = 0;
return;
case 'K':
for (unsigned i = c; i < cols; i++)
draw(' ', i, r);
break;
case 'J':
if (_value == 2)
clear();
break;
case 'A':
draw(' ', c, r);
r -= _value;
break;
case 'B':
draw(' ', c, r);
r += _value;
break;
case 'C':
draw(' ', c, r);
c += _value;
break;
case 'D':
draw(' ', c, r);
c -= _value;
break;
case 'H':
draw(' ', c, r);
r = _line;
c = _value;
break;
default:
// ???
DBG(println(ch));
break;
}
if (r < 0) r = 0;
else if (r > rows-1) r = rows-1;
if (c < 0) c = 0;
if (c > cols-1) c = cols-1;
_value = _line = 0;
_ansi = false;
} else {
_esc = _ansi = false;
_line = _value = 0;
if (ch >= 0x20 && ch < 0x7f) {
draw(ch, c, r);
if (++c == cols) {
c = 0;
r++;
}
}
}
}
if (r == rows) {
// scroll
r--;
for (unsigned j = 0; j < (rows-1); j++)
for (unsigned i = 0; i < cols; i++)
draw(buf[j+1][i], i, j);
for (unsigned i = 0; i < cols; i++)
draw(' ', i, r);
}
draw('_', c, r);
}