My implementation of VT100 ESC-sequence utility
Fork of vt100 by
vt100.cpp@0:94253645a02a, 2014-12-01 (annotated)
- Committer:
- Rhyme
- Date:
- Mon Dec 01 12:48:02 2014 +0000
- Revision:
- 0:94253645a02a
The first version with comments;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:94253645a02a | 1 | #include "serial_api.h" |
Rhyme | 0:94253645a02a | 2 | #include "vt100.h" |
Rhyme | 0:94253645a02a | 3 | #include "mbed.h" |
Rhyme | 0:94253645a02a | 4 | |
Rhyme | 0:94253645a02a | 5 | #define BLACK 0 |
Rhyme | 0:94253645a02a | 6 | #define RED 1 |
Rhyme | 0:94253645a02a | 7 | #define GREEN 2 |
Rhyme | 0:94253645a02a | 8 | #define YELLOW 3 |
Rhyme | 0:94253645a02a | 9 | #define BLUE 4 |
Rhyme | 0:94253645a02a | 10 | #define PURPLE 5 |
Rhyme | 0:94253645a02a | 11 | #define CIAN 6 |
Rhyme | 0:94253645a02a | 12 | #define WHITE 7 |
Rhyme | 0:94253645a02a | 13 | |
Rhyme | 0:94253645a02a | 14 | char ESC = '\033' ; |
Rhyme | 0:94253645a02a | 15 | |
Rhyme | 0:94253645a02a | 16 | vt100::vt100(int baud) |
Rhyme | 0:94253645a02a | 17 | { |
Rhyme | 0:94253645a02a | 18 | extern serial_t stdio_uart ; |
Rhyme | 0:94253645a02a | 19 | serial_baud(&stdio_uart, baud) ; |
Rhyme | 0:94253645a02a | 20 | } |
Rhyme | 0:94253645a02a | 21 | |
Rhyme | 0:94253645a02a | 22 | vt100::~vt100() |
Rhyme | 0:94253645a02a | 23 | { |
Rhyme | 0:94253645a02a | 24 | } |
Rhyme | 0:94253645a02a | 25 | |
Rhyme | 0:94253645a02a | 26 | void vt100::cls(void) |
Rhyme | 0:94253645a02a | 27 | { |
Rhyme | 0:94253645a02a | 28 | printf("%c[2J", ESC) ; |
Rhyme | 0:94253645a02a | 29 | locate(1,1) ; |
Rhyme | 0:94253645a02a | 30 | } |
Rhyme | 0:94253645a02a | 31 | |
Rhyme | 0:94253645a02a | 32 | void vt100::locate(int x, int y) |
Rhyme | 0:94253645a02a | 33 | { |
Rhyme | 0:94253645a02a | 34 | printf("%c[%d;%dH",ESC,y,x) ; |
Rhyme | 0:94253645a02a | 35 | } |
Rhyme | 0:94253645a02a | 36 | |
Rhyme | 0:94253645a02a | 37 | void vt100::putChar(int x, int y, char c) |
Rhyme | 0:94253645a02a | 38 | { |
Rhyme | 0:94253645a02a | 39 | locate(x,y) ; |
Rhyme | 0:94253645a02a | 40 | printf("%c",c) ; |
Rhyme | 0:94253645a02a | 41 | } |
Rhyme | 0:94253645a02a | 42 | |
Rhyme | 0:94253645a02a | 43 | void vt100::putStr(int x, int y, char *str) |
Rhyme | 0:94253645a02a | 44 | { |
Rhyme | 0:94253645a02a | 45 | locate(x,y) ; |
Rhyme | 0:94253645a02a | 46 | printf("%s", str) ; |
Rhyme | 0:94253645a02a | 47 | } |
Rhyme | 0:94253645a02a | 48 | |
Rhyme | 0:94253645a02a | 49 | void vt100::line(int x1, int y1, int x2, int y2, char c) |
Rhyme | 0:94253645a02a | 50 | { |
Rhyme | 0:94253645a02a | 51 | int x, y, dx, dy, w, h, step ; |
Rhyme | 0:94253645a02a | 52 | dx = x2 - x1 ; |
Rhyme | 0:94253645a02a | 53 | dy = y2 - y1 ; |
Rhyme | 0:94253645a02a | 54 | w = (dx >= 0)? dx : -dx ; |
Rhyme | 0:94253645a02a | 55 | h = (dy >= 0)? dy : -dy ; |
Rhyme | 0:94253645a02a | 56 | |
Rhyme | 0:94253645a02a | 57 | if (dx == 0) { /* vertical line */ |
Rhyme | 0:94253645a02a | 58 | step = (dy >= 0) ? 1 : -1 ; |
Rhyme | 0:94253645a02a | 59 | for (y = 0 ; y <= h ; y++) { |
Rhyme | 0:94253645a02a | 60 | putChar(x1, y1 + (step * y), c) ; |
Rhyme | 0:94253645a02a | 61 | } |
Rhyme | 0:94253645a02a | 62 | } else if (dy == 0) { /* Horizontal line */ |
Rhyme | 0:94253645a02a | 63 | step = (dx >= 0) ? 1 : -1 ; |
Rhyme | 0:94253645a02a | 64 | for (x = 0 ; x <= w ; x++) { |
Rhyme | 0:94253645a02a | 65 | putChar(x1 + (step * x), y1, c) ; |
Rhyme | 0:94253645a02a | 66 | } |
Rhyme | 0:94253645a02a | 67 | } else { |
Rhyme | 0:94253645a02a | 68 | if (w >= h) { /* use x as step */ |
Rhyme | 0:94253645a02a | 69 | step = (dx >= 0) ? 1 : -1 ; |
Rhyme | 0:94253645a02a | 70 | for (x = 0 ; x <= w ; x++ ) { |
Rhyme | 0:94253645a02a | 71 | putChar( x1 + step*x, y1 + ((2*x+1) * dy)/(2 * w), c) ; |
Rhyme | 0:94253645a02a | 72 | } |
Rhyme | 0:94253645a02a | 73 | } else { /* use y as step */ |
Rhyme | 0:94253645a02a | 74 | step = (dy >= 0) ? 1 : -1 ; |
Rhyme | 0:94253645a02a | 75 | for (y = 0 ; y <= h ; y++ ) { |
Rhyme | 0:94253645a02a | 76 | putChar( x1 + ((2*y+1) * dx)/(2*h), y1 + step*y, c) ; |
Rhyme | 0:94253645a02a | 77 | } |
Rhyme | 0:94253645a02a | 78 | } |
Rhyme | 0:94253645a02a | 79 | } |
Rhyme | 0:94253645a02a | 80 | } |
Rhyme | 0:94253645a02a | 81 | |
Rhyme | 0:94253645a02a | 82 | /**************************************************** |
Rhyme | 0:94253645a02a | 83 | * frame(x1, y1, x2, y2) |
Rhyme | 0:94253645a02a | 84 | * draw textual frame |
Rhyme | 0:94253645a02a | 85 | * (x1,y1) (x2,y1) |
Rhyme | 0:94253645a02a | 86 | * +--------------------------+ |
Rhyme | 0:94253645a02a | 87 | * | | |
Rhyme | 0:94253645a02a | 88 | * +--------------------------+ |
Rhyme | 0:94253645a02a | 89 | * (x1,y2) (x2,y2) |
Rhyme | 0:94253645a02a | 90 | */ |
Rhyme | 0:94253645a02a | 91 | void vt100::frame(int x1, int y1, int x2, int y2) |
Rhyme | 0:94253645a02a | 92 | { |
Rhyme | 0:94253645a02a | 93 | int tmp ; |
Rhyme | 0:94253645a02a | 94 | if (x1 > x2) { |
Rhyme | 0:94253645a02a | 95 | tmp = x1 ; |
Rhyme | 0:94253645a02a | 96 | x1 = x2 ; |
Rhyme | 0:94253645a02a | 97 | x2 = tmp ; |
Rhyme | 0:94253645a02a | 98 | } |
Rhyme | 0:94253645a02a | 99 | if (y1 > y2) { |
Rhyme | 0:94253645a02a | 100 | tmp = y1 ; |
Rhyme | 0:94253645a02a | 101 | y1 = y2 ; |
Rhyme | 0:94253645a02a | 102 | y2 = tmp ; |
Rhyme | 0:94253645a02a | 103 | } |
Rhyme | 0:94253645a02a | 104 | putChar(x1, y1, '+') ; |
Rhyme | 0:94253645a02a | 105 | line(x1+1,y1,x2-1,y1, '-') ; |
Rhyme | 0:94253645a02a | 106 | putChar(x2, y1, '+') ; |
Rhyme | 0:94253645a02a | 107 | line(x2,y1+1,x2,y2-1, '|') ; |
Rhyme | 0:94253645a02a | 108 | putChar(x2, y2, '+') ; |
Rhyme | 0:94253645a02a | 109 | line(x2-1,y2,x1+1,y2, '-') ; |
Rhyme | 0:94253645a02a | 110 | putChar(x1, y2, '+') ; |
Rhyme | 0:94253645a02a | 111 | line(x1,y2-1,x1,y1+1, '|') ; |
Rhyme | 0:94253645a02a | 112 | } |
Rhyme | 0:94253645a02a | 113 | |
Rhyme | 0:94253645a02a | 114 | /*************************************************** |
Rhyme | 0:94253645a02a | 115 | * circle(x, y, r, c) |
Rhyme | 0:94253645a02a | 116 | * Based on Jack Elton Bresenham's |
Rhyme | 0:94253645a02a | 117 | * Midpoint circle algorithm. |
Rhyme | 0:94253645a02a | 118 | * http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
Rhyme | 0:94253645a02a | 119 | */ |
Rhyme | 0:94253645a02a | 120 | void vt100::circle(int x0, int y0, int r, char c) |
Rhyme | 0:94253645a02a | 121 | { |
Rhyme | 0:94253645a02a | 122 | int f = 1 - r ; |
Rhyme | 0:94253645a02a | 123 | int dFx = 1 ; |
Rhyme | 0:94253645a02a | 124 | int dFy = -2 * r ; |
Rhyme | 0:94253645a02a | 125 | int x = 0 ; |
Rhyme | 0:94253645a02a | 126 | int y = r ; |
Rhyme | 0:94253645a02a | 127 | |
Rhyme | 0:94253645a02a | 128 | putChar(x0, y0 + r, c) ; |
Rhyme | 0:94253645a02a | 129 | putChar(x0, y0 - r, c) ; |
Rhyme | 0:94253645a02a | 130 | putChar(x0 + 2*r, y0, c) ; |
Rhyme | 0:94253645a02a | 131 | putChar(x0 - 2*r, y0, c) ; |
Rhyme | 0:94253645a02a | 132 | |
Rhyme | 0:94253645a02a | 133 | while(x < y) { |
Rhyme | 0:94253645a02a | 134 | if (f >= 0) { |
Rhyme | 0:94253645a02a | 135 | y-- ; |
Rhyme | 0:94253645a02a | 136 | dFy += 2 ; |
Rhyme | 0:94253645a02a | 137 | f += dFy ; |
Rhyme | 0:94253645a02a | 138 | } |
Rhyme | 0:94253645a02a | 139 | x++ ; |
Rhyme | 0:94253645a02a | 140 | dFx += 2 ; |
Rhyme | 0:94253645a02a | 141 | f += dFx ; |
Rhyme | 0:94253645a02a | 142 | putChar(x0 + 2*x, y0 + y, c) ; |
Rhyme | 0:94253645a02a | 143 | putChar(x0 - 2*x, y0 + y, c) ; |
Rhyme | 0:94253645a02a | 144 | putChar(x0 + 2*x, y0 - y, c) ; |
Rhyme | 0:94253645a02a | 145 | putChar(x0 - 2*x, y0 - y, c) ; |
Rhyme | 0:94253645a02a | 146 | putChar(x0 + 2*y, y0 + x, c) ; |
Rhyme | 0:94253645a02a | 147 | putChar(x0 - 2*y, y0 + x, c) ; |
Rhyme | 0:94253645a02a | 148 | putChar(x0 + 2*y, y0 - x, c) ; |
Rhyme | 0:94253645a02a | 149 | putChar(x0 - 2*y, y0 - x, c) ; |
Rhyme | 0:94253645a02a | 150 | } |
Rhyme | 0:94253645a02a | 151 | } |
Rhyme | 0:94253645a02a | 152 | |
Rhyme | 0:94253645a02a | 153 | int vt100::setFG(int newFG) |
Rhyme | 0:94253645a02a | 154 | { |
Rhyme | 0:94253645a02a | 155 | int oldFG = foreground ; |
Rhyme | 0:94253645a02a | 156 | printf("\033[3%dm", newFG) ; |
Rhyme | 0:94253645a02a | 157 | foreground = newFG ; |
Rhyme | 0:94253645a02a | 158 | return( oldFG ) ; |
Rhyme | 0:94253645a02a | 159 | } |
Rhyme | 0:94253645a02a | 160 | |
Rhyme | 0:94253645a02a | 161 | int vt100::getFG() |
Rhyme | 0:94253645a02a | 162 | { |
Rhyme | 0:94253645a02a | 163 | return( foreground ) ; |
Rhyme | 0:94253645a02a | 164 | } |
Rhyme | 0:94253645a02a | 165 | |
Rhyme | 0:94253645a02a | 166 | int vt100::setBG(int newBG) |
Rhyme | 0:94253645a02a | 167 | { |
Rhyme | 0:94253645a02a | 168 | int oldBG = background ; |
Rhyme | 0:94253645a02a | 169 | printf("\033[4%dm", newBG) ; |
Rhyme | 0:94253645a02a | 170 | return( oldBG ) ; |
Rhyme | 0:94253645a02a | 171 | } |
Rhyme | 0:94253645a02a | 172 | |
Rhyme | 0:94253645a02a | 173 | int vt100::getBG() |
Rhyme | 0:94253645a02a | 174 | { |
Rhyme | 0:94253645a02a | 175 | return( background ) ; |
Rhyme | 0:94253645a02a | 176 | } |
Rhyme | 0:94253645a02a | 177 | |
Rhyme | 0:94253645a02a | 178 | void vt100::black() |
Rhyme | 0:94253645a02a | 179 | { |
Rhyme | 0:94253645a02a | 180 | setFG( BLACK ) ; |
Rhyme | 0:94253645a02a | 181 | } |
Rhyme | 0:94253645a02a | 182 | |
Rhyme | 0:94253645a02a | 183 | void vt100::red() |
Rhyme | 0:94253645a02a | 184 | { |
Rhyme | 0:94253645a02a | 185 | setFG( RED ) ; |
Rhyme | 0:94253645a02a | 186 | } |
Rhyme | 0:94253645a02a | 187 | |
Rhyme | 0:94253645a02a | 188 | void vt100::green() |
Rhyme | 0:94253645a02a | 189 | { |
Rhyme | 0:94253645a02a | 190 | setFG( GREEN ) ; |
Rhyme | 0:94253645a02a | 191 | } |
Rhyme | 0:94253645a02a | 192 | |
Rhyme | 0:94253645a02a | 193 | void vt100::yellow() |
Rhyme | 0:94253645a02a | 194 | { |
Rhyme | 0:94253645a02a | 195 | setFG( YELLOW ) ; |
Rhyme | 0:94253645a02a | 196 | } |
Rhyme | 0:94253645a02a | 197 | |
Rhyme | 0:94253645a02a | 198 | void vt100::blue() |
Rhyme | 0:94253645a02a | 199 | { |
Rhyme | 0:94253645a02a | 200 | setFG( BLUE ) ; |
Rhyme | 0:94253645a02a | 201 | } |
Rhyme | 0:94253645a02a | 202 | |
Rhyme | 0:94253645a02a | 203 | void vt100::purple() |
Rhyme | 0:94253645a02a | 204 | { |
Rhyme | 0:94253645a02a | 205 | setFG( PURPLE ) ; |
Rhyme | 0:94253645a02a | 206 | } |
Rhyme | 0:94253645a02a | 207 | |
Rhyme | 0:94253645a02a | 208 | void vt100::cian() |
Rhyme | 0:94253645a02a | 209 | { |
Rhyme | 0:94253645a02a | 210 | setFG( CIAN ) ; |
Rhyme | 0:94253645a02a | 211 | } |
Rhyme | 0:94253645a02a | 212 | |
Rhyme | 0:94253645a02a | 213 | void vt100::white() |
Rhyme | 0:94253645a02a | 214 | { |
Rhyme | 0:94253645a02a | 215 | setFG( WHITE ) ; |
Rhyme | 0:94253645a02a | 216 | } |