Based on Terminal lib from Simon Ford, some adds

Fork of TerminalPlus by Max Scordamaglia

Committer:
MaxScorda
Date:
Sun Dec 27 23:15:31 2015 +0000
Revision:
14:3da6173413b7
Parent:
13:09eb30497e78
functions adds

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MaxScorda 7:80096ab72764 1 /** mbed TerminalPlus Library, for ANSI/VT200 Terminals and ecape codes
MaxScorda 7:80096ab72764 2 * Copyright (c) 2015, Max Scordamaglia , https//developer.mbed.org/users/MaxScorda/
MaxScorda 7:80096ab72764 3 * fork from Terminal Library
simon 2:85184c13476c 4 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:96ae39e58792 5 *
simon 1:96ae39e58792 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:96ae39e58792 7 * of this software and associated documentation files (the "Software"), to deal
simon 1:96ae39e58792 8 * in the Software without restriction, including without limitation the rights
simon 1:96ae39e58792 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:96ae39e58792 10 * copies of the Software, and to permit persons to whom the Software is
simon 1:96ae39e58792 11 * furnished to do so, subject to the following conditions:
simon 1:96ae39e58792 12 *
simon 1:96ae39e58792 13 * The above copyright notice and this permission notice shall be included in
simon 1:96ae39e58792 14 * all copies or substantial portions of the Software.
simon 1:96ae39e58792 15 *
simon 1:96ae39e58792 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:96ae39e58792 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:96ae39e58792 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:96ae39e58792 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:96ae39e58792 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:96ae39e58792 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:96ae39e58792 22 * THE SOFTWARE.
simon 0:2bf27af3c759 23 */
simon 0:2bf27af3c759 24
MaxScorda 6:f8c90e147000 25
MaxScorda 6:f8c90e147000 26 /*
MaxScorda 6:f8c90e147000 27 Porre il terminate in VT100 e set di caratteri cp437
MaxScorda 6:f8c90e147000 28 */
MaxScorda 6:f8c90e147000 29
simon 0:2bf27af3c759 30 #include "Terminal.h"
simon 0:2bf27af3c759 31 #include "mbed.h"
simon 0:2bf27af3c759 32
simon 0:2bf27af3c759 33 Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
simon 0:2bf27af3c759 34
MaxScorda 3:e72f2addfaf8 35 void Terminal::cls()
MaxScorda 3:e72f2addfaf8 36 {
simon 0:2bf27af3c759 37 this->printf("\033[2J");
simon 0:2bf27af3c759 38 }
simon 0:2bf27af3c759 39
MaxScorda 3:e72f2addfaf8 40 void Terminal::locate(int column, int row)
MaxScorda 3:e72f2addfaf8 41 {
simon 0:2bf27af3c759 42 // Cursor Home <ESC>[{ROW};{COLUMN}H
MaxScorda 3:e72f2addfaf8 43
MaxScorda 3:e72f2addfaf8 44 // this->printf("\033[%d;%dH%c", row + 1, column + 1); //original
MaxScorda 5:d045e3561533 45 this->printf("\033[%d;%dH", row+1 , column+1 );
simon 0:2bf27af3c759 46 }
simon 0:2bf27af3c759 47
MaxScorda 3:e72f2addfaf8 48 static int rgb888tobgr111(int colour)
MaxScorda 3:e72f2addfaf8 49 {
simon 0:2bf27af3c759 50 int r = (colour >> 23) & 1;
simon 0:2bf27af3c759 51 int g = (colour >> 15) & 1;
simon 0:2bf27af3c759 52 int b = (colour >> 7) & 1;
simon 0:2bf27af3c759 53 return (b << 2) | (g << 1) | (r << 0);
simon 0:2bf27af3c759 54 }
simon 0:2bf27af3c759 55
MaxScorda 3:e72f2addfaf8 56 void Terminal::foreground(int colour)
MaxScorda 3:e72f2addfaf8 57 {
simon 0:2bf27af3c759 58 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 59 // Foreground Colours : 30 + bgr
simon 0:2bf27af3c759 60 int c = 30 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 61 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 62 }
simon 0:2bf27af3c759 63
MaxScorda 6:f8c90e147000 64 void Terminal::forgcol(int color)
MaxScorda 6:f8c90e147000 65 {
MaxScorda 6:f8c90e147000 66 /*colori da 0 a 7
MaxScorda 6:f8c90e147000 67 [ 3 0 m setaf 0 Set foreground to color #0 - black
MaxScorda 6:f8c90e147000 68 [ 3 1 m setaf 1 Set foreground to color #1 - red
MaxScorda 6:f8c90e147000 69 [ 3 2 m setaf 2 Set foreground to color #2 - green
MaxScorda 6:f8c90e147000 70 [ 3 3 m setaf 3 Set foreground to color #3 - yellow
MaxScorda 6:f8c90e147000 71 [ 3 4 m setaf 4 Set foreground to color #4 - blue
MaxScorda 6:f8c90e147000 72 [ 3 5 m setaf 5 Set foreground to color #5 - magenta
MaxScorda 6:f8c90e147000 73 [ 3 6 m setaf 6 Set foreground to color #6 - cyan
MaxScorda 6:f8c90e147000 74 [ 3 7 m setaf 7 Set foreground to color #7 - white
MaxScorda 6:f8c90e147000 75 [ 3 9 m setaf 9 Set default color as foreground color
MaxScorda 6:f8c90e147000 76 */
MaxScorda 6:f8c90e147000 77
MaxScorda 6:f8c90e147000 78 if ((color<0) || (color>7)) color =7; //normalizza
MaxScorda 6:f8c90e147000 79 color=color+30;
MaxScorda 6:f8c90e147000 80 this->printf("\033[%dm", color);
MaxScorda 6:f8c90e147000 81 }
MaxScorda 6:f8c90e147000 82
MaxScorda 6:f8c90e147000 83 void Terminal::bckgcol(int color)
MaxScorda 6:f8c90e147000 84 {
MaxScorda 6:f8c90e147000 85 /*colori da 0 a 7
MaxScorda 6:f8c90e147000 86 [ 4 0 m setab 0 Set background to color #0 - black
MaxScorda 6:f8c90e147000 87 [ 4 1 m setab 1 Set background to color #1 - red
MaxScorda 6:f8c90e147000 88 [ 4 2 m setab 2 Set background to color #2 - green
MaxScorda 6:f8c90e147000 89 [ 4 3 m setab 3 Set background to color #3 - yellow
MaxScorda 6:f8c90e147000 90 [ 4 4 m setab 4 Set background to color #4 - blue
MaxScorda 6:f8c90e147000 91 [ 4 5 m setab 5 Set background to color #5 - magenta
MaxScorda 6:f8c90e147000 92 [ 4 6 m setab 6 Set background to color #6 - cyan
MaxScorda 6:f8c90e147000 93 [ 4 7 m setab 7 Set background to color #7 - white
MaxScorda 6:f8c90e147000 94 [ 4 9 m setaf 9 Set default color as background color
MaxScorda 6:f8c90e147000 95 */
MaxScorda 6:f8c90e147000 96
MaxScorda 6:f8c90e147000 97 if ((color<0) || (color>7)) color =0; //normalizza
MaxScorda 6:f8c90e147000 98 color=color+40;
MaxScorda 6:f8c90e147000 99 this->printf("\033[%dm", color);
MaxScorda 6:f8c90e147000 100 }
MaxScorda 6:f8c90e147000 101
MaxScorda 6:f8c90e147000 102
MaxScorda 3:e72f2addfaf8 103 void Terminal::background(int colour)
MaxScorda 3:e72f2addfaf8 104 {
simon 0:2bf27af3c759 105 // Set Attribute Mode <ESC>[{n}m
simon 0:2bf27af3c759 106 // Background Colours : 40 + bgr
simon 0:2bf27af3c759 107 int c = 40 + rgb888tobgr111(colour);
simon 0:2bf27af3c759 108 this->printf("\033[%dm", c);
simon 0:2bf27af3c759 109 }
MaxScorda 3:e72f2addfaf8 110
MaxScorda 5:d045e3561533 111 void Terminal::reset()
MaxScorda 5:d045e3561533 112 {
MaxScorda 5:d045e3561533 113 // Reset Screen
MaxScorda 5:d045e3561533 114 this->printf("\033c");
MaxScorda 5:d045e3561533 115 }
MaxScorda 5:d045e3561533 116
MaxScorda 5:d045e3561533 117 void Terminal::eraseline()
MaxScorda 5:d045e3561533 118 {
MaxScorda 7:80096ab72764 119 // Erase full line, cursor save
MaxScorda 5:d045e3561533 120 this->printf("\033[2K");
MaxScorda 5:d045e3561533 121 }
MaxScorda 5:d045e3561533 122
MaxScorda 14:3da6173413b7 123 void Terminal::erasesquare(int x1, int y1, int x2, int y2)
MaxScorda 14:3da6173413b7 124 {
MaxScorda 14:3da6173413b7 125 // cancella un quadrato dello schermo
MaxScorda 14:3da6173413b7 126 for(int i=y1; i<y2; i++) {
MaxScorda 14:3da6173413b7 127 //formatPrintf("*",x1,i,x2-x1);
MaxScorda 14:3da6173413b7 128 formatPrintf(string2char(padstr(" ",x2-x1,' ')),x1,i);
MaxScorda 14:3da6173413b7 129 }
MaxScorda 14:3da6173413b7 130
MaxScorda 14:3da6173413b7 131 }
MaxScorda 7:80096ab72764 132 void Terminal::resetattrib()
MaxScorda 7:80096ab72764 133 {
MaxScorda 7:80096ab72764 134 // reset attrib
MaxScorda 7:80096ab72764 135 this->printf("\033[0m");
MaxScorda 7:80096ab72764 136 }
MaxScorda 5:d045e3561533 137
MaxScorda 7:80096ab72764 138 void Terminal::bold()
MaxScorda 7:80096ab72764 139 {
MaxScorda 7:80096ab72764 140 // font bold
MaxScorda 7:80096ab72764 141 this->printf("\033[1m");
MaxScorda 7:80096ab72764 142 }
MaxScorda 7:80096ab72764 143
MaxScorda 7:80096ab72764 144 void Terminal::underscore()
MaxScorda 7:80096ab72764 145 {
MaxScorda 7:80096ab72764 146 // underscore
MaxScorda 7:80096ab72764 147 this->printf("\033[4m");
MaxScorda 7:80096ab72764 148 }
MaxScorda 7:80096ab72764 149
MaxScorda 7:80096ab72764 150 void Terminal::blink()
MaxScorda 7:80096ab72764 151 {
MaxScorda 7:80096ab72764 152 // font blink
MaxScorda 7:80096ab72764 153 this->printf("\033[5m");
MaxScorda 7:80096ab72764 154 }
MaxScorda 7:80096ab72764 155
MaxScorda 7:80096ab72764 156 void Terminal::reverse()
MaxScorda 7:80096ab72764 157 {
MaxScorda 7:80096ab72764 158 // font reverse
MaxScorda 7:80096ab72764 159 this->printf("\033[7m");
MaxScorda 7:80096ab72764 160 }
MaxScorda 5:d045e3561533 161 //*************************************
MaxScorda 8:e3c6d6322506 162 void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb, bool boldf )
MaxScorda 5:d045e3561533 163 {
MaxScorda 7:80096ab72764 164 int i=0; //mettere lungo come stringa
MaxScorda 7:80096ab72764 165 //string tempstr=string(sstr);
MaxScorda 7:80096ab72764 166 int screenColumn=78; //per ora lo forziamo
MaxScorda 5:d045e3561533 167
MaxScorda 7:80096ab72764 168 locate(xx, yy);
MaxScorda 8:e3c6d6322506 169 if (boldf==1) bold();
MaxScorda 8:e3c6d6322506 170 this->puts(sstr);
MaxScorda 7:80096ab72764 171 if (yy!=23) this->printf("\033[1A"); // fa davvero cagare. Torna su col cursore per evitare il \n eccetto l'ultima riga. Servirebbe togliere il \n
MaxScorda 7:80096ab72764 172 while ((sstr[i]!='\n') && (i<(screenColumn-xx+1))) {
MaxScorda 7:80096ab72764 173 i++;
MaxScorda 7:80096ab72764 174 }
MaxScorda 7:80096ab72764 175
MaxScorda 7:80096ab72764 176 //prosegui col pad
MaxScorda 7:80096ab72764 177 while ((padb>0) && (i<(screenColumn-xx-1))) {
MaxScorda 7:80096ab72764 178 this->printf(" "); //migliorare con stringa unica
MaxScorda 7:80096ab72764 179 i++;
MaxScorda 7:80096ab72764 180 padb--;
MaxScorda 7:80096ab72764 181 }
MaxScorda 8:e3c6d6322506 182 if (boldf==1) resetattrib();
MaxScorda 5:d045e3561533 183 }
MaxScorda 5:d045e3561533 184
MaxScorda 5:d045e3561533 185
MaxScorda 4:ee2311717b80 186 void Terminal::frame(int x, int y, int w, int h, int boxtype)
MaxScorda 3:e72f2addfaf8 187 {
MaxScorda 4:ee2311717b80 188 char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
MaxScorda 4:ee2311717b80 189 // draw frame
MaxScorda 4:ee2311717b80 190 // BLOCK=219; SE SI VORRA' USARE IL FILL
MaxScorda 3:e72f2addfaf8 191 switch (boxtype) {
MaxScorda 3:e72f2addfaf8 192 case 0: //singolo
MaxScorda 13:09eb30497e78 193 B_H =196;
MaxScorda 13:09eb30497e78 194 B_V =179;
MaxScorda 13:09eb30497e78 195 B_TL =218;
MaxScorda 13:09eb30497e78 196 B_TR =191;
MaxScorda 13:09eb30497e78 197 B_BL =192;
MaxScorda 13:09eb30497e78 198 B_BR =217;
MaxScorda 13:09eb30497e78 199 break;
MaxScorda 13:09eb30497e78 200
MaxScorda 13:09eb30497e78 201 case 1: //doppio
MaxScorda 13:09eb30497e78 202 B_H =205;
MaxScorda 4:ee2311717b80 203 B_V =186;
MaxScorda 4:ee2311717b80 204 B_TL =201;
MaxScorda 4:ee2311717b80 205 B_TR =187;
MaxScorda 13:09eb30497e78 206 B_BL =200;
MaxScorda 4:ee2311717b80 207 B_BR =188;
MaxScorda 3:e72f2addfaf8 208 break;
MaxScorda 3:e72f2addfaf8 209
MaxScorda 13:09eb30497e78 210 case 2: //misto 1
MaxScorda 13:09eb30497e78 211 B_H =196;
MaxScorda 3:e72f2addfaf8 212 B_V =186;
MaxScorda 13:09eb30497e78 213 B_TL =214;
MaxScorda 13:09eb30497e78 214 B_TR =183;
MaxScorda 13:09eb30497e78 215 B_BL =212;
MaxScorda 13:09eb30497e78 216 B_BR =189;
MaxScorda 3:e72f2addfaf8 217 break;
MaxScorda 3:e72f2addfaf8 218 }
MaxScorda 10:60b9f0462d03 219
MaxScorda 3:e72f2addfaf8 220 //riga superiore
MaxScorda 7:80096ab72764 221 formatPrintf(createStr(B_TL) ,x,y);
MaxScorda 7:80096ab72764 222 formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y);
MaxScorda 6:f8c90e147000 223 //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y);
MaxScorda 7:80096ab72764 224 formatPrintf(createStr(B_TR),x+w,y);
MaxScorda 3:e72f2addfaf8 225 //corpo
MaxScorda 3:e72f2addfaf8 226 for(int i=y+1; i<y+h; i++) {
MaxScorda 7:80096ab72764 227 formatPrintf(createStr(B_V),x,i);
MaxScorda 7:80096ab72764 228 formatPrintf(createStr(B_V),x+w,i);
MaxScorda 3:e72f2addfaf8 229 }
MaxScorda 3:e72f2addfaf8 230 //riga inferiore
MaxScorda 7:80096ab72764 231 formatPrintf(createStr(B_BL),x,y+h);
MaxScorda 7:80096ab72764 232 // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h);
MaxScorda 7:80096ab72764 233 formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h);
MaxScorda 7:80096ab72764 234 formatPrintf(createStr(B_BR),x+w,y+h);
MaxScorda 5:d045e3561533 235 }
MaxScorda 3:e72f2addfaf8 236
MaxScorda 6:f8c90e147000 237 void Terminal::readypos()
MaxScorda 6:f8c90e147000 238 {
MaxScorda 11:38e8a2260621 239 locate(11, 23);
MaxScorda 11:38e8a2260621 240 //this->printf("Command: > ");
MaxScorda 3:e72f2addfaf8 241 }
MaxScorda 6:f8c90e147000 242
MaxScorda 6:f8c90e147000 243
MaxScorda 6:f8c90e147000 244 //------------------------------------------------
MaxScorda 6:f8c90e147000 245 string Terminal::padstr(string sttde, int maxlen, char fillchar)
MaxScorda 6:f8c90e147000 246 {
MaxScorda 6:f8c90e147000 247 bool flagEOS=false;
MaxScorda 6:f8c90e147000 248 string ret=sttde;
MaxScorda 6:f8c90e147000 249 if (ret.size()>0) {
MaxScorda 6:f8c90e147000 250 //se ha EOS lo tolgo
MaxScorda 6:f8c90e147000 251 if (ret.substr(ret.size()-1,1) == "\n") {
MaxScorda 6:f8c90e147000 252 ret=ret.substr(0,ret.size()-1);
MaxScorda 6:f8c90e147000 253 flagEOS=true;
MaxScorda 6:f8c90e147000 254 }
MaxScorda 6:f8c90e147000 255 //pad
MaxScorda 6:f8c90e147000 256 if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar);
MaxScorda 6:f8c90e147000 257 //se aveva EOS, lo rimetto
MaxScorda 6:f8c90e147000 258 if (flagEOS==true) ret=addEOS(ret);
MaxScorda 6:f8c90e147000 259 }
MaxScorda 6:f8c90e147000 260 return ret;
MaxScorda 6:f8c90e147000 261 }
MaxScorda 6:f8c90e147000 262
MaxScorda 6:f8c90e147000 263 string Terminal::addEOS(string sttde)
MaxScorda 6:f8c90e147000 264 {
MaxScorda 6:f8c90e147000 265 string ret=sttde;
MaxScorda 6:f8c90e147000 266 if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n";
MaxScorda 6:f8c90e147000 267 return ret;
MaxScorda 6:f8c90e147000 268 }
MaxScorda 6:f8c90e147000 269
MaxScorda 6:f8c90e147000 270 char* Terminal::string2char(string sttde)
MaxScorda 6:f8c90e147000 271 {
MaxScorda 6:f8c90e147000 272 //ora aggiunge comunque l'EOS. Decidere se parametrizzare
MaxScorda 6:f8c90e147000 273 sttde=addEOS(sttde);
MaxScorda 6:f8c90e147000 274 char *cstr = new char[sttde.length() + 1];
MaxScorda 6:f8c90e147000 275 strcpy(cstr, sttde.c_str());
MaxScorda 6:f8c90e147000 276 // delete [] cstr;
MaxScorda 6:f8c90e147000 277 return cstr;
MaxScorda 7:80096ab72764 278 }
MaxScorda 7:80096ab72764 279
MaxScorda 7:80096ab72764 280 char* Terminal::createStr(char car)
MaxScorda 7:80096ab72764 281 {
MaxScorda 7:80096ab72764 282 // unico modo in cui passo un varole ascii ad una funzione aggiungendo il ritorno a capo.
MaxScorda 7:80096ab72764 283 //se si potesse evitare sarebbe meglio
MaxScorda 7:80096ab72764 284 char *str = (char *) malloc(2 * sizeof(char));
MaxScorda 7:80096ab72764 285 if(str == NULL) return NULL;
MaxScorda 7:80096ab72764 286 str[0] = car;
MaxScorda 7:80096ab72764 287 str[1] = '\0';
MaxScorda 7:80096ab72764 288 return str;
MaxScorda 6:f8c90e147000 289 }