Based on Terminal lib from Simon Ford, some adds

Fork of TerminalPlus by Max Scordamaglia

Terminal.cpp

Committer:
MaxScorda
Date:
2015-09-16
Revision:
6:f8c90e147000
Parent:
5:d045e3561533
Child:
7:80096ab72764

File content as of revision 6:f8c90e147000:

/* mbed Terminal Library, for ANSI/VT200 Terminals and ecape codes
 * Copyright (c) 2007-2010, sford, http://mbed.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


/*
Porre il terminate in VT100 e set di caratteri cp437
*/

#include "Terminal.h"
#include "mbed.h"

Terminal::Terminal(PinName tx, PinName rx) : Serial(tx, rx) {}

void Terminal::cls()
{
    this->printf("\033[2J");
}

void Terminal::locate(int column, int row)
{
    // Cursor Home    <ESC>[{ROW};{COLUMN}H

    //  this->printf("\033[%d;%dH%c", row + 1, column + 1); //original
    this->printf("\033[%d;%dH", row+1 , column+1 );
}

static int rgb888tobgr111(int colour)
{
    int r = (colour >> 23) & 1;
    int g = (colour >> 15) & 1;
    int b = (colour >> 7) & 1;
    return (b << 2) | (g << 1) | (r << 0);
}

void Terminal::foreground(int colour)
{
    // Set Attribute Mode    <ESC>[{n}m
    // Foreground Colours : 30 + bgr
    int c = 30 + rgb888tobgr111(colour);
    this->printf("\033[%dm", c);
}

void Terminal::forgcol(int color)
{
    /*colori da 0 a 7
    [ 3 0 m setaf 0 Set foreground to color #0 - black
    [ 3 1 m setaf 1 Set foreground to color #1 - red
    [ 3 2 m setaf 2 Set foreground to color #2 - green
    [ 3 3 m setaf 3 Set foreground to color #3 - yellow
    [ 3 4 m setaf 4 Set foreground to color #4 - blue
    [ 3 5 m setaf 5 Set foreground to color #5 - magenta
    [ 3 6 m setaf 6 Set foreground to color #6 - cyan
    [ 3 7 m setaf 7 Set foreground to color #7 - white
    [ 3 9 m setaf 9 Set default color as foreground color
    */

    if ((color<0) || (color>7)) color =7; //normalizza
    color=color+30;
    this->printf("\033[%dm", color);
}

void Terminal::bckgcol(int color)
{
    /*colori da 0 a 7
    [ 4 0 m setab 0 Set background to color #0 - black
    [ 4 1 m setab 1 Set background to color #1 - red
    [ 4 2 m setab 2 Set background to color #2 - green
    [ 4 3 m setab 3 Set background to color #3 - yellow
    [ 4 4 m setab 4 Set background to color #4 - blue
    [ 4 5 m setab 5 Set background to color #5 - magenta
    [ 4 6 m setab 6 Set background to color #6 - cyan
    [ 4 7 m setab 7 Set background to color #7 - white
    [ 4 9 m setaf 9 Set default color as background color
    */

    if ((color<0) || (color>7)) color =0; //normalizza
    color=color+40;
    this->printf("\033[%dm", color);
}


void Terminal::background(int colour)
{
    // Set Attribute Mode    <ESC>[{n}m
    // Background Colours : 40 + bgr
    int c = 40 + rgb888tobgr111(colour);
    this->printf("\033[%dm", c);
}

void Terminal::reset()
{
    // Reset Screen
    this->printf("\033c");
}

void Terminal::eraseline()
{
    // Reset Screen
    this->printf("\033[2K");
}


//*************************************
void Terminal::formatPrintf(char sstr[], int xx, int yy, int padb)
{
    char i=0; //mettere lungo come stringa
    int screenColumn=80; //per ora lo forziamo
    locate(xx, yy);

    //this->printf("\033[%d;%dH", xx, yy);
    this->printf(sstr);

    /*
        //prosegui col pad
        while ((padb>0) && (i<(screenColumn-xx-1))) {
            this->printf(" "); //migliorare con stringa unica
            i++;
            padb--;
        }
    */
}


void Terminal::frame(int x, int y, int w, int h, int boxtype)
{
    char B_H=0, B_V=0, B_TL=0, B_TR=0, B_BL=0, B_BR=0 ;
    // draw frame
    // BLOCK=219; SE SI VORRA' USARE IL FILL
    switch (boxtype) {
        case 0: //singolo
            B_H =196;
            B_V  =186;
            B_TL =201;
            B_TR =187;
            B_BL= 200;
            B_BR =188;
            break;

        case 1: //doppio
            B_H =205;
            B_V  =186;
            B_TL =201;
            B_TR =187;
            B_BL= 200;
            B_BR =188;
            break;
    }
    //riga superiore
    formatPrintf((char *) &B_TL ,x,y);
     formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y); 
    //for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y);
    formatPrintf(&B_TR,x+w,y);
    //corpo
    for(int i=y+1; i<y+h; i++) {
        formatPrintf(&B_V,x,i);
        formatPrintf(&B_V,x+w,i);
    }
    //riga inferiore
    formatPrintf(&B_BL,x,y+h);
   // for(int i=x+1; i<x+w; i++) formatPrintf(&B_H,i,y+h);
        formatPrintf(string2char(padstr("\n",78,char(B_H))),x+1,y+h); 
    formatPrintf(&B_BR,x+w,y+h);
}

void Terminal::banner()
{
    cls();
    frame(0, 0, 79, 22,1);
    forgcol(3);
    formatPrintf("_____ Boot screen _____\n",27,1);
    formatPrintf("___ Nucleo Scorda IO Terminal ___\n",22,2);
    forgcol(9);
    formatPrintf(string2char(padstr("\n",78,char(196))),1,5); //azzo funziona...
    formatPrintf("Parsing \n",2,5);
    formatPrintf("Funzione\n",2,7);
    formatPrintf("Numero\n",22,7);
    formatPrintf("Parametro\n",42,7);
    formatPrintf(string2char(padstr("\n",78,char(196))),1,10); //azzo funziona...
    formatPrintf("Status \n",2,10);
    formatPrintf("Led 1 \n",2,12);
    formatPrintf("Virtual Led \n",22,12);
    formatPrintf("Other Commands \n",42,12);
    formatPrintf("Real Out Serial\n",62,12);
    formatPrintf("Input string\n",2,15);
    formatPrintf("Result\n",42,15);
    formatPrintf(string2char(padstr("\n",78,char(196))),1,18); //azzo funziona...
    formatPrintf("Serial Feedback \n",2,18);
}
void Terminal::readypos()
{
    formatPrintf("Command: > ",1,23);
}


//------------------------------------------------
string Terminal::padstr(string sttde, int maxlen, char fillchar)
{
    bool flagEOS=false;
    string ret=sttde;
    if (ret.size()>0) {
        //se ha EOS lo tolgo
        if (ret.substr(ret.size()-1,1) == "\n") {
            ret=ret.substr(0,ret.size()-1);
            flagEOS=true;
        }
        //pad
        if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar);
        //se aveva EOS, lo rimetto
        if (flagEOS==true) ret=addEOS(ret);
    }
    return ret;
}

string Terminal::addEOS(string sttde)
{
    string ret=sttde;
    if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n";
    return ret;
}

char* Terminal::string2char(string sttde)
{
    //ora aggiunge comunque l'EOS. Decidere se parametrizzare
    sttde=addEOS(sttde);
    char *cstr = new char[sttde.length() + 1];
    strcpy(cstr, sttde.c_str());
    // delete [] cstr;
    return cstr;
}