Semtech LoRaWAN-demo configured for 915MHz operation and multi-radio support (SX1276/SX1272)

Dependencies:   mbed LoRaWAN-lib SX1272Lib SX1276Lib

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