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:
Wed Aug 29 01:40:11 2012 +0000
Revision:
2:ef410bacf0e5
Parent:
1:2263ee1d7353
Added doxygen documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
earlz 2:ef410bacf0e5 1 #ifndef TERMCONTROL_H
earlz 2:ef410bacf0e5 2 #define TERMCONTROL_H
earlz 2:ef410bacf0e5 3 #include "mbed.h"
earlz 2:ef410bacf0e5 4 #include <string>
earlz 2:ef410bacf0e5 5 //0x1B is escape
earlz 2:ef410bacf0e5 6 //#define FLASH_STRING const char * const
earlz 2:ef410bacf0e5 7
earlz 2:ef410bacf0e5 8 /** A simple class for sending VT100 terminal escape codes for handling things such as moving the cursor and clearing the screen
earlz 2:ef410bacf0e5 9 */
earlz 2:ef410bacf0e5 10 class TermControl
earlz 2:ef410bacf0e5 11 {
earlz 2:ef410bacf0e5 12 public:
earlz 2:ef410bacf0e5 13 TermControl()
earlz 2:ef410bacf0e5 14 {
earlz 2:ef410bacf0e5 15
earlz 2:ef410bacf0e5 16 }
earlz 2:ef410bacf0e5 17 /** @Param term The stream to send the escape codes to
earlz 2:ef410bacf0e5 18 */
earlz 2:ef410bacf0e5 19 TermControl(Stream* term)
earlz 2:ef410bacf0e5 20 {
earlz 2:ef410bacf0e5 21 Term=term;
earlz 2:ef410bacf0e5 22 }
earlz 2:ef410bacf0e5 23 void SetTerminal(Stream* term)
earlz 2:ef410bacf0e5 24 {
earlz 2:ef410bacf0e5 25 Term=term;
earlz 2:ef410bacf0e5 26 }
earlz 2:ef410bacf0e5 27 Stream* GetTerminal()
earlz 2:ef410bacf0e5 28 {
earlz 2:ef410bacf0e5 29 return Term;
earlz 2:ef410bacf0e5 30 }
earlz 2:ef410bacf0e5 31 //terminal control
earlz 2:ef410bacf0e5 32 /** Resets the terminal to defaults
earlz 2:ef410bacf0e5 33 */
earlz 2:ef410bacf0e5 34 void Reset()
earlz 2:ef410bacf0e5 35 {
earlz 2:ef410bacf0e5 36 Print("\x1Bc");
earlz 2:ef410bacf0e5 37 }
earlz 2:ef410bacf0e5 38 /** Clears the screen
earlz 2:ef410bacf0e5 39 */
earlz 2:ef410bacf0e5 40 void Clear()
earlz 2:ef410bacf0e5 41 {
earlz 2:ef410bacf0e5 42 Term->printf("\x1B[2J");
earlz 2:ef410bacf0e5 43 }
earlz 2:ef410bacf0e5 44 /** Prints the specified string at coordinates x,y
earlz 2:ef410bacf0e5 45 * The cursor position is not changed
earlz 2:ef410bacf0e5 46 *
earlz 2:ef410bacf0e5 47 * @param x The X coordinate, or column
earlz 2:ef410bacf0e5 48 * @param y The Y coordinate, or row
earlz 2:ef410bacf0e5 49 * @param s The string to print
earlz 2:ef410bacf0e5 50 */
earlz 2:ef410bacf0e5 51 void PrintAt(int x, int y, string s)
earlz 2:ef410bacf0e5 52 {
earlz 2:ef410bacf0e5 53 SaveCursor();
earlz 2:ef410bacf0e5 54 ResetCursor();
earlz 2:ef410bacf0e5 55 SetCursor(x,y);
earlz 2:ef410bacf0e5 56 Print(s);
earlz 2:ef410bacf0e5 57 RestoreCursor();
earlz 2:ef410bacf0e5 58 }
earlz 2:ef410bacf0e5 59 //todo void PrintfAt(int x, int y, string f, ...);
earlz 2:ef410bacf0e5 60 /** Get a character from the terminal stream
earlz 2:ef410bacf0e5 61 */
earlz 2:ef410bacf0e5 62 char GetChar()
earlz 2:ef410bacf0e5 63 {
earlz 2:ef410bacf0e5 64 return Term->getc();
earlz 2:ef410bacf0e5 65 }
earlz 2:ef410bacf0e5 66 /** Sets the cursor's position to x,y
earlz 2:ef410bacf0e5 67 */
earlz 2:ef410bacf0e5 68 void SetCursor(int x, int y)
earlz 2:ef410bacf0e5 69 {
earlz 2:ef410bacf0e5 70 Term->printf("\x1B[%i;%iH",y,x);
earlz 2:ef410bacf0e5 71 }
earlz 2:ef410bacf0e5 72 /** Resets the cursor to (0,0), the top left hand corner of the screen
earlz 2:ef410bacf0e5 73 */
earlz 2:ef410bacf0e5 74 void ResetCursor()
earlz 2:ef410bacf0e5 75 {
earlz 2:ef410bacf0e5 76 Term->printf("\x1B[H");
earlz 2:ef410bacf0e5 77 }
earlz 2:ef410bacf0e5 78 /** Saves the cursor on the display-side. It can only remember one cursor position at a time.
earlz 2:ef410bacf0e5 79 * ie, this can't be nested
earlz 2:ef410bacf0e5 80 */
earlz 2:ef410bacf0e5 81 void SaveCursor()
earlz 2:ef410bacf0e5 82 {
earlz 2:ef410bacf0e5 83 Term->printf("\x1B[s");
earlz 2:ef410bacf0e5 84 }
earlz 2:ef410bacf0e5 85 /** Restores the cursor on the display-side. It can only remember and store one cursor position at a time.
earlz 2:ef410bacf0e5 86 * ie, this can't be nested
earlz 2:ef410bacf0e5 87 */
earlz 2:ef410bacf0e5 88 void RestoreCursor()
earlz 2:ef410bacf0e5 89 {
earlz 2:ef410bacf0e5 90 Term->printf("\x1B[u");
earlz 2:ef410bacf0e5 91 }
earlz 2:ef410bacf0e5 92 /** Enables a scrolling "window" at the specified beginning and ending rows
earlz 2:ef410bacf0e5 93 * @param begin The beginning row
earlz 2:ef410bacf0e5 94 * @param end The ending row
earlz 2:ef410bacf0e5 95 */
earlz 2:ef410bacf0e5 96 void EnableScrolling(int begin, int end) //begin and end are row numbers
earlz 2:ef410bacf0e5 97 {
earlz 2:ef410bacf0e5 98 Term->printf("\x1B[%i;%ir",begin,end);
earlz 2:ef410bacf0e5 99 }
earlz 2:ef410bacf0e5 100 /** Scrolls down the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called
earlz 2:ef410bacf0e5 101 */
earlz 2:ef410bacf0e5 102 void ScrollDown()
earlz 2:ef410bacf0e5 103 {
earlz 2:ef410bacf0e5 104 Term->printf("\x1BD");
earlz 2:ef410bacf0e5 105 }
earlz 2:ef410bacf0e5 106 /** Scrolls up the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called
earlz 2:ef410bacf0e5 107 */
earlz 2:ef410bacf0e5 108 void ScrollUp()
earlz 2:ef410bacf0e5 109 {
earlz 2:ef410bacf0e5 110 Term->printf("\x1BM");
earlz 2:ef410bacf0e5 111 }
earlz 2:ef410bacf0e5 112 /** Erases the current line the cursor is on.
earlz 2:ef410bacf0e5 113 */
earlz 2:ef410bacf0e5 114 void EraseLine()
earlz 2:ef410bacf0e5 115 {
earlz 2:ef410bacf0e5 116 Term->printf("\x1B[2K");
earlz 2:ef410bacf0e5 117 }
earlz 2:ef410bacf0e5 118 /** Prints the specified string to the string at the current cursor position
earlz 2:ef410bacf0e5 119 */
earlz 2:ef410bacf0e5 120 void Print(string s)
earlz 2:ef410bacf0e5 121 {
earlz 2:ef410bacf0e5 122 Term->puts(s.c_str());
earlz 2:ef410bacf0e5 123 }
earlz 2:ef410bacf0e5 124
earlz 2:ef410bacf0e5 125
earlz 2:ef410bacf0e5 126 private:
earlz 2:ef410bacf0e5 127 Stream *Term;
earlz 2:ef410bacf0e5 128 };
earlz 2:ef410bacf0e5 129
earlz 2:ef410bacf0e5 130
earlz 2:ef410bacf0e5 131
earlz 2:ef410bacf0e5 132
earlz 0:e815b5adc7bc 133 #endif