Based on Terminal lib from Simon Ford, some adds

Fork of TerminalPlus by Max Scordamaglia

Terminal.cpp

Committer:
MaxScorda
Date:
2015-09-14
Revision:
5:d045e3561533
Parent:
4:ee2311717b80
Child:
6:f8c90e147000

File content as of revision 5:d045e3561533:

/* 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.
 */

#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::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 =205;
            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
    this->printf("\033[%d;%dH", x, y);
    this->printf((char *) B_TL);
    for(int i=x+1; i<x+w; i++) this->printf((char *) B_H);
    this->printf((char *) B_TR);
    //corpo
    for(int i=y+1; i<y+h; i++) {
        this->printf("\033[%d;%dH", x, i);
        this->printf((char *) B_V);
        this->printf("\033[%d;%dH", x+w, i);
        this->printf((char *) B_V);
    }
    //riga inferiore
    this->printf("\033[%d;%dH", x, y+h);
    this->printf((char *) B_BL);
    for(int i=x+1; i<x+w; i++) this->printf((char *) B_H);
    this->printf((char *) B_BR);
}

void Terminal::banner()
{
       cls(); 
    formatPrintf("_____ Boot screen _____\n",25,1);
    formatPrintf("___ Nucleo Scorda IO Test ___\n",22,2);
 //   formatPrintf(fA.string2char(fA.padstr("-\n",78,'-')),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(fA.string2char(fA.padstr("-\n",78,'-')),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(fA.string2char(fA.padstr("-\n",78,'-')),1,18); //azzo funziona...
    formatPrintf("Serial Input \n",2,18);
}
void Terminal::readypos(){
        formatPrintf("Command: > ",2,23);
    }