Based on Terminal lib from Simon Ford, some adds
Fork of TerminalPlus by
Terminal.cpp@6:f8c90e147000, 2015-09-16 (annotated)
- Committer:
- MaxScorda
- Date:
- Wed Sep 16 00:08:12 2015 +0000
- Revision:
- 6:f8c90e147000
- Parent:
- 5:d045e3561533
- Child:
- 7:80096ab72764
Color functions, layout ok
Who changed what in which revision?
User | Revision | Line number | New 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 | |
MaxScorda | 6:f8c90e147000 | 23 | |
MaxScorda | 6:f8c90e147000 | 24 | /* |
MaxScorda | 6:f8c90e147000 | 25 | Porre il terminate in VT100 e set di caratteri cp437 |
MaxScorda | 6:f8c90e147000 | 26 | */ |
MaxScorda | 6:f8c90e147000 | 27 | |
simon | 0:2bf27af3c759 | 28 | #include "Terminal.h" |
simon | 0:2bf27af3c759 | 29 | #include "mbed.h" |
simon | 0:2bf27af3c759 | 30 | |
simon | 0:2bf27af3c759 | 31 | Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {} |
simon | 0:2bf27af3c759 | 32 | |
MaxScorda | 3:e72f2addfaf8 | 33 | void Terminal::cls() |
MaxScorda | 3:e72f2addfaf8 | 34 | { |
simon | 0:2bf27af3c759 | 35 | this->printf("\033[2J"); |
simon | 0:2bf27af3c759 | 36 | } |
simon | 0:2bf27af3c759 | 37 | |
MaxScorda | 3:e72f2addfaf8 | 38 | void Terminal::locate(int column, int row) |
MaxScorda | 3:e72f2addfaf8 | 39 | { |
simon | 0:2bf27af3c759 | 40 | // Cursor Home <ESC>[{ROW};{COLUMN}H |
MaxScorda | 3:e72f2addfaf8 | 41 | |
MaxScorda | 3:e72f2addfaf8 | 42 | // this->printf("\033[%d;%dH%c", row + 1, column + 1); //original |
MaxScorda | 5:d045e3561533 | 43 | this->printf("\033[%d;%dH", row+1 , column+1 ); |
simon | 0:2bf27af3c759 | 44 | } |
simon | 0:2bf27af3c759 | 45 | |
MaxScorda | 3:e72f2addfaf8 | 46 | static int rgb888tobgr111(int colour) |
MaxScorda | 3:e72f2addfaf8 | 47 | { |
simon | 0:2bf27af3c759 | 48 | int r = (colour >> 23) & 1; |
simon | 0:2bf27af3c759 | 49 | int g = (colour >> 15) & 1; |
simon | 0:2bf27af3c759 | 50 | int b = (colour >> 7) & 1; |
simon | 0:2bf27af3c759 | 51 | return (b << 2) | (g << 1) | (r << 0); |
simon | 0:2bf27af3c759 | 52 | } |
simon | 0:2bf27af3c759 | 53 | |
MaxScorda | 3:e72f2addfaf8 | 54 | void Terminal::foreground(int colour) |
MaxScorda | 3:e72f2addfaf8 | 55 | { |
simon | 0:2bf27af3c759 | 56 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 57 | // Foreground Colours : 30 + bgr |
simon | 0:2bf27af3c759 | 58 | int c = 30 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 59 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 60 | } |
simon | 0:2bf27af3c759 | 61 | |
MaxScorda | 6:f8c90e147000 | 62 | void Terminal::forgcol(int color) |
MaxScorda | 6:f8c90e147000 | 63 | { |
MaxScorda | 6:f8c90e147000 | 64 | /*colori da 0 a 7 |
MaxScorda | 6:f8c90e147000 | 65 | [ 3 0 m setaf 0 Set foreground to color #0 - black |
MaxScorda | 6:f8c90e147000 | 66 | [ 3 1 m setaf 1 Set foreground to color #1 - red |
MaxScorda | 6:f8c90e147000 | 67 | [ 3 2 m setaf 2 Set foreground to color #2 - green |
MaxScorda | 6:f8c90e147000 | 68 | [ 3 3 m setaf 3 Set foreground to color #3 - yellow |
MaxScorda | 6:f8c90e147000 | 69 | [ 3 4 m setaf 4 Set foreground to color #4 - blue |
MaxScorda | 6:f8c90e147000 | 70 | [ 3 5 m setaf 5 Set foreground to color #5 - magenta |
MaxScorda | 6:f8c90e147000 | 71 | [ 3 6 m setaf 6 Set foreground to color #6 - cyan |
MaxScorda | 6:f8c90e147000 | 72 | [ 3 7 m setaf 7 Set foreground to color #7 - white |
MaxScorda | 6:f8c90e147000 | 73 | [ 3 9 m setaf 9 Set default color as foreground color |
MaxScorda | 6:f8c90e147000 | 74 | */ |
MaxScorda | 6:f8c90e147000 | 75 | |
MaxScorda | 6:f8c90e147000 | 76 | if ((color<0) || (color>7)) color =7; //normalizza |
MaxScorda | 6:f8c90e147000 | 77 | color=color+30; |
MaxScorda | 6:f8c90e147000 | 78 | this->printf("\033[%dm", color); |
MaxScorda | 6:f8c90e147000 | 79 | } |
MaxScorda | 6:f8c90e147000 | 80 | |
MaxScorda | 6:f8c90e147000 | 81 | void Terminal::bckgcol(int color) |
MaxScorda | 6:f8c90e147000 | 82 | { |
MaxScorda | 6:f8c90e147000 | 83 | /*colori da 0 a 7 |
MaxScorda | 6:f8c90e147000 | 84 | [ 4 0 m setab 0 Set background to color #0 - black |
MaxScorda | 6:f8c90e147000 | 85 | [ 4 1 m setab 1 Set background to color #1 - red |
MaxScorda | 6:f8c90e147000 | 86 | [ 4 2 m setab 2 Set background to color #2 - green |
MaxScorda | 6:f8c90e147000 | 87 | [ 4 3 m setab 3 Set background to color #3 - yellow |
MaxScorda | 6:f8c90e147000 | 88 | [ 4 4 m setab 4 Set background to color #4 - blue |
MaxScorda | 6:f8c90e147000 | 89 | [ 4 5 m setab 5 Set background to color #5 - magenta |
MaxScorda | 6:f8c90e147000 | 90 | [ 4 6 m setab 6 Set background to color #6 - cyan |
MaxScorda | 6:f8c90e147000 | 91 | [ 4 7 m setab 7 Set background to color #7 - white |
MaxScorda | 6:f8c90e147000 | 92 | [ 4 9 m setaf 9 Set default color as background color |
MaxScorda | 6:f8c90e147000 | 93 | */ |
MaxScorda | 6:f8c90e147000 | 94 | |
MaxScorda | 6:f8c90e147000 | 95 | if ((color<0) || (color>7)) color =0; //normalizza |
MaxScorda | 6:f8c90e147000 | 96 | color=color+40; |
MaxScorda | 6:f8c90e147000 | 97 | this->printf("\033[%dm", color); |
MaxScorda | 6:f8c90e147000 | 98 | } |
MaxScorda | 6:f8c90e147000 | 99 | |
MaxScorda | 6:f8c90e147000 | 100 | |
MaxScorda | 3:e72f2addfaf8 | 101 | void Terminal::background(int colour) |
MaxScorda | 3:e72f2addfaf8 | 102 | { |
simon | 0:2bf27af3c759 | 103 | // Set Attribute Mode <ESC>[{n}m |
simon | 0:2bf27af3c759 | 104 | // Background Colours : 40 + bgr |
simon | 0:2bf27af3c759 | 105 | int c = 40 + rgb888tobgr111(colour); |
simon | 0:2bf27af3c759 | 106 | this->printf("\033[%dm", c); |
simon | 0:2bf27af3c759 | 107 | } |
MaxScorda | 3:e72f2addfaf8 | 108 | |
MaxScorda | 5:d045e3561533 | 109 | void Terminal::reset() |
MaxScorda | 5:d045e3561533 | 110 | { |
MaxScorda | 5:d045e3561533 | 111 | // Reset Screen |
MaxScorda | 5:d045e3561533 | 112 | this->printf("\033c"); |
MaxScorda | 5:d045e3561533 | 113 | } |
MaxScorda | 5:d045e3561533 | 114 | |
MaxScorda | 5:d045e3561533 | 115 | void Terminal::eraseline() |
MaxScorda | 5:d045e3561533 | 116 | { |
MaxScorda | 5:d045e3561533 | 117 | // Reset Screen |
MaxScorda | 5:d045e3561533 | 118 | this->printf("\033[2K"); |
MaxScorda | 5:d045e3561533 | 119 | } |
MaxScorda | 5:d045e3561533 | 120 | |
MaxScorda | 5:d045e3561533 | 121 | |
MaxScorda | 5:d045e3561533 | 122 | //************************************* |
MaxScorda | 5:d045e3561533 | 123 | void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb) |
MaxScorda | 5:d045e3561533 | 124 | { |
MaxScorda | 5:d045e3561533 | 125 | char i=0; //mettere lungo come stringa |
MaxScorda | 5:d045e3561533 | 126 | int screenColumn=80; //per ora lo forziamo |
MaxScorda | 5:d045e3561533 | 127 | locate(xx, yy); |
MaxScorda | 5:d045e3561533 | 128 | |
MaxScorda | 5:d045e3561533 | 129 | //this->printf("\033[%d;%dH", xx, yy); |
MaxScorda | 5:d045e3561533 | 130 | this->printf(sstr); |
MaxScorda | 5:d045e3561533 | 131 | |
MaxScorda | 6:f8c90e147000 | 132 | /* |
MaxScorda | 6:f8c90e147000 | 133 | //prosegui col pad |
MaxScorda | 6:f8c90e147000 | 134 | while ((padb>0) && (i<(screenColumn-xx-1))) { |
MaxScorda | 6:f8c90e147000 | 135 | this->printf(" "); //migliorare con stringa unica |
MaxScorda | 6:f8c90e147000 | 136 | i++; |
MaxScorda | 6:f8c90e147000 | 137 | padb--; |
MaxScorda | 6:f8c90e147000 | 138 | } |
MaxScorda | 6:f8c90e147000 | 139 | */ |
MaxScorda | 5:d045e3561533 | 140 | } |
MaxScorda | 5:d045e3561533 | 141 | |
MaxScorda | 5:d045e3561533 | 142 | |
MaxScorda | 4:ee2311717b80 | 143 | void Terminal::frame(int x, int y, int w, int h, int boxtype) |
MaxScorda | 3:e72f2addfaf8 | 144 | { |
MaxScorda | 4:ee2311717b80 | 145 | char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ; |
MaxScorda | 4:ee2311717b80 | 146 | // draw frame |
MaxScorda | 4:ee2311717b80 | 147 | // BLOCK=219; SE SI VORRA' USARE IL FILL |
MaxScorda | 3:e72f2addfaf8 | 148 | switch (boxtype) { |
MaxScorda | 3:e72f2addfaf8 | 149 | case 0: //singolo |
MaxScorda | 6:f8c90e147000 | 150 | B_H =196; |
MaxScorda | 4:ee2311717b80 | 151 | B_V =186; |
MaxScorda | 4:ee2311717b80 | 152 | B_TL =201; |
MaxScorda | 4:ee2311717b80 | 153 | B_TR =187; |
MaxScorda | 4:ee2311717b80 | 154 | B_BL= 200; |
MaxScorda | 4:ee2311717b80 | 155 | B_BR =188; |
MaxScorda | 3:e72f2addfaf8 | 156 | break; |
MaxScorda | 3:e72f2addfaf8 | 157 | |
MaxScorda | 3:e72f2addfaf8 | 158 | case 1: //doppio |
MaxScorda | 3:e72f2addfaf8 | 159 | B_H =205; |
MaxScorda | 3:e72f2addfaf8 | 160 | B_V =186; |
MaxScorda | 3:e72f2addfaf8 | 161 | B_TL =201; |
MaxScorda | 3:e72f2addfaf8 | 162 | B_TR =187; |
MaxScorda | 3:e72f2addfaf8 | 163 | B_BL= 200; |
MaxScorda | 3:e72f2addfaf8 | 164 | B_BR =188; |
MaxScorda | 3:e72f2addfaf8 | 165 | break; |
MaxScorda | 3:e72f2addfaf8 | 166 | } |
MaxScorda | 3:e72f2addfaf8 | 167 | //riga superiore |
MaxScorda | 6:f8c90e147000 | 168 | formatPrintf((char *) &B_TL ,x,y); |
MaxScorda | 6:f8c90e147000 | 169 | formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y); |
MaxScorda | 6:f8c90e147000 | 170 | //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y); |
MaxScorda | 6:f8c90e147000 | 171 | formatPrintf(&B_TR,x+w,y); |
MaxScorda | 3:e72f2addfaf8 | 172 | //corpo |
MaxScorda | 3:e72f2addfaf8 | 173 | for(int i=y+1; i<y+h; i++) { |
MaxScorda | 6:f8c90e147000 | 174 | formatPrintf(&B_V,x,i); |
MaxScorda | 6:f8c90e147000 | 175 | formatPrintf(&B_V,x+w,i); |
MaxScorda | 3:e72f2addfaf8 | 176 | } |
MaxScorda | 3:e72f2addfaf8 | 177 | //riga inferiore |
MaxScorda | 6:f8c90e147000 | 178 | formatPrintf(&B_BL,x,y+h); |
MaxScorda | 6:f8c90e147000 | 179 | // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h); |
MaxScorda | 6:f8c90e147000 | 180 | formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h); |
MaxScorda | 6:f8c90e147000 | 181 | formatPrintf(&B_BR,x+w,y+h); |
MaxScorda | 5:d045e3561533 | 182 | } |
MaxScorda | 3:e72f2addfaf8 | 183 | |
MaxScorda | 5:d045e3561533 | 184 | void Terminal::banner() |
MaxScorda | 5:d045e3561533 | 185 | { |
MaxScorda | 6:f8c90e147000 | 186 | cls(); |
MaxScorda | 6:f8c90e147000 | 187 | frame(0, 0, 79, 22,1); |
MaxScorda | 6:f8c90e147000 | 188 | forgcol(3); |
MaxScorda | 6:f8c90e147000 | 189 | formatPrintf("_____ Boot screen _____\n",27,1); |
MaxScorda | 6:f8c90e147000 | 190 | formatPrintf("___ Nucleo Scorda IO Terminal ___\n",22,2); |
MaxScorda | 6:f8c90e147000 | 191 | forgcol(9); |
MaxScorda | 6:f8c90e147000 | 192 | formatPrintf(string2char(padstr("\n",78,char(196))),1,5); //azzo funziona... |
MaxScorda | 5:d045e3561533 | 193 | formatPrintf("Parsing \n",2,5); |
MaxScorda | 5:d045e3561533 | 194 | formatPrintf("Funzione\n",2,7); |
MaxScorda | 5:d045e3561533 | 195 | formatPrintf("Numero\n",22,7); |
MaxScorda | 5:d045e3561533 | 196 | formatPrintf("Parametro\n",42,7); |
MaxScorda | 6:f8c90e147000 | 197 | formatPrintf(string2char(padstr("\n",78,char(196))),1,10); //azzo funziona... |
MaxScorda | 5:d045e3561533 | 198 | formatPrintf("Status \n",2,10); |
MaxScorda | 5:d045e3561533 | 199 | formatPrintf("Led 1 \n",2,12); |
MaxScorda | 5:d045e3561533 | 200 | formatPrintf("Virtual Led \n",22,12); |
MaxScorda | 5:d045e3561533 | 201 | formatPrintf("Other Commands \n",42,12); |
MaxScorda | 5:d045e3561533 | 202 | formatPrintf("Real Out Serial\n",62,12); |
MaxScorda | 5:d045e3561533 | 203 | formatPrintf("Input string\n",2,15); |
MaxScorda | 5:d045e3561533 | 204 | formatPrintf("Result\n",42,15); |
MaxScorda | 6:f8c90e147000 | 205 | formatPrintf(string2char(padstr("\n",78,char(196))),1,18); //azzo funziona... |
MaxScorda | 6:f8c90e147000 | 206 | formatPrintf("Serial Feedback \n",2,18); |
MaxScorda | 6:f8c90e147000 | 207 | } |
MaxScorda | 6:f8c90e147000 | 208 | void Terminal::readypos() |
MaxScorda | 6:f8c90e147000 | 209 | { |
MaxScorda | 6:f8c90e147000 | 210 | formatPrintf("Command: > ",1,23); |
MaxScorda | 3:e72f2addfaf8 | 211 | } |
MaxScorda | 6:f8c90e147000 | 212 | |
MaxScorda | 6:f8c90e147000 | 213 | |
MaxScorda | 6:f8c90e147000 | 214 | //------------------------------------------------ |
MaxScorda | 6:f8c90e147000 | 215 | string Terminal::padstr(string sttde, int maxlen, char fillchar) |
MaxScorda | 6:f8c90e147000 | 216 | { |
MaxScorda | 6:f8c90e147000 | 217 | bool flagEOS=false; |
MaxScorda | 6:f8c90e147000 | 218 | string ret=sttde; |
MaxScorda | 6:f8c90e147000 | 219 | if (ret.size()>0) { |
MaxScorda | 6:f8c90e147000 | 220 | //se ha EOS lo tolgo |
MaxScorda | 6:f8c90e147000 | 221 | if (ret.substr(ret.size()-1,1) == "\n") { |
MaxScorda | 6:f8c90e147000 | 222 | ret=ret.substr(0,ret.size()-1); |
MaxScorda | 6:f8c90e147000 | 223 | flagEOS=true; |
MaxScorda | 6:f8c90e147000 | 224 | } |
MaxScorda | 6:f8c90e147000 | 225 | //pad |
MaxScorda | 6:f8c90e147000 | 226 | if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar); |
MaxScorda | 6:f8c90e147000 | 227 | //se aveva EOS, lo rimetto |
MaxScorda | 6:f8c90e147000 | 228 | if (flagEOS==true) ret=addEOS(ret); |
MaxScorda | 6:f8c90e147000 | 229 | } |
MaxScorda | 6:f8c90e147000 | 230 | return ret; |
MaxScorda | 6:f8c90e147000 | 231 | } |
MaxScorda | 6:f8c90e147000 | 232 | |
MaxScorda | 6:f8c90e147000 | 233 | string Terminal::addEOS(string sttde) |
MaxScorda | 6:f8c90e147000 | 234 | { |
MaxScorda | 6:f8c90e147000 | 235 | string ret=sttde; |
MaxScorda | 6:f8c90e147000 | 236 | if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n"; |
MaxScorda | 6:f8c90e147000 | 237 | return ret; |
MaxScorda | 6:f8c90e147000 | 238 | } |
MaxScorda | 6:f8c90e147000 | 239 | |
MaxScorda | 6:f8c90e147000 | 240 | char* Terminal::string2char(string sttde) |
MaxScorda | 6:f8c90e147000 | 241 | { |
MaxScorda | 6:f8c90e147000 | 242 | //ora aggiunge comunque l'EOS. Decidere se parametrizzare |
MaxScorda | 6:f8c90e147000 | 243 | sttde=addEOS(sttde); |
MaxScorda | 6:f8c90e147000 | 244 | char *cstr = new char[sttde.length() + 1]; |
MaxScorda | 6:f8c90e147000 | 245 | strcpy(cstr, sttde.c_str()); |
MaxScorda | 6:f8c90e147000 | 246 | // delete [] cstr; |
MaxScorda | 6:f8c90e147000 | 247 | return cstr; |
MaxScorda | 6:f8c90e147000 | 248 | } |