inital draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72_tjm by Timothy Mulrooney

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vt100.h Source File

vt100.h

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: VT100 terminal support class
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainer: Miguel Luis and Gregory Cristian
00014 */
00015 #ifndef __VT100_H__
00016 #define __VT100_H__
00017 
00018 #ifndef STRING_STACK_LIMIT
00019 #define STRING_STACK_LIMIT    120
00020 #endif
00021 
00022 /**
00023  * Implements VT100 terminal commands support.
00024  * Implments also the same behaviour has RawSerial class. The only difference
00025  * is located in putc fucntion where writeable check is made befor sending the character.
00026  */
00027 class VT100 : public SerialBase
00028 {
00029 public:
00030     enum TextAttributes
00031     {
00032         ATTR_OFF      = 0,
00033         BOLD          = 1,
00034         USCORE        = 4,
00035         BLINK         = 5,
00036         REVERSE       = 7,
00037         BOLD_OFF      = 21,
00038         USCORE_OFF    = 24,
00039         BLINK_OFF     = 25,
00040         REVERSE_OFF   = 27,
00041     };
00042 
00043     enum Colors
00044     {
00045         BLACK   = 0,
00046         RED     = 1,
00047         GREEN   = 2,
00048         BROWN   = 3,
00049         BLUE    = 4,
00050         MAGENTA = 5,
00051         CYAN    = 6,
00052         WHITE   = 7,
00053     };
00054 
00055     VT100( PinName tx, PinName rx ): SerialBase( tx, rx )
00056     {
00057 //        this->baud( 115200 );
00058        this->baud( (115200 * 2)/3 );        /* 8 MHZ/12 MHZ) */
00059         // initializes terminal to "power-on" settings
00060         // ESC c
00061         this->printf( "\x1B\x63" );
00062     }
00063     
00064     void ClearScreen( uint8_t param )
00065     {
00066         // ESC [ Ps J
00067         // 0    Clear screen from cursor down
00068         // 1    Clear screen from cursor up
00069         // 2    Clear entire screen 
00070         this->printf( "\x1B[%dJ", param );
00071     }
00072 
00073     void ClearLine( uint8_t param )
00074     {
00075         // ESC [ Ps K
00076         // 0    Erase from the active position to the end of the line, inclusive (default)
00077         // 1    Erase from the start of the screen to the active position, inclusive
00078         // 2    Erase all of the line, inclusive
00079         this->printf( "\x1B[%dK", param );
00080     }
00081 
00082     void SetAttribute( uint8_t attr )
00083     {
00084         // ESC [ Ps;...;Ps m
00085         this->printf( "\x1B[%dm", attr );
00086     }
00087 
00088     void SetAttribute( uint8_t attr, uint8_t fgcolor, uint8_t bgcolor )
00089     {
00090         // ESC [ Ps;...;Ps m
00091         this->printf( "\x1B[%d;%d;%dm", attr, fgcolor + 30, bgcolor + 40 );
00092     }
00093 
00094     void SetCursorMode( uint8_t visible )
00095     {
00096         if( visible == true )
00097         {
00098             // ESC [ ? 25 h
00099             this->printf( "\x1B[?25h" );
00100         }
00101         else
00102         {
00103             // ESC [ ? 25 l
00104             this->printf( "\x1B[?25l" );
00105         }
00106     }
00107 
00108     void SetCursorPos( uint8_t line, uint8_t col )
00109     {
00110         // ESC [ Pl ; Pc H
00111         this->printf( "\x1B[%d;%dH", line, col );
00112     }
00113 
00114     void PutStringAt( uint8_t line, uint8_t col, const char *s )
00115     {
00116         this->SetCursorPos( line, col );
00117         this->printf( "%s", s );
00118     }
00119 
00120     void PutCharAt( uint8_t line, uint8_t col, uint8_t c )
00121     {
00122         this->SetCursorPos( line, col );
00123         this->printf( "%c", c );
00124     }
00125 
00126     void PutHexAt( uint8_t line, uint8_t col, uint16_t n )
00127     {
00128         this->SetCursorPos( line, col );
00129         this->printf( "%X", n );
00130     }
00131 
00132     void PutBoxDrawingChar( uint8_t c )
00133     {
00134         this->printf( "\x1B(0%c\x1b(B", c );
00135     }
00136     
00137     bool Readable( void )
00138     {
00139         return this->readable( );
00140     }
00141     
00142     uint8_t GetChar( void )
00143     {
00144         return this->getc( );
00145     }
00146 
00147     /*
00148      * RawSerial class implmentation copy.
00149      */
00150     /** Read a char from the serial port
00151      *
00152      * @returns The char read from the serial port
00153      */
00154     int getc( )
00155     {
00156         return _base_getc();
00157     }
00158 
00159     /** Write a char to the serial port
00160      *
00161      * @param c The char to write
00162      *
00163      * @returns The written char or -1 if an error occured
00164      */
00165     int putc( int c )
00166     {
00167         while( this->writeable( ) != 1 );
00168         return _base_putc( c );
00169     }
00170 
00171     /** Write a string to the serial port
00172      *
00173      * @param str The string to write
00174      *
00175      * @returns 0 if the write succeeds, EOF for error
00176      */
00177     int puts( const char *str )
00178     {
00179         while( *str )
00180             putc( *str++ );
00181         return 0;
00182     }
00183 
00184     // Experimental support for printf in RawSerial. No Stream inheritance
00185     // means we can't call printf() directly, so we use sprintf() instead.
00186     // We only call malloc() for the sprintf() buffer if the buffer
00187     // length is above a certain threshold, otherwise we use just the stack.
00188     int printf( const char *format, ... )
00189     {
00190         std::va_list arg;
00191         va_start( arg, format );
00192         int len = vsnprintf( NULL, 0, format, arg );
00193         if( len < STRING_STACK_LIMIT )
00194         {
00195             char temp[STRING_STACK_LIMIT];
00196             vsprintf( temp, format, arg );
00197             puts( temp );
00198         }
00199         else
00200         {
00201             char *temp = new char[len + 1];
00202             vsprintf( temp, format, arg );
00203             puts( temp );
00204             delete[] temp;
00205         }
00206         va_end( arg );
00207         return len;
00208     }
00209 
00210 private:
00211     
00212 };
00213 
00214 #endif // __VT100_H__