An extremely basic VT100 terminal interface. This will generate the proper escape codes to do things such as moving the cursor and clearing the screen. It takes up practically no resources and is really just a nice short cut to avoid figuring out the escape codes yourself.

Dependents:   JOGO_CORRIDA Snake mbedbreakingout

A super simple way to implement crude terminal user interfaces over Stream/Serial using VT100 escape codes.

Simple example:

Serial pc(USBTX, USBRX); 
int main() {
    TermControl ctrl;
    ctrl.SetTerminal(&pc);
    ctrl.Reset();
    ctrl.Clear();
    ctrl.PrintAt(5,10,"FooBar!");
    int i=0;
    i=0;
    while(1) {
        i++;
        ctrl.SetCursor(5,11);
        pc.printf("%i",i);
        
    }
}

This simple program will continuously count, but instead of scrolling and such, it will print the counted numbers in one place. In this case, row 11, column 5.

I don't break out every escape code for the VT100, but I plan on adding more. Maybe even try to get some sort of support for the notion of "widgets" and such. Lightweight is my overall goal though.

TermControl.h

Committer:
earlz
Date:
2012-08-28
Revision:
0:e815b5adc7bc
Child:
1:2263ee1d7353

File content as of revision 0:e815b5adc7bc:

#ifndef TERMCONTROL_H
#define TERMCONTROL_H
#include "mbed.h"
#include <string>
//0x1B is escape
#define FLASH_STRING const char * const
class TermControl
{
    public:
    TermControl()
    {
    
    }
    TermControl(Stream* term)
    {
        Term=term;
    }
    const char Escape='\x1B';
    FLASH_STRING ReportVT100="\x1B[?1;0c";
    void SetTerminal(Stream* term)
    {
        Term=term;
    }
    Stream* GetTerminal()
    {
        return Term;
    }
    //terminal control
    void Reset()
    {
        Print("\x1Bc");
    }
    void Clear()
    {
        Term->printf("\x1B[2J");
    }
    void PrintAt(int x, int y, string s)
    {
        SaveCursor();
        ResetCursor();
        SetCursor(x,y);
        Print(s);
        RestoreCursor();
    }
    //todo void PrintfAt(int x, int y, string f, ...);
    /*string GetLine()
    {
        return new string(Term->gets());
    }*/
    char GetChar()
    {
        return Term->getc();
    }
    void SetCursor(int x, int y)
    {
        Term->printf("\x1B[%i;%iH",y,x);
    }
    void ResetCursor()
    {
        Term->printf("\x1B[H");
    }
    void SaveCursor()
    {
        Term->printf("\x1B[s");
    }
    void RestoreCursor()
    {
        Term->printf("\x1B[u");
    }
    void EnableScrolling(int begin, int end) //begin and end are row numbers
    {
        Term->printf("\x1B[%i;%ir",begin,end);
    } 
    void ScrollDown()
    {
        Term->printf("\x1BD");
    }
    void ScrollUp()
    {
        Term->printf("\x1BM");
    }
    void EraseLine()
    {
        Term->printf("\x1B[2K");
    }
    void Print(string s)
    {
        Term->puts(s.c_str());
    }
    
    
    private:
    Stream *Term;
};
    



#endif