a VT100 library found on the mbed site - w/o any licensing information or restrictions.
VT100.cpp
- Committer:
- WiredHome
- Date:
- 2020-04-30
- Revision:
- 1:98f12a26a3da
- Parent:
- 0:8f2947c0686e
File content as of revision 1:98f12a26a3da:
#include "mbed.h" #include "VT100.h" static const char ESC = '\033' ; vt100::vt100(Serial * _pc, const char * name) : Stream(name) { pc = _pc; } vt100::~vt100() { } void vt100::cls(void) { pc->printf("%c[2J", ESC) ; locate(1,1) ; } void vt100::locate(int x, int y) { pc->printf("%c[%d;%dH",ESC,y,x) ; } void vt100::putChar(int x, int y, char c) { locate(x,y) ; pc->printf("%c",c) ; } void vt100::putStr(int x, int y, const char *str) { locate(x,y) ; pc->puts(str) ; } void vt100::line(int x1, int y1, int x2, int y2, char c) { int x, y, dx, dy, w, h, step ; dx = x2 - x1 ; dy = y2 - y1 ; w = (dx >= 0)? dx : -dx ; h = (dy >= 0)? dy : -dy ; if (dx == 0) { /* vertical line */ step = (dy >= 0) ? 1 : -1 ; for (y = 0 ; y <= h ; y++) { putChar(x1, y1 + (step * y), c) ; } } else if (dy == 0) { /* Horizontal line */ step = (dx >= 0) ? 1 : -1 ; for (x = 0 ; x <= w ; x++) { putChar(x1 + (step * x), y1, c) ; } } else { if (w >= h) { /* use x as step */ step = (dx >= 0) ? 1 : -1 ; for (x = 0 ; x <= w ; x++ ) { putChar( x1 + step*x, y1 + ((2*x+1) * dy)/(2 * w), c) ; } } else { /* use y as step */ step = (dy >= 0) ? 1 : -1 ; for (y = 0 ; y <= h ; y++ ) { putChar( x1 + ((2*y+1) * dx)/(2*h), y1 + step*y, c) ; } } } } int vt100::_putc(int value) { return pc->putc(value); } int vt100::_getc() { return pc->getc(); } int vt100::readable() { return pc->readable(); } /**************************************************** * frame(x1, y1, x2, y2) * draw textual frame * (x1,y1) (x2,y1) * +--------------------------+ * | | * +--------------------------+ * (x1,y2) (x2,y2) */ void vt100::frame(int x1, int y1, int x2, int y2) { int tmp ; if (x1 > x2) { tmp = x1 ; x1 = x2 ; x2 = tmp ; } if (y1 > y2) { tmp = y1 ; y1 = y2 ; y2 = tmp ; } putChar(x1, y1, '+') ; line(x1+1,y1,x2-1,y1, '-') ; putChar(x2, y1, '+') ; line(x2,y1+1,x2,y2-1, '|') ; putChar(x2, y2, '+') ; line(x2-1,y2,x1+1,y2, '-') ; putChar(x1, y2, '+') ; line(x1,y2-1,x1,y1+1, '|') ; } /*************************************************** * circle(x, y, r, c) * Based on Jack Elton Bresenham's * Midpoint circle algorithm. * http://en.wikipedia.org/wiki/Midpoint_circle_algorithm */ void vt100::circle(int x0, int y0, int r, char c) { int f = 1 - r ; int dFx = 1 ; int dFy = -2 * r ; int x = 0 ; int y = r ; putChar(x0, y0 + r, c) ; putChar(x0, y0 - r, c) ; putChar(x0 + 2*r, y0, c) ; putChar(x0 - 2*r, y0, c) ; while(x < y) { if (f >= 0) { y-- ; dFy += 2 ; f += dFy ; } x++ ; dFx += 2 ; f += dFx ; putChar(x0 + 2*x, y0 + y, c) ; putChar(x0 - 2*x, y0 + y, c) ; putChar(x0 + 2*x, y0 - y, c) ; putChar(x0 - 2*x, y0 - y, c) ; putChar(x0 + 2*y, y0 + x, c) ; putChar(x0 - 2*y, y0 + x, c) ; putChar(x0 + 2*y, y0 - x, c) ; putChar(x0 - 2*y, y0 - x, c) ; } } vt100::VT_COLOR vt100::setFG(VT_COLOR newFG) { VT_COLOR oldFG = foreground; pc->printf("\033[3%dm", newFG) ; foreground = newFG ; return( oldFG ) ; } vt100::VT_COLOR vt100::getFG() { return( foreground ) ; } vt100::VT_COLOR vt100::setBG(VT_COLOR newBG) { VT_COLOR oldBG = background ; pc->printf("\033[4%dm", newBG) ; return( oldBG ) ; } vt100::VT_COLOR vt100::getBG() { return( background ) ; }