My implementation of VT100 ESC-sequence utility
Embed:
(wiki syntax)
Show/hide line numbers
vt100.cpp
00001 #include "serial_api.h" 00002 #include "vt100.h" 00003 #include "mbed.h" 00004 00005 #define BLACK 0 00006 #define RED 1 00007 #define GREEN 2 00008 #define YELLOW 3 00009 #define BLUE 4 00010 #define PURPLE 5 00011 #define CIAN 6 00012 #define WHITE 7 00013 00014 char ESC = '\033' ; 00015 00016 extern serial_t stdio_uart ; 00017 vt100::vt100(int baud) : Serial(USBTX, USBRX, baud) 00018 { 00019 // extern serial_t stdio_uart ; 00020 // serial_baud(&stdio_uart, baud) ; 00021 } 00022 00023 vt100::vt100(PinName tx_pin, PinName rx_pin, int baud) :Serial(tx_pin, rx_pin, baud) 00024 { 00025 } 00026 00027 vt100::~vt100() 00028 { 00029 } 00030 00031 void vt100::cls(void) 00032 { 00033 printf("%c[2J", ESC) ; 00034 locate(1,1) ; 00035 } 00036 00037 void vt100::locate(int x, int y) 00038 { 00039 printf("%c[%d;%dH",ESC,y,x) ; 00040 } 00041 00042 void vt100::putChar(int x, int y, char c) 00043 { 00044 locate(x,y) ; 00045 printf("%c",c) ; 00046 } 00047 00048 void vt100::putStr(int x, int y, char *str) 00049 { 00050 locate(x,y) ; 00051 printf("%s", str) ; 00052 } 00053 00054 void vt100::line(int x1, int y1, int x2, int y2, char c) 00055 { 00056 int x, y, dx, dy, w, h, step ; 00057 dx = x2 - x1 ; 00058 dy = y2 - y1 ; 00059 w = (dx >= 0)? dx : -dx ; 00060 h = (dy >= 0)? dy : -dy ; 00061 00062 if (dx == 0) { /* vertical line */ 00063 step = (dy >= 0) ? 1 : -1 ; 00064 for (y = 0 ; y <= h ; y++) { 00065 putChar(x1, y1 + (step * y), c) ; 00066 } 00067 } else if (dy == 0) { /* Horizontal line */ 00068 step = (dx >= 0) ? 1 : -1 ; 00069 for (x = 0 ; x <= w ; x++) { 00070 putChar(x1 + (step * x), y1, c) ; 00071 } 00072 } else { 00073 if (w >= h) { /* use x as step */ 00074 step = (dx >= 0) ? 1 : -1 ; 00075 for (x = 0 ; x <= w ; x++ ) { 00076 putChar( x1 + step*x, y1 + ((2*x+1) * dy)/(2 * w), c) ; 00077 } 00078 } else { /* use y as step */ 00079 step = (dy >= 0) ? 1 : -1 ; 00080 for (y = 0 ; y <= h ; y++ ) { 00081 putChar( x1 + ((2*y+1) * dx)/(2*h), y1 + step*y, c) ; 00082 } 00083 } 00084 } 00085 } 00086 00087 /**************************************************** 00088 * frame(x1, y1, x2, y2) 00089 * draw textual frame 00090 * (x1,y1) (x2,y1) 00091 * +--------------------------+ 00092 * | | 00093 * +--------------------------+ 00094 * (x1,y2) (x2,y2) 00095 */ 00096 void vt100::frame(int x1, int y1, int x2, int y2) 00097 { 00098 int tmp ; 00099 if (x1 > x2) { 00100 tmp = x1 ; 00101 x1 = x2 ; 00102 x2 = tmp ; 00103 } 00104 if (y1 > y2) { 00105 tmp = y1 ; 00106 y1 = y2 ; 00107 y2 = tmp ; 00108 } 00109 putChar(x1, y1, '+') ; 00110 line(x1+1,y1,x2-1,y1, '-') ; 00111 putChar(x2, y1, '+') ; 00112 line(x2,y1+1,x2,y2-1, '|') ; 00113 putChar(x2, y2, '+') ; 00114 line(x2-1,y2,x1+1,y2, '-') ; 00115 putChar(x1, y2, '+') ; 00116 line(x1,y2-1,x1,y1+1, '|') ; 00117 } 00118 00119 /*************************************************** 00120 * circle(x, y, r, c) 00121 * Based on Jack Elton Bresenham's 00122 * Midpoint circle algorithm. 00123 * http://en.wikipedia.org/wiki/Midpoint_circle_algorithm 00124 */ 00125 void vt100::circle(int x0, int y0, int r, char c) 00126 { 00127 int f = 1 - r ; 00128 int dFx = 1 ; 00129 int dFy = -2 * r ; 00130 int x = 0 ; 00131 int y = r ; 00132 00133 putChar(x0, y0 + r, c) ; 00134 putChar(x0, y0 - r, c) ; 00135 putChar(x0 + 2*r, y0, c) ; 00136 putChar(x0 - 2*r, y0, c) ; 00137 00138 while(x < y) { 00139 if (f >= 0) { 00140 y-- ; 00141 dFy += 2 ; 00142 f += dFy ; 00143 } 00144 x++ ; 00145 dFx += 2 ; 00146 f += dFx ; 00147 putChar(x0 + 2*x, y0 + y, c) ; 00148 putChar(x0 - 2*x, y0 + y, c) ; 00149 putChar(x0 + 2*x, y0 - y, c) ; 00150 putChar(x0 - 2*x, y0 - y, c) ; 00151 putChar(x0 + 2*y, y0 + x, c) ; 00152 putChar(x0 - 2*y, y0 + x, c) ; 00153 putChar(x0 + 2*y, y0 - x, c) ; 00154 putChar(x0 - 2*y, y0 - x, c) ; 00155 } 00156 } 00157 00158 int vt100::setFG(int newFG) 00159 { 00160 int oldFG = foreground ; 00161 printf("\033[3%dm", newFG) ; 00162 foreground = newFG ; 00163 return( oldFG ) ; 00164 } 00165 00166 int vt100::getFG() 00167 { 00168 return( foreground ) ; 00169 } 00170 00171 int vt100::setBG(int newBG) 00172 { 00173 int oldBG = background ; 00174 printf("\033[4%dm", newBG) ; 00175 return( oldBG ) ; 00176 } 00177 00178 int vt100::getBG() 00179 { 00180 return( background ) ; 00181 } 00182 00183 void vt100::black() 00184 { 00185 setFG( BLACK ) ; 00186 } 00187 00188 void vt100::red() 00189 { 00190 setFG( RED ) ; 00191 } 00192 00193 void vt100::green() 00194 { 00195 setFG( GREEN ) ; 00196 } 00197 00198 void vt100::yellow() 00199 { 00200 setFG( YELLOW ) ; 00201 } 00202 00203 void vt100::blue() 00204 { 00205 setFG( BLUE ) ; 00206 } 00207 00208 void vt100::purple() 00209 { 00210 setFG( PURPLE ) ; 00211 } 00212 00213 void vt100::cian() 00214 { 00215 setFG( CIAN ) ; 00216 } 00217 00218 void vt100::white() 00219 { 00220 setFG( WHITE ) ; 00221 }
Generated on Sat Jul 30 2022 13:54:51 by
1.7.2