Based on Terminal lib from Simon Ford, some adds

Fork of Terminal by Simon Ford

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::resetattrib()
00124 {
00125     // reset attrib
00126     this->printf("\033[0m");
00127 }
00128 
00129 void Terminal::bold()
00130 {
00131     // font bold
00132     this->printf("\033[1m");
00133 }
00134 
00135 void Terminal::underscore()
00136 {
00137     // underscore
00138     this->printf("\033[4m");
00139 }
00140 
00141 void Terminal::blink()
00142 {
00143     // font blink
00144     this->printf("\033[5m");
00145 }
00146 
00147 void Terminal::reverse()
00148 {
00149     // font reverse
00150     this->printf("\033[7m");
00151 }
00152 //*************************************
00153 void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb, bool boldf )
00154 {
00155     int i=0; //mettere lungo come stringa
00156     //string tempstr=string(sstr);
00157     int screenColumn=78; //per ora lo forziamo
00158 
00159     locate(xx, yy);
00160     if (boldf==1) bold();
00161     this->puts(sstr);
00162     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
00163     while ((sstr[i]!='\n') && (i<(screenColumn-xx+1))) {
00164         i++;
00165     }
00166 
00167     //prosegui col pad
00168     while ((padb>0) && (i<(screenColumn-xx-1))) {
00169         this->printf(" "); //migliorare con stringa unica
00170         i++;
00171         padb--;
00172     }
00173     if (boldf==1) resetattrib();
00174 }
00175 
00176 
00177 void Terminal::frame(int x, int y, int w, int h, int boxtype)
00178 {
00179     char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
00180     // draw frame
00181     // BLOCK=219; SE SI VORRA' USARE IL FILL
00182     switch (boxtype) {
00183         case 0: //singolo
00184             B_H =196;
00185             B_V  =186;
00186             B_TL =201;
00187             B_TR =187;
00188             B_BL= 200;
00189             B_BR =188;
00190             break;
00191 
00192         case 1: //doppio
00193             B_H =205;
00194             B_V  =186;
00195             B_TL =201;
00196             B_TR =187;
00197             B_BL= 200;
00198             B_BR =188;
00199             break;
00200     }
00201     
00202     //riga superiore
00203     formatPrintf(createStr(B_TL) ,x,y);
00204     formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y);
00205     //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y);
00206     formatPrintf(createStr(B_TR),x+w,y);
00207     //corpo
00208     for(int i=y+1; i<y+h; i++) {
00209         formatPrintf(createStr(B_V),x,i);
00210         formatPrintf(createStr(B_V),x+w,i);
00211     }
00212     //riga inferiore
00213     formatPrintf(createStr(B_BL),x,y+h);
00214     // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h);
00215     formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h);
00216     formatPrintf(createStr(B_BR),x+w,y+h);
00217 }
00218 
00219 void Terminal::bannerAdv()
00220 {
00221     reset();
00222     cls();
00223     frame(0, 0, 79, 22,1);
00224     forgcol(3);
00225     bold();
00226     formatPrintf("_____ Boot screen _____\n",27,1);
00227     formatPrintf("___ Nucleo Scorda IO Terminal ___\n",22,2);
00228 
00229     resetattrib();
00230     forgcol(7);
00231 
00232     //foreground(0xFfFfFf);
00233     formatPrintf(string2char(padstr("\n",78,char(196))),1,3,1); //top 1/2
00234     formatPrintf("Funzione \n",2,4);
00235     formatPrintf("Numero \n",32,4);
00236     formatPrintf("Parametro \n",51,4);
00237 //3-4
00238     formatPrintf("Status Led 1...... \n",2,6);
00239     formatPrintf("Status Virtual Led \n",42,6);
00240 //5-6
00241     formatPrintf("Other Commands.... \n",2,8);
00242     formatPrintf("Real Serial(rso/f) \n",42,8);
00243 //7-8
00244     formatPrintf("Input string...... \n",2,10);
00245     formatPrintf("Result............ \n",42,10);
00246 //9-10
00247 //11-12
00248     formatPrintf(string2char(padstr("\n",78,char(196))),1,15,1); //bottom pot
00249 
00250     formatPrintf(string2char(padstr("\n",78,char(196))),1,20); //azzo funziona...
00251     formatPrintf("Serial Feedback \n",2,21);
00252 
00253 // grigino
00254     foreground(0xF0F0F0);
00255     formatPrintf(string2char(padstr("\n",78,char(196))),1,5); //top 3/4
00256     formatPrintf(string2char(padstr("\n",78,char(196))),1,7); //top 5/6
00257     formatPrintf(string2char(padstr("\n",78,char(196))),1,9); //top 7/8
00258     formatPrintf(string2char(padstr("\n",78,char(196))),1,11); //top 9/10
00259     formatPrintf(string2char(padstr("\n",78,char(196))),1,13); //top 11/12
00260     
00261         foreground(0xFfFfFF);
00262     //  bold();
00263 }
00264 
00265 void Terminal::readypos()
00266 {
00267     locate(1, 23);
00268     this->printf("Command: > ");
00269 }
00270 
00271 
00272 //------------------------------------------------
00273 string Terminal::padstr(string sttde, int maxlen, char fillchar)
00274 {
00275     bool flagEOS=false;
00276     string ret=sttde;
00277     if (ret.size()>0) {
00278         //se ha EOS lo tolgo
00279         if (ret.substr(ret.size()-1,1) == "\n") {
00280             ret=ret.substr(0,ret.size()-1);
00281             flagEOS=true;
00282         }
00283         //pad
00284         if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar);
00285         //se aveva EOS, lo rimetto
00286         if (flagEOS==true) ret=addEOS(ret);
00287     }
00288     return ret;
00289 }
00290 
00291 string Terminal::addEOS(string sttde)
00292 {
00293     string ret=sttde;
00294     if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n";
00295     return ret;
00296 }
00297 
00298 char* Terminal::string2char(string sttde)
00299 {
00300     //ora aggiunge comunque l'EOS. Decidere se parametrizzare
00301     sttde=addEOS(sttde);
00302     char *cstr = new char[sttde.length() + 1];
00303     strcpy(cstr, sttde.c_str());
00304     // delete [] cstr;
00305     return cstr;
00306 }
00307 
00308 char* Terminal::createStr(char car)
00309 {
00310     // unico modo in cui passo un varole ascii ad una funzione aggiungendo il ritorno a capo.
00311     //se si potesse evitare sarebbe meglio
00312     char *str = (char *) malloc(2 * sizeof(char));
00313     if(str == NULL) return NULL;
00314     str[0] = car;
00315     str[1] = '\0';
00316     return str;
00317 }