Combines locate fix, show/hide cursor + Justin Jordans additions

Fork of Terminal by Justin Jordan

Terminal.cpp

Committer:
j3
Date:
2016-04-12
Revision:
4:4510b10fb5d9
Parent:
3:7c269f52ad77
Child:
6:419eea2fe960

File content as of revision 4:4510b10fb5d9:

/* 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::home(){
    this->printf("\033[H");
}

void Terminal::locate(int column, int row) {
    // Cursor Home    <ESC>[{ROW};{COLUMN}H
    this->printf("\033[%d;%dH%c", 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);
}

int32_t Terminal::get_int32(const char *msg, const int32_t min_val, const int32_t max_val)
{
    int32_t user_input;
    char str[12];
    uint8_t idx;
    
    do
    {
        this->printf(msg);
        
        //get user input
        idx = 0;
        do
        {
            if(this->readable())
            {
                str[idx++] = this->getc();
            }
        }
        while((str[idx-1] != 0x0A)  && (idx < 12));
        
        //Remove trailing newline and CR, if there.
        if((strlen(str) > 0 ) && (str[strlen(str) - 1] == 0x0A) && (str[strlen(str) - 2] == 0x0D))
        {
            str[strlen(str) - 1] = '\0';
            str[strlen(str) - 1] = '\0';
        }
        
        user_input = strtol(str, NULL, 0);
        
        if((user_input < min_val) || (user_input > max_val))
        {
            this->printf("\nYou entered = %d\n", user_input);
            this->printf("\nOut of range\n");
        }
    }
    while((user_input < min_val) || (user_input > max_val));
    
    return(user_input);
}
    
char Terminal::get_char(const char *msg, const char min_val, const char max_val)
{
    char c[3];
    uint8_t idx;
    
    do
    {
        this->printf(msg);
        
        //get user input
        idx = 0;
        do
        {
            if(this->readable())
            {
                c[idx++] = this->getc();
            }
        }
        while((c[idx-1] != 0x0A)  && (idx < 3));
        
        //Remove trailing newline and CR, if there.
        if((strlen(c) > 0 ) && (c[strlen(c) - 1] == 0x0A) && (c[strlen(c) - 2] == 0x0D))
        {
            c[strlen(c) - 1] = '\0';
            c[strlen(c) - 1] = '\0';
        }
        
        if((c[0] < min_val) || (c[0] > max_val))
        {
            this->printf("\nYou entered = %c\n", c[0]);
            this->printf("\nOut of range\n");
        }
    }
    while((c[0] < min_val) || (c[0] > max_val));
    
    return c[0];
}