Droni e Droidi / TerminalPlusV2

Fork of TerminalPlus by Max Scordamaglia

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Terminal.cpp Source File

Terminal.cpp

00001 /** mbed TerminalPlus Library, for ANSI/VT200 Terminals and ecape codes
00002  * Copyright (c) 2015, Max Scordamaglia , https//developer.mbed.org/users/MaxScorda/
00003  * fork from Terminal Library
00004  * Copyright (c) 2007-2010, sford, http://mbed.org
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 
00026 /*
00027 Porre il terminate in VT100 e set di caratteri cp437
00028 */
00029 
00030 #include "Terminal.h"
00031 #include "mbed.h"
00032 
00033 Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}
00034 
00035 void Terminal::cls()
00036 {
00037     this->printf("\033[2J");
00038 }
00039 
00040 void Terminal::locate(int column, int row)
00041 {
00042     // Cursor Home    <ESC>[{ROW};{COLUMN}H
00043 
00044     //  this->printf("\033[%d;%dH%c", row + 1, column + 1); //original
00045     this->printf("\033[%d;%dH", row+1 , column+1 );
00046 }
00047 
00048 static int rgb888tobgr111(int colour)
00049 {
00050     int r = (colour >> 23) & 1;
00051     int g = (colour >> 15) & 1;
00052     int b = (colour >> 7) & 1;
00053     return (b << 2) | (g << 1) | (r << 0);
00054 }
00055 
00056 void Terminal::foreground(int colour)
00057 {
00058     // Set Attribute Mode    <ESC>[{n}m
00059     // Foreground Colours : 30 + bgr
00060     int c = 30 + rgb888tobgr111(colour);
00061     this->printf("\033[%dm", c);
00062 }
00063 
00064 void Terminal::forgcol(int color)
00065 {
00066     /*colori da 0 a 7
00067     [ 3 0 m setaf 0 Set foreground to color #0 - black
00068     [ 3 1 m setaf 1 Set foreground to color #1 - red
00069     [ 3 2 m setaf 2 Set foreground to color #2 - green
00070     [ 3 3 m setaf 3 Set foreground to color #3 - yellow
00071     [ 3 4 m setaf 4 Set foreground to color #4 - blue
00072     [ 3 5 m setaf 5 Set foreground to color #5 - magenta
00073     [ 3 6 m setaf 6 Set foreground to color #6 - cyan
00074     [ 3 7 m setaf 7 Set foreground to color #7 - white
00075     [ 3 9 m setaf 9 Set default color as foreground color
00076     */
00077 
00078     if ((color<0) || (color>7)) color =7; //normalizza
00079     color=color+30;
00080     this->printf("\033[%dm", color);
00081 }
00082 
00083 void Terminal::bckgcol(int color)
00084 {
00085     /*colori da 0 a 7
00086     [ 4 0 m setab 0 Set background to color #0 - black
00087     [ 4 1 m setab 1 Set background to color #1 - red
00088     [ 4 2 m setab 2 Set background to color #2 - green
00089     [ 4 3 m setab 3 Set background to color #3 - yellow
00090     [ 4 4 m setab 4 Set background to color #4 - blue
00091     [ 4 5 m setab 5 Set background to color #5 - magenta
00092     [ 4 6 m setab 6 Set background to color #6 - cyan
00093     [ 4 7 m setab 7 Set background to color #7 - white
00094     [ 4 9 m setaf 9 Set default color as background color
00095     */
00096 
00097     if ((color<0) || (color>7)) color =0; //normalizza
00098     color=color+40;
00099     this->printf("\033[%dm", color);
00100 }
00101 
00102 
00103 void Terminal::background(int colour)
00104 {
00105     // Set Attribute Mode    <ESC>[{n}m
00106     // Background Colours : 40 + bgr
00107     int c = 40 + rgb888tobgr111(colour);
00108     this->printf("\033[%dm", c);
00109 }
00110 
00111 void Terminal::reset()
00112 {
00113     // Reset Screen
00114     this->printf("\033c");
00115 }
00116 
00117 void Terminal::eraseline()
00118 {
00119     // Erase full line, cursor save
00120     this->printf("\033[2K");
00121 }
00122 
00123 void Terminal::erasesquare(int x1, int y1, int x2, int y2)
00124 {
00125    // cancella un quadrato dello schermo
00126    for(int i=y1; i<y2; i++) {
00127     //formatPrintf("*",x1,i,x2-x1);
00128     formatPrintf(string2char(padstr(" ",x2-x1,' ')),x1,i);
00129     }
00130     
00131 }
00132 void Terminal::resetattrib()
00133 {
00134     // reset attrib
00135     this->printf("\033[0m");
00136 }
00137 
00138 void Terminal::bold()
00139 {
00140     // font bold
00141     this->printf("\033[1m");
00142 }
00143 
00144 void Terminal::underscore()
00145 {
00146     // underscore
00147     this->printf("\033[4m");
00148 }
00149 
00150 void Terminal::blink()
00151 {
00152     // font blink
00153     this->printf("\033[5m");
00154 }
00155 
00156 void Terminal::reverse()
00157 {
00158     // font reverse
00159     this->printf("\033[7m");
00160 }
00161 //*************************************
00162 void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb, bool boldf )
00163 {
00164     int i=0; //mettere lungo come stringa
00165     //string tempstr=string(sstr);
00166     int screenColumn=78; //per ora lo forziamo
00167 
00168     locate(xx, yy);
00169     if (boldf==1) bold();
00170     this->puts(sstr);
00171     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
00172     while ((sstr[i]!='\n') && (i<(screenColumn-xx+1))) {
00173         i++;
00174     }
00175 
00176     //prosegui col pad
00177     while ((padb>0) && (i<(screenColumn-xx-1))) {
00178         this->printf(" "); //migliorare con stringa unica
00179         i++;
00180         padb--;
00181     }
00182     if (boldf==1) resetattrib();
00183 }
00184 
00185 
00186 void Terminal::frame(int x, int y, int w, int h, int boxtype)
00187 {
00188     char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
00189     // draw frame
00190     // BLOCK=219; SE SI VORRA' USARE IL FILL
00191     switch (boxtype) {
00192         case 0: //singolo
00193             B_H  =196;
00194             B_V  =179;
00195             B_TL =218;
00196             B_TR =191;
00197             B_BL =192;
00198             B_BR =217;
00199             break;
00200 
00201         case 1: //doppio
00202             B_H  =205;
00203             B_V  =186;
00204             B_TL =201;
00205             B_TR =187;
00206             B_BL =200;
00207             B_BR =188;
00208             break;
00209 
00210         case 2: //misto 1
00211             B_H  =196;
00212             B_V  =186;
00213             B_TL =214;
00214             B_TR =183;
00215             B_BL =212;
00216             B_BR =189;
00217             break;
00218     }
00219 
00220     //riga superiore
00221     formatPrintf(createStr(B_TL) ,x,y);
00222     formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y);
00223     //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y);
00224     formatPrintf(createStr(B_TR),x+w,y);
00225     //corpo
00226     for(int i=y+1; i<y+h; i++) {
00227         formatPrintf(createStr(B_V),x,i);
00228         formatPrintf(createStr(B_V),x+w,i);
00229     }
00230     //riga inferiore
00231     formatPrintf(createStr(B_BL),x,y+h);
00232     // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h);
00233     formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h);
00234     formatPrintf(createStr(B_BR),x+w,y+h);
00235 }
00236 
00237 void Terminal::readypos()
00238 {
00239     locate(11, 23);
00240     //this->printf("Command: > ");
00241 }
00242 
00243 
00244 //------------------------------------------------
00245 string Terminal::padstr(string sttde, int maxlen, char fillchar)
00246 {
00247     bool flagEOS=false;
00248     string ret=sttde;
00249     if (ret.size()>0) {
00250         //se ha EOS lo tolgo
00251         if (ret.substr(ret.size()-1,1) == "\n") {
00252             ret=ret.substr(0,ret.size()-1);
00253             flagEOS=true;
00254         }
00255         //pad
00256         if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar);
00257         //se aveva EOS, lo rimetto
00258         if (flagEOS==true) ret=addEOS(ret);
00259     }
00260     return ret;
00261 }
00262 
00263 string Terminal::addEOS(string sttde)
00264 {
00265     string ret=sttde;
00266     if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n";
00267     return ret;
00268 }
00269 
00270 char* Terminal::string2char(string sttde)
00271 {
00272     //ora aggiunge comunque l'EOS. Decidere se parametrizzare
00273     sttde=addEOS(sttde);
00274     char *cstr = new char[sttde.length() + 1];
00275     strcpy(cstr, sttde.c_str());
00276     // delete [] cstr;
00277     return cstr;
00278 }
00279 
00280 char* Terminal::createStr(char car)
00281 {
00282     // unico modo in cui passo un varole ascii ad una funzione aggiungendo il ritorno a capo.
00283     //se si potesse evitare sarebbe meglio
00284     char *str = (char *) malloc(2 * sizeof(char));
00285     if(str == NULL) return NULL;
00286     str[0] = car;
00287     str[1] = '\0';
00288     return str;
00289 }