Based on Terminal lib from Simon Ford, some adds

Fork of TerminalPlus by Max Scordamaglia

Committer:
MaxScorda
Date:
Fri Sep 11 00:15:26 2015 +0000
Revision:
4:ee2311717b80
Parent:
3:e72f2addfaf8
Child:
5:d045e3561533
Correzioni formali

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:96ae39e58792 1 /* mbed Terminal Library, for ANSI/VT200 Terminals and ecape codes
simon 2:85184c13476c 2 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:96ae39e58792 3 *
simon 1:96ae39e58792 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:96ae39e58792 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:96ae39e58792 6 * in the Software without restriction, including without limitation the rights
simon 1:96ae39e58792 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:96ae39e58792 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:96ae39e58792 9 * furnished to do so, subject to the following conditions:
simon 1:96ae39e58792 10 *
simon 1:96ae39e58792 11 * The above copyright notice and this permission notice shall be included in
simon 1:96ae39e58792 12 * all copies or substantial portions of the Software.
simon 1:96ae39e58792 13 *
simon 1:96ae39e58792 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:96ae39e58792 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:96ae39e58792 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:96ae39e58792 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:96ae39e58792 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:96ae39e58792 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:96ae39e58792 20 * THE SOFTWARE.
simon 0:2bf27af3c759 21 */
simon 0:2bf27af3c759 22
simon 0:2bf27af3c759 23 #include "Terminal.h"
simon 0:2bf27af3c759 24
simon 0:2bf27af3c759 25 #include "mbed.h"
simon 0:2bf27af3c759 26
simon 0:2bf27af3c759 27 Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
simon 0:2bf27af3c759 28
MaxScorda 3:e72f2addfaf8 29 void Terminal::cls()
MaxScorda 3:e72f2addfaf8 30 {
simon 0:2bf27af3c759 31 this->printf("\033[2J");
simon 0:2bf27af3c759 32 }
simon 0:2bf27af3c759 33
MaxScorda 3:e72f2addfaf8 34 void Terminal::locate(int column, int row)
MaxScorda 3:e72f2addfaf8 35 {
simon 0:2bf27af3c759 36 // Cursor Home <ESC>[{ROW};{COLUMN}H
MaxScorda 3:e72f2addfaf8 37
MaxScorda 3:e72f2addfaf8 38 // this->printf("\033[%d;%dH%c", row + 1, column + 1); //original
MaxScorda 3:e72f2addfaf8 39 this->printf("\033[%d;%dH", row + 1, column + 1);
simon 0:2bf27af3c759 40 }
simon 0:2bf27af3c759 41
MaxScorda 3:e72f2addfaf8 42 static int rgb888tobgr111(int colour)
MaxScorda 3:e72f2addfaf8 43 {
simon 0:2bf27af3c759 44 int r = (colour >> 23) & 1;
simon 0:2bf27af3c759 45 int g = (colour >> 15) & 1;
simon 0:2bf27af3c759 46 int b = (colour >> 7) & 1;
simon 0:2bf27af3c759 47 return (b << 2) | (g << 1) | (r << 0);
simon 0:2bf27af3c759 48 }
simon 0:2bf27af3c759 49
MaxScorda 3:e72f2addfaf8 50 void Terminal::foreground(int colour)
MaxScorda 3:e72f2addfaf8 51 {
simon 0:2bf27af3c759 52 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 53 // Foreground Colours : 30 + bgr
simon 0:2bf27af3c759 54 int c = 30 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 55 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 56 }
simon 0:2bf27af3c759 57
MaxScorda 3:e72f2addfaf8 58 void Terminal::background(int colour)
MaxScorda 3:e72f2addfaf8 59 {
simon 0:2bf27af3c759 60 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 61 // Background Colours : 40 + bgr
simon 0:2bf27af3c759 62 int c = 40 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 63 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 64 }
MaxScorda 3:e72f2addfaf8 65
MaxScorda 4:ee2311717b80 66 void Terminal::frame(int x, int y, int w, int h, int boxtype)
MaxScorda 3:e72f2addfaf8 67 {
MaxScorda 4:ee2311717b80 68 char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
MaxScorda 4:ee2311717b80 69 // draw frame
MaxScorda 4:ee2311717b80 70 // BLOCK=219; SE SI VORRA' USARE IL FILL
MaxScorda 3:e72f2addfaf8 71 switch (boxtype) {
MaxScorda 3:e72f2addfaf8 72 case 0: //singolo
MaxScorda 4:ee2311717b80 73 B_H =205;
MaxScorda 4:ee2311717b80 74 B_V =186;
MaxScorda 4:ee2311717b80 75 B_TL =201;
MaxScorda 4:ee2311717b80 76 B_TR =187;
MaxScorda 4:ee2311717b80 77 B_BL= 200;
MaxScorda 4:ee2311717b80 78 B_BR =188;
MaxScorda 3:e72f2addfaf8 79 break;
MaxScorda 3:e72f2addfaf8 80
MaxScorda 3:e72f2addfaf8 81 case 1: //doppio
MaxScorda 3:e72f2addfaf8 82 B_H =205;
MaxScorda 3:e72f2addfaf8 83 B_V =186;
MaxScorda 3:e72f2addfaf8 84 B_TL =201;
MaxScorda 3:e72f2addfaf8 85 B_TR =187;
MaxScorda 3:e72f2addfaf8 86 B_BL= 200;
MaxScorda 3:e72f2addfaf8 87 B_BR =188;
MaxScorda 3:e72f2addfaf8 88 break;
MaxScorda 3:e72f2addfaf8 89 }
MaxScorda 3:e72f2addfaf8 90 //riga superiore
MaxScorda 3:e72f2addfaf8 91 this->printf("\033[%d;%dH", x, y);
MaxScorda 4:ee2311717b80 92 this->printf((char *) B_TL);
MaxScorda 4:ee2311717b80 93 for(int i=x+1; i<x+w; i++) this->printf((char *) B_H);
MaxScorda 4:ee2311717b80 94 this->printf((char *) B_TR);
MaxScorda 3:e72f2addfaf8 95 //corpo
MaxScorda 3:e72f2addfaf8 96 for(int i=y+1; i<y+h; i++) {
MaxScorda 3:e72f2addfaf8 97 this->printf("\033[%d;%dH", x, i);
MaxScorda 4:ee2311717b80 98 this->printf((char *) B_V);
MaxScorda 3:e72f2addfaf8 99 this->printf("\033[%d;%dH", x+w, i);
MaxScorda 4:ee2311717b80 100 this->printf((char *) B_V);
MaxScorda 3:e72f2addfaf8 101 }
MaxScorda 3:e72f2addfaf8 102 //riga inferiore
MaxScorda 3:e72f2addfaf8 103 this->printf("\033[%d;%dH", x, y+h);
MaxScorda 4:ee2311717b80 104 this->printf((char *) B_BL);
MaxScorda 4:ee2311717b80 105 for(int i=x+1; i<x+w; i++) this->printf((char *) B_H);
MaxScorda 4:ee2311717b80 106 this->printf((char *) B_BR);
MaxScorda 3:e72f2addfaf8 107
MaxScorda 3:e72f2addfaf8 108 }