Terminal

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


3 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);
}

13 Aug 2016 . Edited: 13 Aug 2016
It's working but it's not. When i put the code in infinite while loop it doesn't execute for at least one time. But it is working absolutely fine without that checkout my code below #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.printf("World!"); //while(1) { term.foreground(0x00FF00); term.locate(0,0); term.printf("Panel 1"); term.locate(30,0); term.printf("Panel 2"); term.locate(60,0); term.printf("Panel 3"); term.locate(0,5); term.printf("Panel 4"); term.locate(30,5); term.printf("Panel 5"); term.locate(60,5); term.printf("Panel 6"); term.locate(0,10); term.printf("Panel 7"); term.locate(30,10); term.printf("Panel 8"); term.locate(60,10); term.printf("Panel 9"); term.cls(); } } I have commented the while loop here if you remove comment on the while loop it doesn't work. Provide me some solution if you have it thank you.

You need to log in to post a comment