Search Notebooks
Terminal

A simple library for controlling the cursor position and colour on a serial terminal emulator.

ANSI, escape, Serial, Terminal, VT100

Page owner: user Simon Ford

Created 31 Dec 2009.
Last updated 12 Jan 2010

Terminal

Page last updated 12 Jan 2010, by   user Simon Ford   tag ANSI, escape, Serial, Terminal, VT100 | 2 replies  

A simple library for controlling the cursor position and colour on a serial terminal emulator.

A terminal program like Teraterm or Hyperterminal often supports escape sequences to control things like cursor location and colour. A common set of escape codes are those first used on the VT100 terminal, which are listed sequences, as found here:

Based on the conversation about the TextStar LCD, this is an experiment for a very simple library to wrap these up in to a class inherited from Serial to make it much easier to use, and more like the drivers we'll have for LCDs.

ANSI/VT100 Terminal Library and Example

Screenshots

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

Terminal term(USBTX, USBRX); // tx, rx

int main() {
    term.background(0x00FF00);
    term.foreground(0xFF0000);
    term.printf("Hello");
    term.locate(3,3);
    term.foreground(0x0000FF);
    term.printf("World!");
}
#include "mbed.h"
#include "Terminal.h"

Terminal term(USBTX, USBRX); // tx, rx

#define ASCII_BLOCK     219
#define ASCII_BORDER_H  205
#define ASCII_BORDER_V  186
#define ASCII_BORDER_TL 201
#define ASCII_BORDER_TR 187
#define ASCII_BORDER_BL 200
#define ASCII_BORDER_BR 188

#define WIDTH 30

void box(int x, int y, int w, int h) {
    // corners
    term.locate(x, y);
    term.putc(ASCII_BORDER_TL);
    term.locate(x + w - 1, y);
    term.putc(ASCII_BORDER_TR);
    term.locate(x, y + h - 1);
    term.putc(ASCII_BORDER_BL);
    term.locate(x + w - 1, y + h - 1);
    term.putc(ASCII_BORDER_BR);
    
    // top
    term.locate(x + 1, y);
    for(int i=0; i


2 comments

04 Jan 2010

Hey..! Maybe someone can start porting dos games now haha. This is very nice..

15 Jan 2010

I believe you have a typo in your locate function:

void Terminal::locate(int column, int row) {
    // Cursor Home    [{ROW};{COLUMN}H
    this->printf("\033[%d;%dH%c", row + 1, column + 1); //<-- extra %c at the end of your format
}
Should be:

void Terminal::locate(int column, int row) {
    // Cursor Home    [{ROW};{COLUMN}H
    this->printf("\033[%d;%dH", row + 1, column + 1);
}

Please log in to post a comment.