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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TermControl.h Source File

TermControl.h

00001 #ifndef TERMCONTROL_H
00002 #define TERMCONTROL_H
00003 #include "mbed.h"
00004 #include <string>
00005 //0x1B is escape
00006 //#define FLASH_STRING const char * const
00007 
00008 /** A simple class for sending VT100 terminal escape codes for handling things such as moving the cursor and clearing the screen
00009   */
00010 class TermControl
00011 {
00012     public:
00013     TermControl()
00014     {
00015     
00016     }
00017     /** @Param term The stream to send the escape codes to
00018      */
00019     TermControl(Stream* term)
00020     {
00021         Term=term;
00022     }
00023     void SetTerminal(Stream* term)
00024     {
00025         Term=term;
00026     }
00027     Stream* GetTerminal()
00028     {
00029         return Term;
00030     }
00031     //terminal control
00032     /** Resets the terminal to defaults
00033      */
00034     void Reset()
00035     {
00036         Print("\x1Bc");
00037     }
00038     /** Clears the screen
00039      */
00040     void Clear()
00041     {
00042         Term->printf("\x1B[2J");
00043     }
00044     /** Prints the specified string at coordinates x,y 
00045      *  The cursor position is not changed
00046      * 
00047      *  @param x The X coordinate, or column
00048      *  @param y The Y coordinate, or row
00049      *  @param s The string to print
00050      */
00051     void PrintAt(int x, int y, string s)
00052     {
00053         SaveCursor();
00054         ResetCursor();
00055         SetCursor(x,y);
00056         Print(s);
00057         RestoreCursor();
00058     }
00059     //todo void PrintfAt(int x, int y, string f, ...);
00060     /** Get a character from the terminal stream
00061      */
00062     char GetChar()
00063     {
00064         return Term->getc();
00065     }
00066     /** Sets the cursor's position to x,y
00067      */
00068     void SetCursor(int x, int y)
00069     {
00070         Term->printf("\x1B[%i;%iH",y,x);
00071     }
00072     /** Resets the cursor to (0,0), the top left hand corner of the screen
00073      */
00074     void ResetCursor()
00075     {
00076         Term->printf("\x1B[H");
00077     }
00078     /** Saves the cursor on the display-side. It can only remember one cursor position at a time.
00079      * ie, this can't be nested
00080      */
00081     void SaveCursor()
00082     {
00083         Term->printf("\x1B[s");
00084     }
00085     /** Restores the cursor on the display-side. It can only remember and store one cursor position at a time.
00086      * ie, this can't be nested
00087      */
00088     void RestoreCursor()
00089     {
00090         Term->printf("\x1B[u");
00091     }
00092     /** Enables a scrolling "window" at the specified beginning and ending rows
00093      * @param begin The beginning row
00094      * @param end The ending row
00095      */
00096     void EnableScrolling(int begin, int end) //begin and end are row numbers
00097     {
00098         Term->printf("\x1B[%i;%ir",begin,end);
00099     } 
00100     /** Scrolls down the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called
00101      */
00102     void ScrollDown()
00103     {
00104         Term->printf("\x1BD");
00105     }
00106     /** Scrolls up the scrolling "window" by 1 line. Note, EnableScrolling is required before this can be called
00107      */
00108     void ScrollUp()
00109     {
00110         Term->printf("\x1BM");
00111     }
00112     /** Erases the current line the cursor is on.
00113      */
00114     void EraseLine()
00115     {
00116         Term->printf("\x1B[2K");
00117     }
00118     /** Prints the specified string to the string at the current cursor position
00119      */
00120     void Print(string s)
00121     {
00122         Term->puts(s.c_str());
00123     }
00124     
00125     
00126     private:
00127     Stream *Term;
00128 };
00129     
00130 
00131 
00132 
00133 #endif