ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Terminal.h
1 /*
2  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef TERMINAL_h
24 #define TERMINAL_h
25 
26 #include <Arduino.h>
27 #include <USBAPI.h>
28 #include "USBKeysExtra.h"
29 
30 // Special key code that indicates that unicodeKey() contains the actual code.
31 #define KEY_UNICODE 0x1000
32 
33 // Special key code that indicates that the window size has changed.
34 #define KEY_WINSIZE 0x1001
35 
36 class Terminal : public Stream
37 {
38 public:
39  Terminal();
40  virtual ~Terminal();
41 
42  enum Mode
43  {
46  };
47 
48  void begin(Stream &stream, Mode mode = Serial);
49  void end();
50 
51  Stream *stream() const { return _stream; }
52  Terminal::Mode mode() const { return (Terminal::Mode)mod; }
53 
54  virtual int available();
55  virtual int peek();
56  virtual int read();
57 
58  virtual void flush();
59 
60  virtual size_t write(uint8_t c);
61  virtual size_t write(const uint8_t *buffer, size_t size);
62  using Stream::write;
63 
64  void writeProgMem(const char *str);
65 
66  int readKey();
67 
68  long unicodeKey() const { return ucode; }
69 
70  size_t writeUnicode(long code);
71 
72  int columns() const { return ncols; }
73  int rows() const { return nrows; }
74 
75  bool setWindowSize(int columns, int rows);
76 
77  void clear();
78  void clearToEOL();
79 
80  void cursorMove(int x, int y);
81  void cursorLeft();
82  void cursorRight();
83  void cursorUp();
84  void cursorDown();
85 
86  void backspace();
87 
88  void insertLine();
89  void insertChar();
90  void deleteLine();
91  void deleteChar();
92 
93  void scrollUp();
94  void scrollDown();
95 
96  void normal();
97  void bold();
98  void underline();
99  void blink();
100  void reverse();
101 
102  enum Color
103  {
104  Black = 0x00,
105  DarkRed = 0x01,
106  DarkGreen = 0x02,
107  DarkYellow = 0x03,
108  DarkBlue = 0x04,
109  DarkMagenta = 0x05,
110  DarkCyan = 0x06,
111  LightGray = 0x07,
112  DarkGray = 0x08,
113  Red = 0x09,
114  Green = 0x0A,
115  Yellow = 0x0B,
116  Blue = 0x0C,
117  Magenta = 0x0D,
118  Cyan = 0x0E,
119  White = 0x0F
120  };
121 
122  void color(Color fg);
123  void color(Color fg, Color bg);
124 
125  static bool isWideCharacter(long code);
126 
127  static size_t utf8Length(long code);
128  static size_t utf8Format(uint8_t *buffer, long code);
129 
130 private:
131  Stream *_stream;
132  long ucode;
133  int ncols, nrows;
134  unsigned long timer;
135  uint16_t offset;
136  uint8_t state;
137  uint8_t utf8len;
138  uint8_t mod;
139  uint8_t sb[8];
140  uint8_t flags;
141 
142  int matchEscape(int ch);
143  void telnetCommand(uint8_t type, uint8_t option);
144 };
145 
146 #endif
void insertChar()
Inserts a blank character at the cursor position.
Definition: Terminal.cpp:971
Color is white.
Definition: Terminal.h:119
void cursorDown()
Moves the cursor down by one line.
Definition: Terminal.cpp:931
void insertLine()
Inserts a line at the cursor position.
Definition: Terminal.cpp:960
Color is light green.
Definition: Terminal.h:114
virtual ~Terminal()
Destroys this terminal object.
Definition: Terminal.cpp:150
Color is dark cyan.
Definition: Terminal.h:110
void deleteLine()
Deletes a line at the cursor position.
Definition: Terminal.cpp:982
Color is light blue.
Definition: Terminal.h:116
Color is dark green.
Definition: Terminal.h:106
Color is dark red.
Definition: Terminal.h:105
void cursorLeft()
Moves the cursor left by one character.
Definition: Terminal.cpp:898
void reverse()
Reverse the foreground and background colors for inverted text.
Definition: Terminal.cpp:1064
Color
Terminal foreground or background colors.
Definition: Terminal.h:102
long unicodeKey() const
Gets the Unicode version of the last key returned by readKey().
Definition: Terminal.h:68
void scrollDown()
Scrolls the contents of the window down one line.
Definition: Terminal.cpp:1015
virtual void flush()
Flushes all data in the underlying stream.
Definition: Terminal.cpp:274
Color is black.
Definition: Terminal.h:104
virtual size_t write(uint8_t c)
Writes a single byte to the underlying stream.
Definition: Terminal.cpp:286
void deleteChar()
Deletes the character at the cursor position.
Definition: Terminal.cpp:993
void bold()
Enables bold text.
Definition: Terminal.cpp:1037
void clearToEOL()
Clears from the current cursor position to the end of the line.
Definition: Terminal.cpp:833
Color is light cyan.
Definition: Terminal.h:118
void color(Color fg)
Selects a text foreground color with the default background color.
Definition: Terminal.cpp:1174
Operates the terminal in serial mode.
Definition: Terminal.h:44
void begin(Stream &stream, Mode mode=Serial)
Begins terminal operations on an underlying stream.
Definition: Terminal.cpp:183
Color is light red.
Definition: Terminal.h:113
void cursorUp()
Moves the cursor up by one line.
Definition: Terminal.cpp:920
void backspace()
Backspaces over the last character.
Definition: Terminal.cpp:949
void cursorRight()
Moves the cursor right by one character.
Definition: Terminal.cpp:909
void cursorMove(int x, int y)
Moves the cursor to a specific location in the window.
Definition: Terminal.cpp:866
Operates the terminal in telnet mode.
Definition: Terminal.h:45
bool setWindowSize(int columns, int rows)
Sets the number of columns and rows in the window.
Definition: Terminal.cpp:801
Terminal::Mode mode() const
Returns the mode this terminal is operating in, Serial or Telnet.
Definition: Terminal.h:52
Color is light gray.
Definition: Terminal.h:111
Extended stream interface for terminal operations.
Definition: Terminal.h:36
size_t writeUnicode(long code)
Writes a Unicode code point to the output in UTF-8 encoding.
Definition: Terminal.cpp:757
Color is dark yellow.
Definition: Terminal.h:107
Color is dark gray.
Definition: Terminal.h:112
Mode
Mode to operate in, Serial or Telnet.
Definition: Terminal.h:42
Color is dark blue.
Definition: Terminal.h:108
Terminal()
Constructs a terminal object.
Definition: Terminal.cpp:133
Stream * stream() const
Returns a pointer to the underlying Stream, or NULL if the stream has not been set with begin() yet...
Definition: Terminal.h:51
virtual int peek()
Peeks at the next byte from the underlying stream.
Definition: Terminal.cpp:240
void underline()
Enables underlined text.
Definition: Terminal.cpp:1046
void clear()
Move the cursor to the top-left position and clear the screen.
Definition: Terminal.cpp:824
Color is light magenta.
Definition: Terminal.h:117
void writeProgMem(const char *str)
Writes a static string that is stored in program memory.
Definition: Terminal.cpp:314
virtual int read()
Reads the next byte from the underlying stream.
Definition: Terminal.cpp:261
Color is dark magenta.
Definition: Terminal.h:109
static size_t utf8Format(uint8_t *buffer, long code)
Formats a Unicode code point in a buffer in the UTF-8 encoding.
Definition: Terminal.cpp:1334
int rows() const
Gets the number of rows in the window; defaults to 24.
Definition: Terminal.h:73
void scrollUp()
Scrolls the contents of the window up one line.
Definition: Terminal.cpp:1004
static size_t utf8Length(long code)
Determines the length of a Unicode code point in the UTF-8 encoding.
Definition: Terminal.cpp:1302
void end()
Ends terminal operations on an underlying stream.
Definition: Terminal.cpp:198
Color is light yellow.
Definition: Terminal.h:115
int readKey()
Reads the next key that was typed on this terminal.
Definition: Terminal.cpp:387
void normal()
Selects normal text with all attributes and colors off.
Definition: Terminal.cpp:1026
void blink()
Enables blinking text.
Definition: Terminal.cpp:1055
int columns() const
Gets the number of columns in the window; defaults to 80.
Definition: Terminal.h:72
static bool isWideCharacter(long code)
Determine if a Unicode character is wide.
Definition: Terminal.cpp:1247
virtual int available()
Returns the number of bytes that are available for reading.
Definition: Terminal.cpp:228