a VT100 library found on the mbed site - w/o any licensing information or restrictions.

Committer:
WiredHome
Date:
Thu Apr 30 13:21:08 2020 +0000
Revision:
1:98f12a26a3da
Parent:
0:8f2947c0686e
General clean up of the interface APIs (not backward compatible, but better).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:8f2947c0686e 1 #ifndef VT100_H
WiredHome 0:8f2947c0686e 2 #define VT100_H included
WiredHome 1:98f12a26a3da 3 #include "mbed.h"
WiredHome 0:8f2947c0686e 4
WiredHome 0:8f2947c0686e 5 /** vt100 class
WiredHome 0:8f2947c0686e 6 * Utility for handling text/letter on a terminal
WiredHome 0:8f2947c0686e 7 * which can handle VT100 escape command sequence.
WiredHome 0:8f2947c0686e 8 *
WiredHome 1:98f12a26a3da 9 * Derived from https://os.mbed.com/users/Rhyme/code/vt100/file/b7229a9eae1c/vt100.h/
WiredHome 1:98f12a26a3da 10 * which had no licensing requirements.
WiredHome 0:8f2947c0686e 11 *
WiredHome 0:8f2947c0686e 12 * Example:
WiredHome 0:8f2947c0686e 13 * @code
WiredHome 0:8f2947c0686e 14 * #include "mbed.h"
WiredHome 0:8f2947c0686e 15 * #include "vt100.h"
WiredHome 0:8f2947c0686e 16 *
WiredHome 0:8f2947c0686e 17 * vt100 tty;
WiredHome 0:8f2947c0686e 18 *
WiredHome 0:8f2947c0686e 19 * int main() {
WiredHome 0:8f2947c0686e 20 * int count = 0 ;
WiredHome 0:8f2947c0686e 21 * tty.cls() ;
WiredHome 1:98f12a26a3da 22 * tty.setFG(VT_COLOR::VT_BLUE) ;
WiredHome 0:8f2947c0686e 23 * tty.frame(5, 5, 15, 9) ;
WiredHome 0:8f2947c0686e 24 * while(1) {
WiredHome 0:8f2947c0686e 25 * tty.locate(7, 7) ;
WiredHome 0:8f2947c0686e 26 * tty.setFG(count % 8) ;
WiredHome 0:8f2947c0686e 27 * printf("%d\r\n", count++) ;
WiredHome 0:8f2947c0686e 28 * wait(1.0) ;
WiredHome 0:8f2947c0686e 29 * }
WiredHome 0:8f2947c0686e 30 * }
WiredHome 0:8f2947c0686e 31 * @endcode
WiredHome 0:8f2947c0686e 32 *
WiredHome 0:8f2947c0686e 33 */
WiredHome 0:8f2947c0686e 34
WiredHome 1:98f12a26a3da 35 class vt100 : public Stream {
WiredHome 0:8f2947c0686e 36 public:
WiredHome 0:8f2947c0686e 37 /** constructor
WiredHome 1:98f12a26a3da 38 * @param pc is a pointer to the Serial Interface to use.
WiredHome 1:98f12a26a3da 39 * @param name is the interface name
WiredHome 0:8f2947c0686e 40 */
WiredHome 1:98f12a26a3da 41 vt100(Serial * pc, const char * name) ;
WiredHome 0:8f2947c0686e 42
WiredHome 0:8f2947c0686e 43 /** destructor */
WiredHome 1:98f12a26a3da 44 virtual ~vt100();
WiredHome 0:8f2947c0686e 45
WiredHome 0:8f2947c0686e 46 /** clear screen */
WiredHome 0:8f2947c0686e 47 void cls(void) ;
WiredHome 0:8f2947c0686e 48
WiredHome 0:8f2947c0686e 49 /** move cursor to (x, y)
WiredHome 0:8f2947c0686e 50 * @param x start column of the next letter
WiredHome 0:8f2947c0686e 51 * @param y start row of the next letter
WiredHome 0:8f2947c0686e 52 * @note no value checking is performed.
WiredHome 0:8f2947c0686e 53 */
WiredHome 0:8f2947c0686e 54 void locate(int x, int y) ;
WiredHome 0:8f2947c0686e 55
WiredHome 0:8f2947c0686e 56 /** print a letter c at (x,y)
WiredHome 0:8f2947c0686e 57 * @param c the letter to be written
WiredHome 0:8f2947c0686e 58 * @param x column of the letter
WiredHome 0:8f2947c0686e 59 * @param y row of the letter
WiredHome 0:8f2947c0686e 60 */
WiredHome 0:8f2947c0686e 61 void putChar(int x, int y, char c) ;
WiredHome 0:8f2947c0686e 62
WiredHome 0:8f2947c0686e 63 /** print a string str from (x,y)
WiredHome 0:8f2947c0686e 64 * @param *str c-style string to be written
WiredHome 0:8f2947c0686e 65 * @param x column of the first letter
WiredHome 0:8f2947c0686e 66 * @param y row of the first letter
WiredHome 0:8f2947c0686e 67 */
WiredHome 1:98f12a26a3da 68 void putStr(int x, int y, const char *str) ;
WiredHome 0:8f2947c0686e 69
WiredHome 0:8f2947c0686e 70 /** print a line of char
WiredHome 0:8f2947c0686e 71 * @param x1 starting column
WiredHome 0:8f2947c0686e 72 * @param y1 starting row
WiredHome 0:8f2947c0686e 73 * @param x2 ending column
WiredHome 0:8f2947c0686e 74 * @param y2 ending row
WiredHome 1:98f12a26a3da 75 * @param c the letter to form the line
WiredHome 0:8f2947c0686e 76 */
WiredHome 0:8f2947c0686e 77 void line(int x1, int y1, int x2, int y2, char c='*') ;
WiredHome 0:8f2947c0686e 78
WiredHome 0:8f2947c0686e 79 /** print a text frame
WiredHome 0:8f2947c0686e 80 * @param x1 left column
WiredHome 0:8f2947c0686e 81 * @param y1 top row
WiredHome 0:8f2947c0686e 82 * @param x2 right column
WiredHome 0:8f2947c0686e 83 * @param y2 bottom row
WiredHome 0:8f2947c0686e 84 */
WiredHome 0:8f2947c0686e 85 void frame(int x1, int y1, int x2, int y2) ;
WiredHome 0:8f2947c0686e 86
WiredHome 0:8f2947c0686e 87 /** print a text circle
WiredHome 0:8f2947c0686e 88 * @param x0 center column
WiredHome 0:8f2947c0686e 89 * @param y1 center row
WiredHome 0:8f2947c0686e 90 * @param r radius
WiredHome 1:98f12a26a3da 91 * @param c the optional letter to form the circle, default '*'
WiredHome 0:8f2947c0686e 92 */
WiredHome 0:8f2947c0686e 93 void circle(int x0, int y0, int r, char c='*') ;
WiredHome 0:8f2947c0686e 94
WiredHome 1:98f12a26a3da 95 /// VT Color definitions
WiredHome 1:98f12a26a3da 96 typedef enum {
WiredHome 1:98f12a26a3da 97 VT_BLACK , ///< VT Color Black
WiredHome 1:98f12a26a3da 98 VT_RED , ///< VT Color Red
WiredHome 1:98f12a26a3da 99 VT_GREEN , ///< VT Color Green
WiredHome 1:98f12a26a3da 100 VT_YELLOW , ///< VT Color Yellow
WiredHome 1:98f12a26a3da 101 VT_BLUE , ///< VT Color Blue
WiredHome 1:98f12a26a3da 102 VT_PURPLE , ///< VT Color Purple
WiredHome 1:98f12a26a3da 103 VT_CYAN , ///< VT Color Cyan
WiredHome 1:98f12a26a3da 104 VT_WHITE , ///< VT Color White
WiredHome 1:98f12a26a3da 105 } VT_COLOR;
WiredHome 1:98f12a26a3da 106
WiredHome 0:8f2947c0686e 107 /** set foreground color
WiredHome 0:8f2947c0686e 108 * @param newFG new foreground color
WiredHome 0:8f2947c0686e 109 * @returns previous foreground color
WiredHome 1:98f12a26a3da 110 * @note VT_COLOR::VT_BLACK
WiredHome 1:98f12a26a3da 111 * @note VT_COLOR::VT_RED
WiredHome 1:98f12a26a3da 112 * @note VT_COLOR::VT_GREEN
WiredHome 1:98f12a26a3da 113 * @note VT_COLOR::VT_YELLOW
WiredHome 1:98f12a26a3da 114 * @note VT_COLOR::VT_BLUE
WiredHome 1:98f12a26a3da 115 * @note VT_COLOR::VT_PURPLE
WiredHome 1:98f12a26a3da 116 * @note VT_COLOR::VT_CIAN
WiredHome 1:98f12a26a3da 117 * @note VT_COLOR::VT_WHITE
WiredHome 0:8f2947c0686e 118 */
WiredHome 1:98f12a26a3da 119 VT_COLOR setFG(VT_COLOR newFG) ;
WiredHome 0:8f2947c0686e 120
WiredHome 0:8f2947c0686e 121 /** get current foreground color
WiredHome 0:8f2947c0686e 122 * @returns current foreground color
WiredHome 0:8f2947c0686e 123 */
WiredHome 1:98f12a26a3da 124 VT_COLOR getFG(void) ;
WiredHome 0:8f2947c0686e 125
WiredHome 0:8f2947c0686e 126 /** set background color
WiredHome 0:8f2947c0686e 127 * @param newBG new background color
WiredHome 0:8f2947c0686e 128 * @returns previous background color
WiredHome 0:8f2947c0686e 129 */
WiredHome 1:98f12a26a3da 130 VT_COLOR setBG(VT_COLOR newBG) ;
WiredHome 0:8f2947c0686e 131
WiredHome 0:8f2947c0686e 132 /** get current background color
WiredHome 0:8f2947c0686e 133 * @returns current background color
WiredHome 0:8f2947c0686e 134 */
WiredHome 1:98f12a26a3da 135 VT_COLOR getBG(void) ;
WiredHome 1:98f12a26a3da 136
WiredHome 0:8f2947c0686e 137 protected:
WiredHome 1:98f12a26a3da 138 /// a method to put a character to the display.
WiredHome 1:98f12a26a3da 139 ///
WiredHome 1:98f12a26a3da 140 /// @param value is the character value to send to the display
WiredHome 1:98f12a26a3da 141 /// @returns the character that was sent.
WiredHome 1:98f12a26a3da 142 ///
WiredHome 1:98f12a26a3da 143 virtual int _putc(int value);
WiredHome 1:98f12a26a3da 144
WiredHome 1:98f12a26a3da 145
WiredHome 1:98f12a26a3da 146 /// a method to get a character from the stdin
WiredHome 1:98f12a26a3da 147 ///
WiredHome 1:98f12a26a3da 148
WiredHome 1:98f12a26a3da 149 /// @returns the fetched character.
WiredHome 1:98f12a26a3da 150 ///
WiredHome 1:98f12a26a3da 151 virtual int _getc();
WiredHome 1:98f12a26a3da 152
WiredHome 1:98f12a26a3da 153
WiredHome 1:98f12a26a3da 154 virtual int readable();
WiredHome 1:98f12a26a3da 155
WiredHome 0:8f2947c0686e 156 private:
WiredHome 1:98f12a26a3da 157 Serial * pc;
WiredHome 1:98f12a26a3da 158 VT_COLOR foreground ;
WiredHome 1:98f12a26a3da 159 VT_COLOR background ;
WiredHome 0:8f2947c0686e 160
WiredHome 0:8f2947c0686e 161 } ;
WiredHome 0:8f2947c0686e 162
WiredHome 0:8f2947c0686e 163 #endif /* VT100_H */