forked from nottwo/BasicTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VTerm.cpp
197 lines (168 loc) · 4.02 KB
/
VTerm.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
/*
* BasicTerm.cpp
* Provides basic ANSI/VT220 terminal control over a serial interface
* Copyright 2011 Trannie Carter <[email protected]>
* Licensed for use under the terms of the GNU Lesser General Public License v3
*/
#include "VTerm.h"
#include "VT100.h"
VTerm::VTerm(Stream *ser) {
serial = ser;
}
int VTerm::available(void) {
return serial->available();
}
int VTerm::peek(void) {
return serial->peek();
}
int VTerm::read(void) {
return serial->read();
}
void VTerm::flush(void) {
serial->flush();
}
size_t VTerm::write(uint8_t c) {
uint8_t ret;
if(c & BT_ALTCHAR) {
serial->print(BT_START_ALTCHAR);
ret = serial->write(c & ~BT_ALTCHAR);
serial->print(BT_END_ALTCHAR);
return ret;
}
return serial->write(c);
}
void VTerm::init(void) {
serial->print(RIS);
}
void VTerm::cls(void) {
serial->print(CLS);
}
void VTerm::home(void) {
serial->print(HOME);
}
void VTerm::up(char x) {
serial->print(CSI);
serial->print(x,DEC);
serial->write('A');
}
void VTerm::down(char x) {
serial->print(CSI);
serial->print(x,DEC);
serial->write('B');
}
void VTerm::forward(char x) {
serial->print(CSI);
serial->print(x,DEC);
serial->write('C');
}
void VTerm::backward(char x) {
serial->print(CSI);
serial->print(x,DEC);
serial->write('D');
}
void VTerm::eraseLine(void) {
serial->print(EL2);
}
void VTerm::eraseScreen(void) {
serial->print(ED2);
}
void VTerm::fill(char x1, char y1, char x2, char y2) {
for (char x = x1; x <= x2; x++)
{
for (char y = y1; y <= y2; y++)
{
position(x,y);
serial->print(' ');
}
}
}
void VTerm::position(uint8_t row, uint8_t col) {
serial->print(CSI);
serial->print((uint8_t)row + 1);
serial->print(F(";"));
serial->print((uint8_t)col + 1);
serial->print(F("H"));
}
void VTerm::show_cursor(boolean show) {
if(show) {
serial->print(SHOW_CURSOR);
} else {
serial->print(HIDE_CURSOR);
}
}
int16_t VTerm::get_key(void) {
int16_t key;
uint16_t when;
key = serial->read();
if(key == ESC_CHAR) { /* escape sequence */
when = millis();
while(serial->available() < 2) {
if(((uint16_t) millis() - when) > 1000) {
return key;
}
}
key = serial->read();
switch(key) {
case '[':
key = serial->read();
switch(key) {
case 'A':
return BT_KEY_UP;
case 'B':
return BT_KEY_DOWN;
case 'C':
return BT_KEY_RIGHT;
case 'D':
return BT_KEY_LEFT;
default:
return BT_KEY_UNKNOWN;
}
break;
case 'O':
key = serial->read();
switch(key) {
case 'P':
return BT_KEY_F(1);
case 'Q':
return BT_KEY_F(2);
case 'R':
return BT_KEY_F(3);
case 'S':
return BT_KEY_F(4);
default:
return BT_KEY_UNKNOWN;
}
break;
default:
return BT_KEY_UNKNOWN;
}
}
return key;
}
void VTerm::set_attribute(uint8_t attr) {
if(attr & BT_REVERSE) {
serial->print(REVERSE);
}
if(attr & BT_UNDERLINE) {
serial->print(UNDERLINE);
}
if(attr & BT_BOLD) {
serial->print(BOLD);
}
if(attr & BT_BLINK) {
serial->print(BLINK);
}
if(attr == BT_NORMAL) {
serial->print(OFF);
}
}
void VTerm::set_color(uint8_t fg, uint8_t bg) {
serial->print(CSI);
serial->print(30 + fg);
serial->print(";");
serial->print(40 + bg);
serial->print("m");
}
void VTerm::beep(void) {
serial->print(BELL_CHAR);
}