April_12th_San_Jose_LoRa_Bootcamp

Dependencies:   lorawan1v1

Fork of LoRaWAN-grove-cayenne by wayne roberts

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