first draft

Dependencies:   mbed LoRaWAN-lib SX1272Liby

Fork of LoRaWAN-demo-72 by Semtech

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         // initializes terminal to "power-on" settings
00059         // ESC c
00060         this->printf( "\x1B\x63" );
00061     }
00062     
00063     void ClearScreen( uint8_t param )
00064     {
00065         // ESC [ Ps J
00066         // 0    Clear screen from cursor down
00067         // 1    Clear screen from cursor up
00068         // 2    Clear entire screen 
00069         this->printf( "\x1B[%dJ", param );
00070     }
00071 
00072     void ClearLine( uint8_t param )
00073     {
00074         // ESC [ Ps K
00075         // 0    Erase from the active position to the end of the line, inclusive (default)
00076         // 1    Erase from the start of the screen to the active position, inclusive
00077         // 2    Erase all of the line, inclusive
00078         this->printf( "\x1B[%dK", param );
00079     }
00080 
00081     void SetAttribute( uint8_t attr )
00082     {
00083         // ESC [ Ps;...;Ps m
00084         this->printf( "\x1B[%dm", attr );
00085     }
00086 
00087     void SetAttribute( uint8_t attr, uint8_t fgcolor, uint8_t bgcolor )
00088     {
00089         // ESC [ Ps;...;Ps m
00090         this->printf( "\x1B[%d;%d;%dm", attr, fgcolor + 30, bgcolor + 40 );
00091     }
00092 
00093     void SetCursorMode( uint8_t visible )
00094     {
00095         if( visible == true )
00096         {
00097             // ESC [ ? 25 h
00098             this->printf( "\x1B[?25h" );
00099         }
00100         else
00101         {
00102             // ESC [ ? 25 l
00103             this->printf( "\x1B[?25l" );
00104         }
00105     }
00106 
00107     void SetCursorPos( uint8_t line, uint8_t col )
00108     {
00109         // ESC [ Pl ; Pc H
00110         this->printf( "\x1B[%d;%dH", line, col );
00111     }
00112 
00113     void PutStringAt( uint8_t line, uint8_t col, const char *s )
00114     {
00115         this->SetCursorPos( line, col );
00116         this->printf( "%s", s );
00117     }
00118 
00119     void PutCharAt( uint8_t line, uint8_t col, uint8_t c )
00120     {
00121         this->SetCursorPos( line, col );
00122         this->printf( "%c", c );
00123     }
00124 
00125     void PutHexAt( uint8_t line, uint8_t col, uint16_t n )
00126     {
00127         this->SetCursorPos( line, col );
00128         this->printf( "%X", n );
00129     }
00130 
00131     void PutBoxDrawingChar( uint8_t c )
00132     {
00133         this->printf( "\x1B(0%c\x1b(B", c );
00134     }
00135     
00136     bool Readable( void )
00137     {
00138         return this->readable( );
00139     }
00140     
00141     uint8_t GetChar( void )
00142     {
00143         return this->getc( );
00144     }
00145 
00146     /*
00147      * RawSerial class implmentation copy.
00148      */
00149     /** Read a char from the serial port
00150      *
00151      * @returns The char read from the serial port
00152      */
00153     int getc( )
00154     {
00155         return _base_getc();
00156     }
00157 
00158     /** Write a char to the serial port
00159      *
00160      * @param c The char to write
00161      *
00162      * @returns The written char or -1 if an error occured
00163      */
00164     int putc( int c )
00165     {
00166         while( this->writeable( ) != 1 );
00167         return _base_putc( c );
00168     }
00169 
00170     /** Write a string to the serial port
00171      *
00172      * @param str The string to write
00173      *
00174      * @returns 0 if the write succeeds, EOF for error
00175      */
00176     int puts( const char *str )
00177     {
00178         while( *str )
00179             putc( *str++ );
00180         return 0;
00181     }
00182 
00183     // Experimental support for printf in RawSerial. No Stream inheritance
00184     // means we can't call printf() directly, so we use sprintf() instead.
00185     // We only call malloc() for the sprintf() buffer if the buffer
00186     // length is above a certain threshold, otherwise we use just the stack.
00187     int printf( const char *format, ... )
00188     {
00189         std::va_list arg;
00190         va_start( arg, format );
00191         int len = vsnprintf( NULL, 0, format, arg );
00192         if( len < STRING_STACK_LIMIT )
00193         {
00194             char temp[STRING_STACK_LIMIT];
00195             vsprintf( temp, format, arg );
00196             puts( temp );
00197         }
00198         else
00199         {
00200             char *temp = new char[len + 1];
00201             vsprintf( temp, format, arg );
00202             puts( temp );
00203             delete[] temp;
00204         }
00205         va_end( arg );
00206         return len;
00207     }
00208 
00209 private:
00210     
00211 };
00212 
00213 #endif // __VT100_H__