Improved version of Simon Ford's TextDisplay library, with addressing and timing fixes. Supports up to 20x4 text displays.

Terminal.cpp

Committer:
bikeNomad
Date:
2010-02-14
Revision:
0:2c5bba968d7c

File content as of revision 0:2c5bba968d7c:

/* mbed Terminal TextDisplay Library
 * Copyright (c) 2007-2009 sford
 * Released under the MIT License: http://mbed.org/license/mit
 */

#include "Terminal.h"

#include "mbed.h"

Terminal::Terminal(PinName tx, PinName rx) : _serial(tx, rx) {
    cls();
}

void Terminal::character(uint16_t column, uint16_t row, int c) {
    // Cursor Home    <ESC>[{ROW};{COLUMN}H 
    _serial.printf("\033[%u;%uH%c", row + 1, column + 1, c);
}

uint16_t Terminal::columns() {
    return 80;
}

uint16_t Terminal::rows() { 
    return 35; 
}

void Terminal::cls() {
    _serial.printf("\033[2J");
}

void Terminal::foreground(uint32_t colour) {

    /* Set Attribute Mode    <ESC>[{n}m
     * - Sets display attribute settings. The following lists standard attributes:
     * 
     * Foreground Colours
     * 30    Black
     * 31    Red
     * 32    Green
     * 33    Yellow
     * 34    Blue
     * 35    Magenta
     * 36    Cyan
     * 37    White
     */
    uint32_t r = (colour >> 23) & 1;
    uint32_t g = (colour >> 15) & 1;
    uint32_t b = (colour >> 7) & 1;
    uint32_t bgr = (b << 2) | (g << 1) | (r << 0);
    uint32_t c = 30 + bgr;
    _serial.printf("\033[%um", c);
}

void Terminal::background(uint32_t colour) {

    /* Background Colours
     * 40    Black
     * 41    Red
     * 42    Green
     * 43    Yellow
     * 44    Blue
     * 45    Magenta
     * 46    Cyan
     * 47    White
     */
    uint32_t r = (colour >> 23) & 1;
    uint32_t g = (colour >> 15) & 1;
    uint32_t b = (colour >> 7) & 1;
    uint32_t bgr = (b << 2) | (g << 1) | (r << 0);
    uint32_t c = 40 + bgr;
    _serial.printf("\033[%um", c);
}