My implementation of VT100 ESC-sequence utility
vt100.h@4:d2c3bf5d00f4, 2018-02-08 (annotated)
- Committer:
- Rhyme
- Date:
- Thu Feb 08 05:36:56 2018 +0000
- Revision:
- 4:d2c3bf5d00f4
- Parent:
- 3:141a8a98c504
temporary commit not working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:94253645a02a | 1 | #ifndef VT100_H |
Rhyme | 0:94253645a02a | 2 | #define VT100_H included |
Rhyme | 3:141a8a98c504 | 3 | #include "mbed.h" |
Rhyme | 0:94253645a02a | 4 | |
Rhyme | 0:94253645a02a | 5 | /** vt100 class |
Rhyme | 0:94253645a02a | 6 | * Utility for handling text/letter on a terminal |
Rhyme | 0:94253645a02a | 7 | * which can handle VT100 escape command sequence. |
Rhyme | 0:94253645a02a | 8 | * |
Rhyme | 0:94253645a02a | 9 | * Example: |
Rhyme | 0:94253645a02a | 10 | * @code |
Rhyme | 0:94253645a02a | 11 | * #include "mbed.h" |
Rhyme | 0:94253645a02a | 12 | * #include "vt100.h" |
Rhyme | 0:94253645a02a | 13 | * |
Rhyme | 0:94253645a02a | 14 | * vt100 tty ; |
Rhyme | 0:94253645a02a | 15 | * |
Rhyme | 0:94253645a02a | 16 | * int main() { |
Rhyme | 0:94253645a02a | 17 | * int count = 0 ; |
Rhyme | 0:94253645a02a | 18 | * tty.cls() ; |
Rhyme | 0:94253645a02a | 19 | * tty.black() ; |
Rhyme | 0:94253645a02a | 20 | * tty.frame(5, 5, 15, 9) ; |
Rhyme | 0:94253645a02a | 21 | * while(1) { |
Rhyme | 0:94253645a02a | 22 | * tty.locate(7, 7) ; |
Rhyme | 0:94253645a02a | 23 | * tty.setFG(count % 8) ; |
Rhyme | 0:94253645a02a | 24 | * printf("%d\r\n", count++) ; |
Rhyme | 0:94253645a02a | 25 | * wait(1.0) ; |
Rhyme | 0:94253645a02a | 26 | * } |
Rhyme | 0:94253645a02a | 27 | * } |
Rhyme | 0:94253645a02a | 28 | * @endcode |
Rhyme | 0:94253645a02a | 29 | * |
Rhyme | 0:94253645a02a | 30 | * Note: I know there should be other libraries |
Rhyme | 0:94253645a02a | 31 | * with similar functions, but I could not help |
Rhyme | 0:94253645a02a | 32 | * writing one for myself anyway. |
Rhyme | 0:94253645a02a | 33 | */ |
Rhyme | 0:94253645a02a | 34 | |
Rhyme | 3:141a8a98c504 | 35 | class vt100 : public Serial { |
Rhyme | 0:94253645a02a | 36 | public: |
Rhyme | 0:94253645a02a | 37 | /** constructor |
Rhyme | 0:94253645a02a | 38 | * @param baud baud rate |
Rhyme | 0:94253645a02a | 39 | */ |
Rhyme | 0:94253645a02a | 40 | vt100(int baud = 115200) ; |
Rhyme | 0:94253645a02a | 41 | |
Rhyme | 3:141a8a98c504 | 42 | vt100(PinName tx_pin, PinName rx_pin, int baud=115200) ; |
Rhyme | 3:141a8a98c504 | 43 | |
Rhyme | 0:94253645a02a | 44 | /** destructor */ |
Rhyme | 4:d2c3bf5d00f4 | 45 | ~vt100() ; |
Rhyme | 0:94253645a02a | 46 | |
Rhyme | 0:94253645a02a | 47 | /** clear screen */ |
Rhyme | 0:94253645a02a | 48 | void cls(void) ; |
Rhyme | 0:94253645a02a | 49 | |
Rhyme | 0:94253645a02a | 50 | /** move cursor to (x, y) |
Rhyme | 0:94253645a02a | 51 | * @param x start column of the next letter |
Rhyme | 0:94253645a02a | 52 | * @param y start row of the next letter |
Rhyme | 0:94253645a02a | 53 | * @note no value checking is performed. |
Rhyme | 0:94253645a02a | 54 | */ |
Rhyme | 0:94253645a02a | 55 | void locate(int x, int y) ; |
Rhyme | 0:94253645a02a | 56 | |
Rhyme | 0:94253645a02a | 57 | /** print a letter c at (x,y) |
Rhyme | 0:94253645a02a | 58 | * @param c the letter to be written |
Rhyme | 0:94253645a02a | 59 | * @param x column of the letter |
Rhyme | 0:94253645a02a | 60 | * @param y row of the letter |
Rhyme | 0:94253645a02a | 61 | */ |
Rhyme | 0:94253645a02a | 62 | void putChar(int x, int y, char c) ; |
Rhyme | 0:94253645a02a | 63 | |
Rhyme | 0:94253645a02a | 64 | /** print a string str from (x,y) |
Rhyme | 0:94253645a02a | 65 | * @param *str c-style string to be written |
Rhyme | 0:94253645a02a | 66 | * @param x column of the first letter |
Rhyme | 0:94253645a02a | 67 | * @param y row of the first letter |
Rhyme | 0:94253645a02a | 68 | */ |
Rhyme | 0:94253645a02a | 69 | void putStr(int x, int y, char *str) ; |
Rhyme | 0:94253645a02a | 70 | |
Rhyme | 0:94253645a02a | 71 | /** print a line of char |
Rhyme | 0:94253645a02a | 72 | * @param c the letter to form the line |
Rhyme | 0:94253645a02a | 73 | * @param x1 starting column |
Rhyme | 0:94253645a02a | 74 | * @param y1 starting row |
Rhyme | 0:94253645a02a | 75 | * @param x2 ending column |
Rhyme | 0:94253645a02a | 76 | * @param y2 ending row |
Rhyme | 0:94253645a02a | 77 | */ |
Rhyme | 0:94253645a02a | 78 | void line(int x1, int y1, int x2, int y2, char c='*') ; |
Rhyme | 0:94253645a02a | 79 | |
Rhyme | 0:94253645a02a | 80 | /** print a text frame |
Rhyme | 0:94253645a02a | 81 | * @param x1 left column |
Rhyme | 0:94253645a02a | 82 | * @param y1 top row |
Rhyme | 0:94253645a02a | 83 | * @param x2 right column |
Rhyme | 0:94253645a02a | 84 | * @param y2 bottom row |
Rhyme | 0:94253645a02a | 85 | */ |
Rhyme | 0:94253645a02a | 86 | void frame(int x1, int y1, int x2, int y2) ; |
Rhyme | 0:94253645a02a | 87 | |
Rhyme | 0:94253645a02a | 88 | /** print a text circle |
Rhyme | 0:94253645a02a | 89 | * @c the letter to form the circle |
Rhyme | 0:94253645a02a | 90 | * @param x0 center column |
Rhyme | 0:94253645a02a | 91 | * @param y1 center row |
Rhyme | 0:94253645a02a | 92 | * @param r radius |
Rhyme | 0:94253645a02a | 93 | */ |
Rhyme | 0:94253645a02a | 94 | void circle(int x0, int y0, int r, char c='*') ; |
Rhyme | 0:94253645a02a | 95 | |
Rhyme | 0:94253645a02a | 96 | /** set foreground color |
Rhyme | 0:94253645a02a | 97 | * @param newFG new foreground color |
Rhyme | 0:94253645a02a | 98 | * @returns previous foreground color |
Rhyme | 0:94253645a02a | 99 | * @note 0 BLACK |
Rhyme | 0:94253645a02a | 100 | * @note 1 RED |
Rhyme | 0:94253645a02a | 101 | * @note 2 GREEN |
Rhyme | 0:94253645a02a | 102 | * @note 3 YELLOW |
Rhyme | 0:94253645a02a | 103 | * @note 4 BLUE |
Rhyme | 0:94253645a02a | 104 | * @note 5 PURPLE |
Rhyme | 0:94253645a02a | 105 | * @note 6 CIAN |
Rhyme | 0:94253645a02a | 106 | * @note 7 WHITE |
Rhyme | 0:94253645a02a | 107 | */ |
Rhyme | 0:94253645a02a | 108 | int setFG(int newFG) ; |
Rhyme | 0:94253645a02a | 109 | |
Rhyme | 0:94253645a02a | 110 | /** get current foreground color |
Rhyme | 0:94253645a02a | 111 | * @returns current foreground color |
Rhyme | 0:94253645a02a | 112 | */ |
Rhyme | 0:94253645a02a | 113 | int getFG(void) ; |
Rhyme | 0:94253645a02a | 114 | |
Rhyme | 0:94253645a02a | 115 | /** set background color |
Rhyme | 0:94253645a02a | 116 | * @param newBG new background color |
Rhyme | 0:94253645a02a | 117 | * @returns previous background color |
Rhyme | 0:94253645a02a | 118 | */ |
Rhyme | 0:94253645a02a | 119 | int setBG(int newBG) ; |
Rhyme | 0:94253645a02a | 120 | |
Rhyme | 0:94253645a02a | 121 | /** get current background color |
Rhyme | 0:94253645a02a | 122 | * @returns current background color |
Rhyme | 0:94253645a02a | 123 | */ |
Rhyme | 0:94253645a02a | 124 | int getBG(void) ; |
Rhyme | 0:94253645a02a | 125 | /** set foreground color to black */ |
Rhyme | 0:94253645a02a | 126 | void black(void) ; |
Rhyme | 0:94253645a02a | 127 | /** set foreground color to red */ |
Rhyme | 0:94253645a02a | 128 | void red(void) ; |
Rhyme | 0:94253645a02a | 129 | /** set foreground color to green */ |
Rhyme | 0:94253645a02a | 130 | void green(void) ; |
Rhyme | 0:94253645a02a | 131 | /** set foreground color to yellow */ |
Rhyme | 0:94253645a02a | 132 | void yellow(void) ; |
Rhyme | 0:94253645a02a | 133 | /** set foreground color to blue */ |
Rhyme | 0:94253645a02a | 134 | void blue(void) ; |
Rhyme | 0:94253645a02a | 135 | /** set foreground color to purple */ |
Rhyme | 0:94253645a02a | 136 | void purple(void) ; |
Rhyme | 0:94253645a02a | 137 | /** set foreground color to cian */ |
Rhyme | 0:94253645a02a | 138 | void cian(void) ; |
Rhyme | 0:94253645a02a | 139 | /** set foreground color to white */ |
Rhyme | 0:94253645a02a | 140 | void white(void) ; |
Rhyme | 0:94253645a02a | 141 | protected: |
Rhyme | 0:94253645a02a | 142 | private: |
Rhyme | 0:94253645a02a | 143 | int foreground ; |
Rhyme | 0:94253645a02a | 144 | int background ; |
Rhyme | 0:94253645a02a | 145 | |
Rhyme | 0:94253645a02a | 146 | } ; |
Rhyme | 0:94253645a02a | 147 | |
Rhyme | 0:94253645a02a | 148 | #endif /* VT100_H */ |