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.
Revision 2:ef410bacf0e5, committed 2012-08-29
- Comitter:
- earlz
- Date:
- Wed Aug 29 01:40:11 2012 +0000
- Parent:
- 1:2263ee1d7353
- Commit message:
- Added doxygen documentation
Changed in this revision
TermControl.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 2263ee1d7353 -r ef410bacf0e5 TermControl.h --- a/TermControl.h Tue Aug 28 04:30:21 2012 +0000 +++ b/TermControl.h Wed Aug 29 01:40:11 2012 +0000 @@ -1,95 +1,133 @@ -#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, ...); - 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; -}; - - - - +#ifndef TERMCONTROL_H +#define TERMCONTROL_H +#include "mbed.h" +#include <string> +//0x1B is escape +//#define FLASH_STRING const char * const + +/** A simple class for sending VT100 terminal escape codes for handling things such as moving the cursor and clearing the screen + */ +class TermControl +{ + public: + TermControl() + { + + } + /** @Param term The stream to send the escape codes to + */ + TermControl(Stream* term) + { + Term=term; + } + void SetTerminal(Stream* term) + { + Term=term; + } + Stream* GetTerminal() + { + return Term; + } + //terminal control + /** Resets the terminal to defaults + */ + void Reset() + { + Print("\x1Bc"); + } + /** Clears the screen + */ + void Clear() + { + Term->printf("\x1B[2J"); + } + /** Prints the specified string at coordinates x,y + * The cursor position is not changed + * + * @param x The X coordinate, or column + * @param y The Y coordinate, or row + * @param s The string to print + */ + 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, ...); + /** Get a character from the terminal stream + */ + char GetChar() + { + return Term->getc(); + } + /** Sets the cursor's position to x,y + */ + void SetCursor(int x, int y) + { + Term->printf("\x1B[%i;%iH",y,x); + } + /** Resets the cursor to (0,0), the top left hand corner of the screen + */ + void ResetCursor() + { + Term->printf("\x1B[H"); + } + /** Saves the cursor on the display-side. It can only remember one cursor position at a time. + * ie, this can't be nested + */ + void SaveCursor() + { + Term->printf("\x1B[s"); + } + /** Restores the cursor on the display-side. It can only remember and store one cursor position at a time. + * ie, this can't be nested + */ + void RestoreCursor() + { + Term->printf("\x1B[u"); + } + /** Enables a scrolling "window" at the specified beginning and ending rows + * @param begin The beginning row + * @param end The ending row + */ + void EnableScrolling(int begin, int end) //begin and end are row numbers + { + Term->printf("\x1B[%i;%ir",begin,end); + } + /** Scrolls down the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called + */ + void ScrollDown() + { + Term->printf("\x1BD"); + } + /** Scrolls up the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called + */ + void ScrollUp() + { + Term->printf("\x1BM"); + } + /** Erases the current line the cursor is on. + */ + void EraseLine() + { + Term->printf("\x1B[2K"); + } + /** Prints the specified string to the string at the current cursor position + */ + void Print(string s) + { + Term->puts(s.c_str()); + } + + + private: + Stream *Term; +}; + + + + #endif \ No newline at end of file