ESP32VGA
ESP32 VGA Controller and Graphics Library
VGATerminal.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com)
3  Copyright (c) 2018 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of ESP32VGA Library.
7 
8  ESP32VGA is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  ESP32VGA is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with ESP32VGA. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef _VGATERMINAL_H_INCLUDED
24 #define _VGATERMINAL_H_INCLUDED
25 
26 
34 #include "freertos/FreeRTOS.h"
35 
36 #include "VGAConf.h"
37 #include "VGACanvas.h"
38 #include "VGAKeyboard.h"
39 
40 #include "Stream.h"
41 
42 
43 
209 namespace ESP32VGA {
210 
211 
212 
213 
214 // used by saveCursorState / restoreCursorState
215 struct TerminalCursorState_t {
216  TerminalCursorState_t * next;
217  int16_t cursorX;
218  int16_t cursorY;
219  uint8_t * tabStop;
220  bool cursorPastLastCol;
221  bool originMode;
222  GlyphOptions_t glyphOptions;
223  uint8_t characterSetIndex;
224  uint8_t characterSet[4];
225 };
226 
227 
228 enum class KeypadMode_t {
229  Application, // DECKPAM
230  Numeric, // DECKPNM
231 };
232 
233 
234 
238 class VGATerminalClass : public Stream {
239 
240 public:
241 
249  void begin();
250 
255  void end();
256 
257  // When serial port is set the typed keys on PS/2 keyboard are encoded
258  // as ANSI/VT100 codes and then sent to the specified port.
259  // Also terminal queries like terminal identification, cursor position, etc.. will be
260  // sent to the stream.
261  // Call pollSerialPort() to send codes from serial port to the display.
262  void connectSerialPort(HardwareSerial & serialPort, bool autoXONXOFF = true);
263  void pollSerialPort();
264 
265  void setLogStream(Stream & stream) { m_logStream = &stream; }
266  void logFmt(const char * format, ...);
267  void log(const char * txt);
268  void log(char c);
269 
270  void loadFont(FontInfo const * font);
271 
272  void setBackgroundColor(Color color, bool setAsDefault = true);
273  void setForegroundColor(Color color, bool setAsDefault = true);
274 
275  void clear();
276 
277  void flush(bool waitVSync);
278 
279  // informational
280  int16_t getColumns() { return m_columns; }
281  int16_t getRows() { return m_rows; }
282 
283  // cursor control
284  void enableCursor(bool value);
285 
286  int availableForWrite();
287 
288  // Stream abstract class implementation
289  int available();
290  int read();
291  int peek();
292  void flush();
293  size_t write(const uint8_t * buffer, size_t size);
294  size_t write(uint8_t c);
295 
296 
297 private:
298 
299  void reset();
300  void int_clear();
301  void clearMap(uint32_t * map);
302 
303  void freeFont();
304  void freeTabStops();
305  void freeGlyphsMap();
306 
307  void set132ColumnMode(bool value);
308 
309  bool moveUp();
310  bool moveDown();
311  void setCursorPos(int16_t X, int16_t Y);
312  int16_t getAbsoluteRow(int16_t Y);
313 
314  void int_setBackgroundColor(Color color);
315  void int_setForegroundColor(Color color);
316 
317  // tab stops
318  void nextTabStop();
319  void setTabStop(int16_t column, bool set);
320  void resetTabStops();
321 
322  // scroll control
323  void scrollDown();
324  void scrollDownAt(int16_t startingRow);
325  void scrollUp();
326  void scrollUpAt(int16_t startingRow);
327  void setScrollingRegion(int16_t top, int16_t down, bool resetCursorPos = true);
328 
329  // multilevel save/restore cursor state
330  void saveCursorState();
331  void restoreCursorState();
332  void clearSavedCursorStates();
333 
334  void erase(int16_t X1, int16_t Y1, int16_t X2, int16_t Y2, char c, bool maintainDoubleWidth);
335 
336  void consumeInputQueue();
337  void consumeESC();
338  void consumeCSI();
339  void consumeCSISPC(int * params, int paramsCount);
340  char consumeParamsAndGetCode(int * params, int * paramsCount, bool * questionMarkFound);
341  void consumeDECPrivateModes(int const * params, int paramsCount, char c);
342  void consumeDCS();
343  void execSGRParameters(int const * params, int paramsCount);
344 
345  void execCtrlCode(char c);
346 
347  static void charsConsumerTask(void * pvParameters);
348  static void keyboardReaderTask(void * pvParameters);
349 
350  static void blinkTimerFunc(TimerHandle_t xTimer);
351  void blinkText();
352  bool enableBlinkingText(bool value);
353  void blinkCursor();
354  bool int_enableCursor(bool value);
355 
356  char getNextCode(bool processCtrlCodes);
357 
358  void setChar(char c);
359  GlyphOptions_t getGlyphOptionsAt(int16_t X, int16_t Y);
360 
361  void insertAt(int16_t column, int16_t row, uint16_t count);
362  void deleteAt(int16_t column, int16_t row, uint16_t count);
363 
364  void reverseVideo(bool value);
365 
366  void refresh();
367  void refresh(int16_t X, int16_t Y);
368  void refresh(int16_t X1, int16_t Y1, int16_t X2, int16_t Y2);
369  void beginRefresh();
370  void endRefresh();
371 
372  void setLineDoubleWidth(int16_t row, uint8_t value);
373  uint8_t getCharWidthAt(int16_t row);
374 
375  void useAlternateScreenBuffer(bool value);
376 
377  void send(char c);
378  void send(char const * str);
379 
380  Stream * m_logStream;
381 
382  // characters, characters attributes and characters colors container
383  // you may also call this the "text screen buffer"
384  GlyphsBuffer_t m_glyphsBuffer;
385  uint32_t * m_alternateMap; // used to implement alternate screen buffer
386  bool m_alternateScreenBuffer; // true when m_alternateMap and m_glyphBuffer.map has been swapped
387  volatile int16_t m_alternateCursorX; // just to restore cursor X pos when swapping screens
388  volatile int16_t m_alternateCursorY; // just to restore cursor Y pos when swapping screens
389 
390 
391  FontInfo m_font;
392  GlyphOptions_t m_glyphOptions;
393  uint8_t m_characterSetIndex; // Index of m_CharacterSet[], 0 = G0 (Standard) 1 = G1 (Alternate), 2 = G2, 3 = G3
394  uint8_t m_characterSet[4]; // 0 = DEC Special Character and Line Drawing 1 = United States (USASCII)
395 
396  // foreground (pen) and background (brush) colors
397  Color m_foregroundColor;
398  Color m_defaultForegroundColor;
399  Color m_backgroundColor;
400  Color m_defaultBackgroundColor;
401 
402  PaintOptions_t m_paintOptions;
403 
404  // states of cursor and blinking text before consumeInputQueue()
405  bool m_prevCursorEnabled;
406  bool m_prevBlinkingTextEnabled;
407 
408  // task that reads and processes incoming characters
409  TaskHandle_t m_charsConsumerTaskHandle;
410 
411  // task that reads keyboard input and send ANSI/VT100 codes to the output stream
412  // this task lives only when output stream has been specified in begin()
413  TaskHandle_t m_keyboardReaderTaskHandle;
414 
415  // cursor info (top-left position = 1, 1)
416  volatile int16_t m_cursorX;
417  volatile int16_t m_cursorY;
418  bool m_cursorPastLastCol;
419  bool m_originMode;
420  bool m_wraparound;
421  int16_t m_scrollingRegionTop;
422  int16_t m_scrollingRegionDown;
423  volatile bool m_cursorState; // true = cursor in reverse state (visible), false = cursor invisible
424  volatile bool m_cursorEnabled;
425  volatile bool m_cursorBlinkingEnabled; // true = blinking cursor, false = steady cursor
426  volatile uint8_t m_cursorStyle; // 0,1,2 = block 3,4 = underline 5,6 = bar
427 
428  // timer used to blink
429  TimerHandle_t m_blinkTimer;
430  volatile SemaphoreHandle_t m_blinkTimerMutex;
431 
432  volatile bool m_blinkingTextVisible; // true = blinking text is currently visible
433  volatile bool m_blinkingTextEnabled;
434 
435  // current cursor position
436  volatile int16_t m_columns;
437  volatile int16_t m_rows;
438 
439  // column 1 at m_tabStop[0], column 2 at m_tabStop[1], etc... 0=no tab stop, 1 = tab stop
440  uint8_t * m_tabStop;
441 
442  HardwareSerial * m_serialPort;
443 
444  // contains character that must be processed (from write(), or from input stream)
445  QueueHandle_t m_inputQueue;
446 
447  // linked list that contains saved cursor states (first item is the last added)
448  TerminalCursorState_t * m_savedCursorStateList;
449 
450  // a reset has been requested
451  bool m_resetRequested;
452 
453  // IRM (Insert Mode)
454  bool m_insertMode;
455 
456  // NLM (Automatic CR LF)
457  bool m_newLineMode;
458 
459  // DECSCLM (Smooth scroll)
460  // Smooth scroll is effective only when vertical sync refresh is enabled,
461  // hence must be VGAController.enableBackgroundPrimitiveExecution(true),
462  // that is the default.
463  bool m_smoothScroll;
464 
465  // DECKPAM (Keypad Application Mode)
466  // DECKPNM (Keypad Numeric Mode)
467  KeypadMode_t m_keypadMode;
468 
469  // DECCKM (Cursor Keys Mode)
470  bool m_cursorKeysMode;
471 
472  bool m_autoXONOFF;
473  bool m_XOFF; // true = XOFF sent
474 
475  uint8_t m_conformanceLevel; // DESSCL (1 = VT100 ... 5 = VT500)
476  uint8_t m_ctrlBits; // two values allowed: 7 and 8
477 
478  bool m_keyAutorepeat;
479  VirtualKey_t m_lastPressedKey; // used to implement m_keyAutorepeat
480 
481 };
482 
483 
484 } // end of namespace
485 
486 
487 
488 extern ESP32VGA::VGATerminalClass VGATerminal;
489 
490 
491 
492 #endif
Color
This enum defines named colors.
Definition: VGAController.h:184
This file contains VGACanvasClass definition and the VGACanvas instance.
This file contains VGAKeyboardClass definition and the VGAKeyboard instance.
This file contains ESP32VGA library configuration settings, like number of supported colors...
Definition: VGACanvas.cpp:29
Specifies general paint options.
Definition: VGAController.h:329
An ANSI-VT100 compatible display terminal.
Definition: VGATerminal.h:238
Specifies various glyph painting options.
Definition: VGAController.h:301
void end()
Definition: VGATerminal.cpp:192
VirtualKey_t
Represents each possible real or derived (SHIFT + real) key.
Definition: VGAKeyboard.h:98
void begin()
Initializes the terminal.
Definition: VGATerminal.cpp:65