Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Terminal.cpp Source File

Terminal.cpp

00001 /* mbed ANSI/VT100 Terminal Library
00002  * Copyright (c) 2007-2009 sford
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  */
00005 
00006 #include "Terminal.h"
00007 #include "mbed.h"
00008 
00009 #define ASCII_BLOCK     219
00010 #define ASCII_BORDER_H  205
00011 #define ASCII_BORDER_V  186
00012 #define ASCII_BORDER_TL 201
00013 #define ASCII_BORDER_TR 187
00014 #define ASCII_BORDER_BL 200
00015 #define ASCII_BORDER_BR 188
00016 #define WIDTH 30
00017 
00018 Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
00019 
00020 void Terminal::cls() {
00021     this->printf("\033[2J");
00022 }
00023 
00024 void Terminal::locate(int column, int row) {
00025     // Cursor Home    <ESC>[{ROW};{COLUMN}H
00026     this->printf("\033[%d;%dH", row + 1, column + 1);
00027 }
00028 
00029 static int rgb888tobgr111(int colour) {
00030     int r = (colour >> 23) & 1;
00031     int g = (colour >> 15) & 1;
00032     int b = (colour >> 7) & 1;
00033     return (b << 2) | (g << 1) | (r << 0);
00034 }
00035 
00036 void Terminal::foreground(int colour) {
00037     // Set Attribute Mode    <ESC>[{n}m
00038     // Foreground Colours : 30 + bgr
00039     int c = 30 + rgb888tobgr111(colour);
00040     this->printf("\033[%dm", c);
00041 }
00042 
00043 void Terminal::background(int colour) {
00044     // Set Attribute Mode    <ESC>[{n}m
00045     // Background Colours : 40 + bgr
00046     int c = 40 + rgb888tobgr111(colour);
00047     this->printf("\033[%dm", c);
00048 }
00049 
00050 void Terminal::box(int x, int y, int w, int h) { 
00051      // corners
00052     locate(x, y);
00053     putc(ASCII_BORDER_TL);
00054     locate(x + w - 1, y);
00055     putc(ASCII_BORDER_TR);
00056     locate(x, y + h - 1);
00057     putc(ASCII_BORDER_BL);
00058     locate(x + w - 1, y + h - 1);
00059     putc(ASCII_BORDER_BR);
00060     
00061     // top
00062     locate(x + 1, y);
00063     for(int i=0; i<(w-2); i++){
00064         putc(ASCII_BORDER_H);
00065     }
00066     
00067     // bottom
00068     locate(x + 1, y + h - 1);
00069     for(int i=0; i<(w-2); i++){
00070         putc(ASCII_BORDER_H);
00071     }
00072     
00073     // left
00074     locate(x, y + 1);
00075     for(int i=1; i<(h-1); i++){
00076         putc(ASCII_BORDER_V);
00077         printf("\n");
00078         putc(0x08);
00079     }
00080     
00081     // right
00082     locate(x + w - 1, y + 1);
00083     for(int i=1; i<(h-1); i++){
00084         putc(ASCII_BORDER_V);
00085         printf("\n");
00086         putc(0x08);
00087     }  
00088 } 
00089 
00090