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.

Committer:
earlz
Date:
Tue Aug 28 04:30:21 2012 +0000
Revision:
1:2263ee1d7353
Parent:
0:e815b5adc7bc
Child:
2:ef410bacf0e5
Removed commented out code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
earlz 1:2263ee1d7353 1 #ifndef TERMCONTROL_H
earlz 1:2263ee1d7353 2 #define TERMCONTROL_H
earlz 1:2263ee1d7353 3 #include "mbed.h"
earlz 1:2263ee1d7353 4 #include <string>
earlz 1:2263ee1d7353 5 //0x1B is escape
earlz 1:2263ee1d7353 6 #define FLASH_STRING const char * const
earlz 1:2263ee1d7353 7 class TermControl
earlz 1:2263ee1d7353 8 {
earlz 1:2263ee1d7353 9 public:
earlz 1:2263ee1d7353 10 TermControl()
earlz 1:2263ee1d7353 11 {
earlz 1:2263ee1d7353 12
earlz 1:2263ee1d7353 13 }
earlz 1:2263ee1d7353 14 TermControl(Stream* term)
earlz 1:2263ee1d7353 15 {
earlz 1:2263ee1d7353 16 Term=term;
earlz 1:2263ee1d7353 17 }
earlz 1:2263ee1d7353 18 const char Escape='\x1B';
earlz 1:2263ee1d7353 19 FLASH_STRING ReportVT100="\x1B[?1;0c";
earlz 1:2263ee1d7353 20 void SetTerminal(Stream* term)
earlz 1:2263ee1d7353 21 {
earlz 1:2263ee1d7353 22 Term=term;
earlz 1:2263ee1d7353 23 }
earlz 1:2263ee1d7353 24 Stream* GetTerminal()
earlz 1:2263ee1d7353 25 {
earlz 1:2263ee1d7353 26 return Term;
earlz 1:2263ee1d7353 27 }
earlz 1:2263ee1d7353 28 //terminal control
earlz 1:2263ee1d7353 29 void Reset()
earlz 1:2263ee1d7353 30 {
earlz 1:2263ee1d7353 31 Print("\x1Bc");
earlz 1:2263ee1d7353 32 }
earlz 1:2263ee1d7353 33 void Clear()
earlz 1:2263ee1d7353 34 {
earlz 1:2263ee1d7353 35 Term->printf("\x1B[2J");
earlz 1:2263ee1d7353 36 }
earlz 1:2263ee1d7353 37 void PrintAt(int x, int y, string s)
earlz 1:2263ee1d7353 38 {
earlz 1:2263ee1d7353 39 SaveCursor();
earlz 1:2263ee1d7353 40 ResetCursor();
earlz 1:2263ee1d7353 41 SetCursor(x,y);
earlz 1:2263ee1d7353 42 Print(s);
earlz 1:2263ee1d7353 43 RestoreCursor();
earlz 1:2263ee1d7353 44 }
earlz 1:2263ee1d7353 45 //todo void PrintfAt(int x, int y, string f, ...);
earlz 1:2263ee1d7353 46 char GetChar()
earlz 1:2263ee1d7353 47 {
earlz 1:2263ee1d7353 48 return Term->getc();
earlz 1:2263ee1d7353 49 }
earlz 1:2263ee1d7353 50 void SetCursor(int x, int y)
earlz 1:2263ee1d7353 51 {
earlz 1:2263ee1d7353 52 Term->printf("\x1B[%i;%iH",y,x);
earlz 1:2263ee1d7353 53 }
earlz 1:2263ee1d7353 54 void ResetCursor()
earlz 1:2263ee1d7353 55 {
earlz 1:2263ee1d7353 56 Term->printf("\x1B[H");
earlz 1:2263ee1d7353 57 }
earlz 1:2263ee1d7353 58 void SaveCursor()
earlz 1:2263ee1d7353 59 {
earlz 1:2263ee1d7353 60 Term->printf("\x1B[s");
earlz 1:2263ee1d7353 61 }
earlz 1:2263ee1d7353 62 void RestoreCursor()
earlz 1:2263ee1d7353 63 {
earlz 1:2263ee1d7353 64 Term->printf("\x1B[u");
earlz 1:2263ee1d7353 65 }
earlz 1:2263ee1d7353 66 void EnableScrolling(int begin, int end) //begin and end are row numbers
earlz 1:2263ee1d7353 67 {
earlz 1:2263ee1d7353 68 Term->printf("\x1B[%i;%ir",begin,end);
earlz 1:2263ee1d7353 69 }
earlz 1:2263ee1d7353 70 void ScrollDown()
earlz 1:2263ee1d7353 71 {
earlz 1:2263ee1d7353 72 Term->printf("\x1BD");
earlz 1:2263ee1d7353 73 }
earlz 1:2263ee1d7353 74 void ScrollUp()
earlz 1:2263ee1d7353 75 {
earlz 1:2263ee1d7353 76 Term->printf("\x1BM");
earlz 1:2263ee1d7353 77 }
earlz 1:2263ee1d7353 78 void EraseLine()
earlz 1:2263ee1d7353 79 {
earlz 1:2263ee1d7353 80 Term->printf("\x1B[2K");
earlz 1:2263ee1d7353 81 }
earlz 1:2263ee1d7353 82 void Print(string s)
earlz 1:2263ee1d7353 83 {
earlz 1:2263ee1d7353 84 Term->puts(s.c_str());
earlz 1:2263ee1d7353 85 }
earlz 1:2263ee1d7353 86
earlz 1:2263ee1d7353 87
earlz 1:2263ee1d7353 88 private:
earlz 1:2263ee1d7353 89 Stream *Term;
earlz 1:2263ee1d7353 90 };
earlz 1:2263ee1d7353 91
earlz 1:2263ee1d7353 92
earlz 1:2263ee1d7353 93
earlz 1:2263ee1d7353 94
earlz 0:e815b5adc7bc 95 #endif