vt100.h
5.22 KB
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
/*
/ _____) _ | |
( (____ _____ ____ _| |_ _____ ____| |__
\____ \| ___ | (_ _) ___ |/ ___) _ \
_____) ) ____| | | || |_| ____( (___| | | |
(______/|_____)_|_|_| \__)_____)\____)_| |_|
(C)2015 Semtech
Description: VT100 terminal support class
License: Revised BSD License, see LICENSE.TXT file include in the project
Maintainer: Miguel Luis and Gregory Cristian
*/
#ifndef __VT100_H__
#define __VT100_H__
#ifndef STRING_STACK_LIMIT
#define STRING_STACK_LIMIT 120
#endif
/**
* Implements VT100 terminal commands support.
* Implments also the same behaviour has RawSerial class. The only difference
* is located in putc fucntion where writeable check is made befor sending the character.
*/
class VT100 : public SerialBase
{
public:
enum TextAttributes
{
ATTR_OFF = 0,
BOLD = 1,
USCORE = 4,
BLINK = 5,
REVERSE = 7,
BOLD_OFF = 21,
USCORE_OFF = 24,
BLINK_OFF = 25,
REVERSE_OFF = 27,
};
enum Colors
{
BLACK = 0,
RED = 1,
GREEN = 2,
BROWN = 3,
BLUE = 4,
MAGENTA = 5,
CYAN = 6,
WHITE = 7,
};
VT100( PinName tx, PinName rx ): SerialBase( tx, rx )
{
this->baud( 115200 );
// initializes terminal to "power-on" settings
// ESC c
this->printf( "\x1B\x63" );
}
void ClearScreen( uint8_t param )
{
// ESC [ Ps J
// 0 Clear screen from cursor down
// 1 Clear screen from cursor up
// 2 Clear entire screen
this->printf( "\x1B[%dJ", param );
}
void ClearLine( uint8_t param )
{
// ESC [ Ps K
// 0 Erase from the active position to the end of the line, inclusive (default)
// 1 Erase from the start of the screen to the active position, inclusive
// 2 Erase all of the line, inclusive
this->printf( "\x1B[%dK", param );
}
void SetAttribute( uint8_t attr )
{
// ESC [ Ps;...;Ps m
this->printf( "\x1B[%dm", attr );
}
void SetAttribute( uint8_t attr, uint8_t fgcolor, uint8_t bgcolor )
{
// ESC [ Ps;...;Ps m
this->printf( "\x1B[%d;%d;%dm", attr, fgcolor + 30, bgcolor + 40 );
}
void SetCursorMode( uint8_t visible )
{
if( visible == true )
{
// ESC [ ? 25 h
this->printf( "\x1B[?25h" );
}
else
{
// ESC [ ? 25 l
this->printf( "\x1B[?25l" );
}
}
void SetCursorPos( uint8_t line, uint8_t col )
{
// ESC [ Pl ; Pc H
this->printf( "\x1B[%d;%dH", line, col );
}
void PutStringAt( uint8_t line, uint8_t col, const char *s )
{
this->SetCursorPos( line, col );
this->printf( "%s", s );
}
void PutCharAt( uint8_t line, uint8_t col, uint8_t c )
{
this->SetCursorPos( line, col );
this->printf( "%c", c );
}
void PutHexAt( uint8_t line, uint8_t col, uint16_t n )
{
this->SetCursorPos( line, col );
this->printf( "%X", n );
}
void PutBoxDrawingChar( uint8_t c )
{
this->printf( "\x1B(0%c\x1b(B", c );
}
bool Readable( void )
{
return this->readable( );
}
uint8_t GetChar( void )
{
return this->getc( );
}
/*
* RawSerial class implmentation copy.
*/
/** Read a char from the serial port
*
* @returns The char read from the serial port
*/
int getc( )
{
return _base_getc();
}
/** Write a char to the serial port
*
* @param c The char to write
*
* @returns The written char or -1 if an error occured
*/
int putc( int c )
{
while( this->writeable( ) != 1 );
return _base_putc( c );
}
/** Write a string to the serial port
*
* @param str The string to write
*
* @returns 0 if the write succeeds, EOF for error
*/
int puts( const char *str )
{
while( *str )
putc( *str++ );
return 0;
}
// Experimental support for printf in RawSerial. No Stream inheritance
// means we can't call printf() directly, so we use sprintf() instead.
// We only call malloc() for the sprintf() buffer if the buffer
// length is above a certain threshold, otherwise we use just the stack.
int printf( const char *format, ... )
{
std::va_list arg;
va_start( arg, format );
int len = vsnprintf( NULL, 0, format, arg );
if( len < STRING_STACK_LIMIT )
{
char temp[STRING_STACK_LIMIT];
vsprintf( temp, format, arg );
puts( temp );
}
else
{
char *temp = new char[len + 1];
vsprintf( temp, format, arg );
puts( temp );
delete[] temp;
}
va_end( arg );
return len;
}
private:
};
#endif // __VT100_H__