Terminal interface for communicating with serial ANSI/VT100 terminals

Dependents:   Terminal_HelloWorld geigercounter01 geigercounter04 DiscoTech ... more

Committer:
simon
Date:
Thu Dec 31 09:50:48 2009 +0000
Revision:
0:2bf27af3c759
Child:
1:96ae39e58792

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:2bf27af3c759 1 /* mbed ANSI/VT100 Terminal Library
simon 0:2bf27af3c759 2 * Copyright (c) 2007-2009 sford
simon 0:2bf27af3c759 3 * Released under the MIT License: http://mbed.org/license/mit
simon 0:2bf27af3c759 4 */
simon 0:2bf27af3c759 5
simon 0:2bf27af3c759 6 #include "Terminal.h"
simon 0:2bf27af3c759 7
simon 0:2bf27af3c759 8 #include "mbed.h"
simon 0:2bf27af3c759 9
simon 0:2bf27af3c759 10 Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
simon 0:2bf27af3c759 11
simon 0:2bf27af3c759 12 void Terminal::cls() {
simon 0:2bf27af3c759 13 this->printf("\033[2J");
simon 0:2bf27af3c759 14 }
simon 0:2bf27af3c759 15
simon 0:2bf27af3c759 16 void Terminal::locate(int column, int row) {
simon 0:2bf27af3c759 17 // Cursor Home <ESC>[{ROW};{COLUMN}H
simon 0:2bf27af3c759 18 this->printf("\033[%d;%dH%c", row + 1, column + 1);
simon 0:2bf27af3c759 19 }
simon 0:2bf27af3c759 20
simon 0:2bf27af3c759 21 static int rgb888tobgr111(int colour) {
simon 0:2bf27af3c759 22 int r = (colour >> 23) & 1;
simon 0:2bf27af3c759 23 int g = (colour >> 15) & 1;
simon 0:2bf27af3c759 24 int b = (colour >> 7) & 1;
simon 0:2bf27af3c759 25 return (b << 2) | (g << 1) | (r << 0);
simon 0:2bf27af3c759 26 }
simon 0:2bf27af3c759 27
simon 0:2bf27af3c759 28 void Terminal::foreground(int colour) {
simon 0:2bf27af3c759 29 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 30 // Foreground Colours : 30 + bgr
simon 0:2bf27af3c759 31 int c = 30 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 32 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 33 }
simon 0:2bf27af3c759 34
simon 0:2bf27af3c759 35 void Terminal::background(int colour) {
simon 0:2bf27af3c759 36 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 37 // Background Colours : 40 + bgr
simon 0:2bf27af3c759 38 int c = 40 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 39 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 40 }