Terminal Library - To interface with a termainal application (e.g. Terterm) to give some more functionallity
Dependents: TestConsoleLib MAX32600MBED_MAX31723_Temperature_Sensor
Revision 0:3bea6f596c03, committed 2010-11-21
- Comitter:
- ms523
- Date:
- Sun Nov 21 08:33:20 2010 +0000
- Commit message:
Changed in this revision
Terminal.cpp | Show annotated file Show diff for this revision Revisions of this file |
Terminal.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 3bea6f596c03 Terminal.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.cpp Sun Nov 21 08:33:20 2010 +0000 @@ -0,0 +1,94 @@ +/* mbed ANSI/VT100 Terminal Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "Terminal.h" +#include "mbed.h" + +#define ASCII_BLOCK 219 +#define ASCII_BORDER_H 205 +#define ASCII_BORDER_V 186 +#define ASCII_BORDER_TL 201 +#define ASCII_BORDER_TR 187 +#define ASCII_BORDER_BL 200 +#define ASCII_BORDER_BR 188 +#define WIDTH 30 + +Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {} + +void Terminal::cls() { + this->printf("\033[2J"); +} + +void Terminal::HideCursor() { + locate(50, 0); +} + +void Terminal::locate(int column, int row) { + // Cursor Home <ESC>[{ROW};{COLUMN}H + this->printf("\033[%d;%dH", row + 1, column + 1); +} + +static int rgb888tobgr111(int colour) { + int r = (colour >> 23) & 1; + int g = (colour >> 15) & 1; + int b = (colour >> 7) & 1; + return (b << 2) | (g << 1) | (r << 0); +} + +void Terminal::foreground(int colour) { + // Set Attribute Mode <ESC>[{n}m + // Foreground Colours : 30 + bgr + int c = 30 + rgb888tobgr111(colour); + this->printf("\033[%dm", c); +} + +void Terminal::background(int colour) { + // Set Attribute Mode <ESC>[{n}m + // Background Colours : 40 + bgr + int c = 40 + rgb888tobgr111(colour); + this->printf("\033[%dm", c); +} + +void Terminal::box(int x, int y, int w, int h) { + // corners + locate(x, y); + putc(ASCII_BORDER_TL); + locate(x + w - 1, y); + putc(ASCII_BORDER_TR); + locate(x, y + h - 1); + putc(ASCII_BORDER_BL); + locate(x + w - 1, y + h - 1); + putc(ASCII_BORDER_BR); + + // top + locate(x + 1, y); + for(int i=0; i<(w-2); i++){ + putc(ASCII_BORDER_H); + } + + // bottom + locate(x + 1, y + h - 1); + for(int i=0; i<(w-2); i++){ + putc(ASCII_BORDER_H); + } + + // left + locate(x, y + 1); + for(int i=1; i<(h-1); i++){ + putc(ASCII_BORDER_V); + printf("\n"); + putc(0x08); + } + + // right + locate(x + w - 1, y + 1); + for(int i=1; i<(h-1); i++){ + putc(ASCII_BORDER_V); + printf("\n"); + putc(0x08); + } +} + +
diff -r 000000000000 -r 3bea6f596c03 Terminal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Terminal.h Sun Nov 21 08:33:20 2010 +0000 @@ -0,0 +1,29 @@ +/* mbed Terminal TextDisplay Library + * Copyright (c) 2007-2009 sford + * Released under the MIT License: http://mbed.org/license/mit + * + * Implementation of ANSI/VT100 Terminal escape codes + * for use with e.g. Teraterm, Hyperterminal + */ + +#include "mbed.h" + +#ifndef MBED_TERMINAL_H +#define MBED_TERMINAL_H + +class Terminal : public Serial { +public: + + Terminal(PinName tx, PinName rx); + + // printf(), put(), baud() etc - inherited from Serial + + void cls(); + void HideCursor(); + void locate(int column, int row); + void foreground(int colour); + void background(int colour); + void box(int x, int y, int w, int h); +}; + +#endif