Based on Terminal lib from Simon Ford, some adds

Fork of TerminalPlus by Max Scordamaglia

Committer:
MaxScorda
Date:
Wed Sep 23 23:57:08 2015 +0000
Revision:
9:82803dacf475
Parent:
8:e3c6d6322506
Child:
10:60b9f0462d03
Minor

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 7:80096ab72764 123 void Terminal::resetattrib()
MaxScorda 7:80096ab72764 124 {
MaxScorda 7:80096ab72764 125 // reset attrib
MaxScorda 7:80096ab72764 126 this->printf("\033[0m");
MaxScorda 7:80096ab72764 127 }
MaxScorda 5:d045e3561533 128
MaxScorda 7:80096ab72764 129 void Terminal::bold()
MaxScorda 7:80096ab72764 130 {
MaxScorda 7:80096ab72764 131 // font bold
MaxScorda 7:80096ab72764 132 this->printf("\033[1m");
MaxScorda 7:80096ab72764 133 }
MaxScorda 7:80096ab72764 134
MaxScorda 7:80096ab72764 135 void Terminal::underscore()
MaxScorda 7:80096ab72764 136 {
MaxScorda 7:80096ab72764 137 // underscore
MaxScorda 7:80096ab72764 138 this->printf("\033[4m");
MaxScorda 7:80096ab72764 139 }
MaxScorda 7:80096ab72764 140
MaxScorda 7:80096ab72764 141 void Terminal::blink()
MaxScorda 7:80096ab72764 142 {
MaxScorda 7:80096ab72764 143 // font blink
MaxScorda 7:80096ab72764 144 this->printf("\033[5m");
MaxScorda 7:80096ab72764 145 }
MaxScorda 7:80096ab72764 146
MaxScorda 7:80096ab72764 147 void Terminal::reverse()
MaxScorda 7:80096ab72764 148 {
MaxScorda 7:80096ab72764 149 // font reverse
MaxScorda 7:80096ab72764 150 this->printf("\033[7m");
MaxScorda 7:80096ab72764 151 }
MaxScorda 5:d045e3561533 152 //*************************************
MaxScorda 8:e3c6d6322506 153 void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb, bool boldf )
MaxScorda 5:d045e3561533 154 {
MaxScorda 7:80096ab72764 155 int i=0; //mettere lungo come stringa
MaxScorda 7:80096ab72764 156 //string tempstr=string(sstr);
MaxScorda 7:80096ab72764 157 int screenColumn=78; //per ora lo forziamo
MaxScorda 5:d045e3561533 158
MaxScorda 7:80096ab72764 159 locate(xx, yy);
MaxScorda 8:e3c6d6322506 160 if (boldf==1) bold();
MaxScorda 8:e3c6d6322506 161 this->puts(sstr);
MaxScorda 7:80096ab72764 162 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 163 while ((sstr[i]!='\n') && (i<(screenColumn-xx+1))) {
MaxScorda 7:80096ab72764 164 i++;
MaxScorda 7:80096ab72764 165 }
MaxScorda 7:80096ab72764 166
MaxScorda 7:80096ab72764 167 //prosegui col pad
MaxScorda 7:80096ab72764 168 while ((padb>0) && (i<(screenColumn-xx-1))) {
MaxScorda 7:80096ab72764 169 this->printf(" "); //migliorare con stringa unica
MaxScorda 7:80096ab72764 170 i++;
MaxScorda 7:80096ab72764 171 padb--;
MaxScorda 7:80096ab72764 172 }
MaxScorda 8:e3c6d6322506 173 if (boldf==1) resetattrib();
MaxScorda 5:d045e3561533 174 }
MaxScorda 5:d045e3561533 175
MaxScorda 5:d045e3561533 176
MaxScorda 4:ee2311717b80 177 void Terminal::frame(int x, int y, int w, int h, int boxtype)
MaxScorda 3:e72f2addfaf8 178 {
MaxScorda 4:ee2311717b80 179 char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
MaxScorda 4:ee2311717b80 180 // draw frame
MaxScorda 4:ee2311717b80 181 // BLOCK=219; SE SI VORRA' USARE IL FILL
MaxScorda 3:e72f2addfaf8 182 switch (boxtype) {
MaxScorda 3:e72f2addfaf8 183 case 0: //singolo
MaxScorda 6:f8c90e147000 184 B_H =196;
MaxScorda 4:ee2311717b80 185 B_V =186;
MaxScorda 4:ee2311717b80 186 B_TL =201;
MaxScorda 4:ee2311717b80 187 B_TR =187;
MaxScorda 4:ee2311717b80 188 B_BL= 200;
MaxScorda 4:ee2311717b80 189 B_BR =188;
MaxScorda 3:e72f2addfaf8 190 break;
MaxScorda 3:e72f2addfaf8 191
MaxScorda 3:e72f2addfaf8 192 case 1: //doppio
MaxScorda 3:e72f2addfaf8 193 B_H =205;
MaxScorda 3:e72f2addfaf8 194 B_V =186;
MaxScorda 3:e72f2addfaf8 195 B_TL =201;
MaxScorda 3:e72f2addfaf8 196 B_TR =187;
MaxScorda 3:e72f2addfaf8 197 B_BL= 200;
MaxScorda 3:e72f2addfaf8 198 B_BR =188;
MaxScorda 3:e72f2addfaf8 199 break;
MaxScorda 3:e72f2addfaf8 200 }
MaxScorda 9:82803dacf475 201
MaxScorda 3:e72f2addfaf8 202 //riga superiore
MaxScorda 7:80096ab72764 203 formatPrintf(createStr(B_TL) ,x,y);
MaxScorda 7:80096ab72764 204 formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y);
MaxScorda 6:f8c90e147000 205 //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y);
MaxScorda 7:80096ab72764 206 formatPrintf(createStr(B_TR),x+w,y);
MaxScorda 3:e72f2addfaf8 207 //corpo
MaxScorda 3:e72f2addfaf8 208 for(int i=y+1; i<y+h; i++) {
MaxScorda 7:80096ab72764 209 formatPrintf(createStr(B_V),x,i);
MaxScorda 7:80096ab72764 210 formatPrintf(createStr(B_V),x+w,i);
MaxScorda 3:e72f2addfaf8 211 }
MaxScorda 3:e72f2addfaf8 212 //riga inferiore
MaxScorda 7:80096ab72764 213 formatPrintf(createStr(B_BL),x,y+h);
MaxScorda 7:80096ab72764 214 // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h);
MaxScorda 7:80096ab72764 215 formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h);
MaxScorda 7:80096ab72764 216 formatPrintf(createStr(B_BR),x+w,y+h);
MaxScorda 5:d045e3561533 217 }
MaxScorda 3:e72f2addfaf8 218
MaxScorda 7:80096ab72764 219 void Terminal::bannerAdv()
MaxScorda 7:80096ab72764 220 {
MaxScorda 9:82803dacf475 221 reset();
MaxScorda 7:80096ab72764 222 cls();
MaxScorda 7:80096ab72764 223 frame(0, 0, 79, 22,1);
MaxScorda 7:80096ab72764 224 forgcol(3);
MaxScorda 9:82803dacf475 225 bold();
MaxScorda 7:80096ab72764 226 formatPrintf("_____ Boot screen _____\n",27,1);
MaxScorda 7:80096ab72764 227 formatPrintf("___ Nucleo Scorda IO Terminal ___\n",22,2);
MaxScorda 8:e3c6d6322506 228
MaxScorda 9:82803dacf475 229 resetattrib();
MaxScorda 8:e3c6d6322506 230 forgcol(7);
MaxScorda 7:80096ab72764 231
MaxScorda 9:82803dacf475 232 //foreground(0xFfFfFf);
MaxScorda 9:82803dacf475 233 formatPrintf(string2char(padstr("\n",78,char(196))),1,3,1); //top 1/2
MaxScorda 7:80096ab72764 234 formatPrintf("Funzione \n",2,4);
MaxScorda 7:80096ab72764 235 formatPrintf("Numero \n",32,4);
MaxScorda 7:80096ab72764 236 formatPrintf("Parametro \n",51,4);
MaxScorda 7:80096ab72764 237 //3-4
MaxScorda 7:80096ab72764 238 formatPrintf("Status Led 1...... \n",2,6);
MaxScorda 7:80096ab72764 239 formatPrintf("Status Virtual Led \n",42,6);
MaxScorda 7:80096ab72764 240 //5-6
MaxScorda 7:80096ab72764 241 formatPrintf("Other Commands.... \n",2,8);
MaxScorda 8:e3c6d6322506 242 formatPrintf("Real Serial(rso/f) \n",42,8);
MaxScorda 7:80096ab72764 243 //7-8
MaxScorda 7:80096ab72764 244 formatPrintf("Input string...... \n",2,10);
MaxScorda 7:80096ab72764 245 formatPrintf("Result............ \n",42,10);
MaxScorda 7:80096ab72764 246 //9-10
MaxScorda 7:80096ab72764 247 //11-12
MaxScorda 9:82803dacf475 248 formatPrintf(string2char(padstr("\n",78,char(196))),1,15,1); //bottom pot
MaxScorda 7:80096ab72764 249
MaxScorda 8:e3c6d6322506 250 formatPrintf(string2char(padstr("\n",78,char(196))),1,20); //azzo funziona...
MaxScorda 8:e3c6d6322506 251 formatPrintf("Serial Feedback \n",2,21);
MaxScorda 7:80096ab72764 252
MaxScorda 7:80096ab72764 253 // grigino
MaxScorda 9:82803dacf475 254 foreground(0xF0F0F0);
MaxScorda 7:80096ab72764 255 formatPrintf(string2char(padstr("\n",78,char(196))),1,5); //top 3/4
MaxScorda 7:80096ab72764 256 formatPrintf(string2char(padstr("\n",78,char(196))),1,7); //top 5/6
MaxScorda 7:80096ab72764 257 formatPrintf(string2char(padstr("\n",78,char(196))),1,9); //top 7/8
MaxScorda 7:80096ab72764 258 formatPrintf(string2char(padstr("\n",78,char(196))),1,11); //top 9/10
MaxScorda 7:80096ab72764 259 formatPrintf(string2char(padstr("\n",78,char(196))),1,13); //top 11/12
MaxScorda 9:82803dacf475 260
MaxScorda 9:82803dacf475 261 foreground(0xFfFfFF);
MaxScorda 8:e3c6d6322506 262 // bold();
MaxScorda 7:80096ab72764 263 }
MaxScorda 7:80096ab72764 264
MaxScorda 6:f8c90e147000 265 void Terminal::readypos()
MaxScorda 6:f8c90e147000 266 {
MaxScorda 8:e3c6d6322506 267 locate(1, 23);
MaxScorda 8:e3c6d6322506 268 this->printf("Command: > ");
MaxScorda 3:e72f2addfaf8 269 }
MaxScorda 6:f8c90e147000 270
MaxScorda 6:f8c90e147000 271
MaxScorda 6:f8c90e147000 272 //------------------------------------------------
MaxScorda 6:f8c90e147000 273 string Terminal::padstr(string sttde, int maxlen, char fillchar)
MaxScorda 6:f8c90e147000 274 {
MaxScorda 6:f8c90e147000 275 bool flagEOS=false;
MaxScorda 6:f8c90e147000 276 string ret=sttde;
MaxScorda 6:f8c90e147000 277 if (ret.size()>0) {
MaxScorda 6:f8c90e147000 278 //se ha EOS lo tolgo
MaxScorda 6:f8c90e147000 279 if (ret.substr(ret.size()-1,1) == "\n") {
MaxScorda 6:f8c90e147000 280 ret=ret.substr(0,ret.size()-1);
MaxScorda 6:f8c90e147000 281 flagEOS=true;
MaxScorda 6:f8c90e147000 282 }
MaxScorda 6:f8c90e147000 283 //pad
MaxScorda 6:f8c90e147000 284 if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar);
MaxScorda 6:f8c90e147000 285 //se aveva EOS, lo rimetto
MaxScorda 6:f8c90e147000 286 if (flagEOS==true) ret=addEOS(ret);
MaxScorda 6:f8c90e147000 287 }
MaxScorda 6:f8c90e147000 288 return ret;
MaxScorda 6:f8c90e147000 289 }
MaxScorda 6:f8c90e147000 290
MaxScorda 6:f8c90e147000 291 string Terminal::addEOS(string sttde)
MaxScorda 6:f8c90e147000 292 {
MaxScorda 6:f8c90e147000 293 string ret=sttde;
MaxScorda 6:f8c90e147000 294 if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n";
MaxScorda 6:f8c90e147000 295 return ret;
MaxScorda 6:f8c90e147000 296 }
MaxScorda 6:f8c90e147000 297
MaxScorda 6:f8c90e147000 298 char* Terminal::string2char(string sttde)
MaxScorda 6:f8c90e147000 299 {
MaxScorda 6:f8c90e147000 300 //ora aggiunge comunque l'EOS. Decidere se parametrizzare
MaxScorda 6:f8c90e147000 301 sttde=addEOS(sttde);
MaxScorda 6:f8c90e147000 302 char *cstr = new char[sttde.length() + 1];
MaxScorda 6:f8c90e147000 303 strcpy(cstr, sttde.c_str());
MaxScorda 6:f8c90e147000 304 // delete [] cstr;
MaxScorda 6:f8c90e147000 305 return cstr;
MaxScorda 7:80096ab72764 306 }
MaxScorda 7:80096ab72764 307
MaxScorda 7:80096ab72764 308 char* Terminal::createStr(char car)
MaxScorda 7:80096ab72764 309 {
MaxScorda 7:80096ab72764 310 // unico modo in cui passo un varole ascii ad una funzione aggiungendo il ritorno a capo.
MaxScorda 7:80096ab72764 311 //se si potesse evitare sarebbe meglio
MaxScorda 7:80096ab72764 312 char *str = (char *) malloc(2 * sizeof(char));
MaxScorda 7:80096ab72764 313 if(str == NULL) return NULL;
MaxScorda 7:80096ab72764 314 str[0] = car;
MaxScorda 7:80096ab72764 315 str[1] = '\0';
MaxScorda 7:80096ab72764 316 return str;
MaxScorda 6:f8c90e147000 317 }