for India band 30dBm LoRa Module, DSPLoRa based on code from Semtech and as modified by Sachin Pukale/ TCL/
Dependencies: mbed Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1276Lib
Fork of DSP_LoRaWAN by
app/vt100.h@0:cb80564f40e1, 2016-03-17 (annotated)
- Committer:
- mluis
- Date:
- Thu Mar 17 09:32:46 2016 +0000
- Revision:
- 0:cb80564f40e1
Project creation based on LoRaWAN-demo-76
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:cb80564f40e1 | 1 | /* |
mluis | 0:cb80564f40e1 | 2 | / _____) _ | | |
mluis | 0:cb80564f40e1 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mluis | 0:cb80564f40e1 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mluis | 0:cb80564f40e1 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mluis | 0:cb80564f40e1 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mluis | 0:cb80564f40e1 | 7 | (C)2015 Semtech |
mluis | 0:cb80564f40e1 | 8 | |
mluis | 0:cb80564f40e1 | 9 | Description: VT100 terminal support class |
mluis | 0:cb80564f40e1 | 10 | |
mluis | 0:cb80564f40e1 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mluis | 0:cb80564f40e1 | 12 | |
mluis | 0:cb80564f40e1 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
mluis | 0:cb80564f40e1 | 14 | */ |
mluis | 0:cb80564f40e1 | 15 | #ifndef __VT100_H__ |
mluis | 0:cb80564f40e1 | 16 | #define __VT100_H__ |
mluis | 0:cb80564f40e1 | 17 | |
mluis | 0:cb80564f40e1 | 18 | #ifndef STRING_STACK_LIMIT |
mluis | 0:cb80564f40e1 | 19 | #define STRING_STACK_LIMIT 120 |
mluis | 0:cb80564f40e1 | 20 | #endif |
mluis | 0:cb80564f40e1 | 21 | |
mluis | 0:cb80564f40e1 | 22 | /** |
mluis | 0:cb80564f40e1 | 23 | * Implements VT100 terminal commands support. |
mluis | 0:cb80564f40e1 | 24 | * Implments also the same behaviour has RawSerial class. The only difference |
mluis | 0:cb80564f40e1 | 25 | * is located in putc fucntion where writeable check is made befor sending the character. |
mluis | 0:cb80564f40e1 | 26 | */ |
mluis | 0:cb80564f40e1 | 27 | class VT100 : public SerialBase |
mluis | 0:cb80564f40e1 | 28 | { |
mluis | 0:cb80564f40e1 | 29 | public: |
mluis | 0:cb80564f40e1 | 30 | enum TextAttributes |
mluis | 0:cb80564f40e1 | 31 | { |
mluis | 0:cb80564f40e1 | 32 | ATTR_OFF = 0, |
mluis | 0:cb80564f40e1 | 33 | BOLD = 1, |
mluis | 0:cb80564f40e1 | 34 | USCORE = 4, |
mluis | 0:cb80564f40e1 | 35 | BLINK = 5, |
mluis | 0:cb80564f40e1 | 36 | REVERSE = 7, |
mluis | 0:cb80564f40e1 | 37 | BOLD_OFF = 21, |
mluis | 0:cb80564f40e1 | 38 | USCORE_OFF = 24, |
mluis | 0:cb80564f40e1 | 39 | BLINK_OFF = 25, |
mluis | 0:cb80564f40e1 | 40 | REVERSE_OFF = 27, |
mluis | 0:cb80564f40e1 | 41 | }; |
mluis | 0:cb80564f40e1 | 42 | |
mluis | 0:cb80564f40e1 | 43 | enum Colors |
mluis | 0:cb80564f40e1 | 44 | { |
mluis | 0:cb80564f40e1 | 45 | BLACK = 0, |
mluis | 0:cb80564f40e1 | 46 | RED = 1, |
mluis | 0:cb80564f40e1 | 47 | GREEN = 2, |
mluis | 0:cb80564f40e1 | 48 | BROWN = 3, |
mluis | 0:cb80564f40e1 | 49 | BLUE = 4, |
mluis | 0:cb80564f40e1 | 50 | MAGENTA = 5, |
mluis | 0:cb80564f40e1 | 51 | CYAN = 6, |
mluis | 0:cb80564f40e1 | 52 | WHITE = 7, |
mluis | 0:cb80564f40e1 | 53 | }; |
mluis | 0:cb80564f40e1 | 54 | |
mluis | 0:cb80564f40e1 | 55 | VT100( PinName tx, PinName rx ): SerialBase( tx, rx ) |
mluis | 0:cb80564f40e1 | 56 | { |
mluis | 0:cb80564f40e1 | 57 | this->baud( 115200 ); |
mluis | 0:cb80564f40e1 | 58 | // initializes terminal to "power-on" settings |
mluis | 0:cb80564f40e1 | 59 | // ESC c |
mluis | 0:cb80564f40e1 | 60 | this->printf( "\x1B\x63" ); |
mluis | 0:cb80564f40e1 | 61 | } |
mluis | 0:cb80564f40e1 | 62 | |
mluis | 0:cb80564f40e1 | 63 | void ClearScreen( uint8_t param ) |
mluis | 0:cb80564f40e1 | 64 | { |
mluis | 0:cb80564f40e1 | 65 | // ESC [ Ps J |
mluis | 0:cb80564f40e1 | 66 | // 0 Clear screen from cursor down |
mluis | 0:cb80564f40e1 | 67 | // 1 Clear screen from cursor up |
mluis | 0:cb80564f40e1 | 68 | // 2 Clear entire screen |
mluis | 0:cb80564f40e1 | 69 | this->printf( "\x1B[%dJ", param ); |
mluis | 0:cb80564f40e1 | 70 | } |
mluis | 0:cb80564f40e1 | 71 | |
mluis | 0:cb80564f40e1 | 72 | void ClearLine( uint8_t param ) |
mluis | 0:cb80564f40e1 | 73 | { |
mluis | 0:cb80564f40e1 | 74 | // ESC [ Ps K |
mluis | 0:cb80564f40e1 | 75 | // 0 Erase from the active position to the end of the line, inclusive (default) |
mluis | 0:cb80564f40e1 | 76 | // 1 Erase from the start of the screen to the active position, inclusive |
mluis | 0:cb80564f40e1 | 77 | // 2 Erase all of the line, inclusive |
mluis | 0:cb80564f40e1 | 78 | this->printf( "\x1B[%dK", param ); |
mluis | 0:cb80564f40e1 | 79 | } |
mluis | 0:cb80564f40e1 | 80 | |
mluis | 0:cb80564f40e1 | 81 | void SetAttribute( uint8_t attr ) |
mluis | 0:cb80564f40e1 | 82 | { |
mluis | 0:cb80564f40e1 | 83 | // ESC [ Ps;...;Ps m |
mluis | 0:cb80564f40e1 | 84 | this->printf( "\x1B[%dm", attr ); |
mluis | 0:cb80564f40e1 | 85 | } |
mluis | 0:cb80564f40e1 | 86 | |
mluis | 0:cb80564f40e1 | 87 | void SetAttribute( uint8_t attr, uint8_t fgcolor, uint8_t bgcolor ) |
mluis | 0:cb80564f40e1 | 88 | { |
mluis | 0:cb80564f40e1 | 89 | // ESC [ Ps;...;Ps m |
mluis | 0:cb80564f40e1 | 90 | this->printf( "\x1B[%d;%d;%dm", attr, fgcolor + 30, bgcolor + 40 ); |
mluis | 0:cb80564f40e1 | 91 | } |
mluis | 0:cb80564f40e1 | 92 | |
mluis | 0:cb80564f40e1 | 93 | void SetCursorMode( uint8_t visible ) |
mluis | 0:cb80564f40e1 | 94 | { |
mluis | 0:cb80564f40e1 | 95 | if( visible == true ) |
mluis | 0:cb80564f40e1 | 96 | { |
mluis | 0:cb80564f40e1 | 97 | // ESC [ ? 25 h |
mluis | 0:cb80564f40e1 | 98 | this->printf( "\x1B[?25h" ); |
mluis | 0:cb80564f40e1 | 99 | } |
mluis | 0:cb80564f40e1 | 100 | else |
mluis | 0:cb80564f40e1 | 101 | { |
mluis | 0:cb80564f40e1 | 102 | // ESC [ ? 25 l |
mluis | 0:cb80564f40e1 | 103 | this->printf( "\x1B[?25l" ); |
mluis | 0:cb80564f40e1 | 104 | } |
mluis | 0:cb80564f40e1 | 105 | } |
mluis | 0:cb80564f40e1 | 106 | |
mluis | 0:cb80564f40e1 | 107 | void SetCursorPos( uint8_t line, uint8_t col ) |
mluis | 0:cb80564f40e1 | 108 | { |
mluis | 0:cb80564f40e1 | 109 | // ESC [ Pl ; Pc H |
mluis | 0:cb80564f40e1 | 110 | this->printf( "\x1B[%d;%dH", line, col ); |
mluis | 0:cb80564f40e1 | 111 | } |
mluis | 0:cb80564f40e1 | 112 | |
mluis | 0:cb80564f40e1 | 113 | void PutStringAt( uint8_t line, uint8_t col, const char *s ) |
mluis | 0:cb80564f40e1 | 114 | { |
mluis | 0:cb80564f40e1 | 115 | this->SetCursorPos( line, col ); |
mluis | 0:cb80564f40e1 | 116 | this->printf( "%s", s ); |
mluis | 0:cb80564f40e1 | 117 | } |
mluis | 0:cb80564f40e1 | 118 | |
mluis | 0:cb80564f40e1 | 119 | void PutCharAt( uint8_t line, uint8_t col, uint8_t c ) |
mluis | 0:cb80564f40e1 | 120 | { |
mluis | 0:cb80564f40e1 | 121 | this->SetCursorPos( line, col ); |
mluis | 0:cb80564f40e1 | 122 | this->printf( "%c", c ); |
mluis | 0:cb80564f40e1 | 123 | } |
mluis | 0:cb80564f40e1 | 124 | |
mluis | 0:cb80564f40e1 | 125 | void PutHexAt( uint8_t line, uint8_t col, uint16_t n ) |
mluis | 0:cb80564f40e1 | 126 | { |
mluis | 0:cb80564f40e1 | 127 | this->SetCursorPos( line, col ); |
mluis | 0:cb80564f40e1 | 128 | this->printf( "%X", n ); |
mluis | 0:cb80564f40e1 | 129 | } |
mluis | 0:cb80564f40e1 | 130 | |
mluis | 0:cb80564f40e1 | 131 | void PutBoxDrawingChar( uint8_t c ) |
mluis | 0:cb80564f40e1 | 132 | { |
mluis | 0:cb80564f40e1 | 133 | this->printf( "\x1B(0%c\x1b(B", c ); |
mluis | 0:cb80564f40e1 | 134 | } |
mluis | 0:cb80564f40e1 | 135 | |
mluis | 0:cb80564f40e1 | 136 | bool Readable( void ) |
mluis | 0:cb80564f40e1 | 137 | { |
mluis | 0:cb80564f40e1 | 138 | return this->readable( ); |
mluis | 0:cb80564f40e1 | 139 | } |
mluis | 0:cb80564f40e1 | 140 | |
mluis | 0:cb80564f40e1 | 141 | uint8_t GetChar( void ) |
mluis | 0:cb80564f40e1 | 142 | { |
mluis | 0:cb80564f40e1 | 143 | return this->getc( ); |
mluis | 0:cb80564f40e1 | 144 | } |
mluis | 0:cb80564f40e1 | 145 | |
mluis | 0:cb80564f40e1 | 146 | /* |
mluis | 0:cb80564f40e1 | 147 | * RawSerial class implmentation copy. |
mluis | 0:cb80564f40e1 | 148 | */ |
mluis | 0:cb80564f40e1 | 149 | /** Read a char from the serial port |
mluis | 0:cb80564f40e1 | 150 | * |
mluis | 0:cb80564f40e1 | 151 | * @returns The char read from the serial port |
mluis | 0:cb80564f40e1 | 152 | */ |
mluis | 0:cb80564f40e1 | 153 | int getc( ) |
mluis | 0:cb80564f40e1 | 154 | { |
mluis | 0:cb80564f40e1 | 155 | return _base_getc(); |
mluis | 0:cb80564f40e1 | 156 | } |
mluis | 0:cb80564f40e1 | 157 | |
mluis | 0:cb80564f40e1 | 158 | /** Write a char to the serial port |
mluis | 0:cb80564f40e1 | 159 | * |
mluis | 0:cb80564f40e1 | 160 | * @param c The char to write |
mluis | 0:cb80564f40e1 | 161 | * |
mluis | 0:cb80564f40e1 | 162 | * @returns The written char or -1 if an error occured |
mluis | 0:cb80564f40e1 | 163 | */ |
mluis | 0:cb80564f40e1 | 164 | int putc( int c ) |
mluis | 0:cb80564f40e1 | 165 | { |
mluis | 0:cb80564f40e1 | 166 | while( this->writeable( ) != 1 ); |
mluis | 0:cb80564f40e1 | 167 | return _base_putc( c ); |
mluis | 0:cb80564f40e1 | 168 | } |
mluis | 0:cb80564f40e1 | 169 | |
mluis | 0:cb80564f40e1 | 170 | /** Write a string to the serial port |
mluis | 0:cb80564f40e1 | 171 | * |
mluis | 0:cb80564f40e1 | 172 | * @param str The string to write |
mluis | 0:cb80564f40e1 | 173 | * |
mluis | 0:cb80564f40e1 | 174 | * @returns 0 if the write succeeds, EOF for error |
mluis | 0:cb80564f40e1 | 175 | */ |
mluis | 0:cb80564f40e1 | 176 | int puts( const char *str ) |
mluis | 0:cb80564f40e1 | 177 | { |
mluis | 0:cb80564f40e1 | 178 | while( *str ) |
mluis | 0:cb80564f40e1 | 179 | putc( *str++ ); |
mluis | 0:cb80564f40e1 | 180 | return 0; |
mluis | 0:cb80564f40e1 | 181 | } |
mluis | 0:cb80564f40e1 | 182 | |
mluis | 0:cb80564f40e1 | 183 | // Experimental support for printf in RawSerial. No Stream inheritance |
mluis | 0:cb80564f40e1 | 184 | // means we can't call printf() directly, so we use sprintf() instead. |
mluis | 0:cb80564f40e1 | 185 | // We only call malloc() for the sprintf() buffer if the buffer |
mluis | 0:cb80564f40e1 | 186 | // length is above a certain threshold, otherwise we use just the stack. |
mluis | 0:cb80564f40e1 | 187 | int printf( const char *format, ... ) |
mluis | 0:cb80564f40e1 | 188 | { |
mluis | 0:cb80564f40e1 | 189 | std::va_list arg; |
mluis | 0:cb80564f40e1 | 190 | va_start( arg, format ); |
mluis | 0:cb80564f40e1 | 191 | int len = vsnprintf( NULL, 0, format, arg ); |
mluis | 0:cb80564f40e1 | 192 | if( len < STRING_STACK_LIMIT ) |
mluis | 0:cb80564f40e1 | 193 | { |
mluis | 0:cb80564f40e1 | 194 | char temp[STRING_STACK_LIMIT]; |
mluis | 0:cb80564f40e1 | 195 | vsprintf( temp, format, arg ); |
mluis | 0:cb80564f40e1 | 196 | puts( temp ); |
mluis | 0:cb80564f40e1 | 197 | } |
mluis | 0:cb80564f40e1 | 198 | else |
mluis | 0:cb80564f40e1 | 199 | { |
mluis | 0:cb80564f40e1 | 200 | char *temp = new char[len + 1]; |
mluis | 0:cb80564f40e1 | 201 | vsprintf( temp, format, arg ); |
mluis | 0:cb80564f40e1 | 202 | puts( temp ); |
mluis | 0:cb80564f40e1 | 203 | delete[] temp; |
mluis | 0:cb80564f40e1 | 204 | } |
mluis | 0:cb80564f40e1 | 205 | va_end( arg ); |
mluis | 0:cb80564f40e1 | 206 | return len; |
mluis | 0:cb80564f40e1 | 207 | } |
mluis | 0:cb80564f40e1 | 208 | |
mluis | 0:cb80564f40e1 | 209 | private: |
mluis | 0:cb80564f40e1 | 210 | |
mluis | 0:cb80564f40e1 | 211 | }; |
mluis | 0:cb80564f40e1 | 212 | |
mluis | 0:cb80564f40e1 | 213 | #endif // __VT100_H__ |