px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_vt100.h
1#ifndef __PX_VT100_H__
2#define __PX_VT100_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2012 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_vt100.h : VT100/ANSI Terminal module
16 Author(s): Pieter Conradie
17 Creation Date: 2012-09-15
18
19============================================================================= */
20
21/**
22 * @ingroup COMMS
23 * @defgroup PX_VT100 px_vt100.h : VT100/ANSI Terminal module
24 *
25 * Parses VT100 / ANSI escape sequences to interface with a terminal emulator.
26 *
27 * File(s):
28 * - comms/inc/px_vt100.h
29 * - comms/src/px_vt100.c
30 *
31 * @see
32 * - http://en.wikipedia.org/wiki/ANSI_escape_code
33 * - http://www.termsys.demon.co.uk/vtansi.htm
34 *
35 * Example:
36 *
37 * @include comms/test/px_cli_test.c
38 *
39 * @{
40 */
41
42/* _____STANDARD INCLUDES____________________________________________________ */
43
44/* _____PROJECT INCLUDES_____________________________________________________ */
45#include "px_defs.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50/* _____DEFINITIONS__________________________________________________________ */
51/// @name Special ASCII values
52/// @{
53#define PX_VT100_CHAR_BEL 0x07
54#define PX_VT100_CHAR_BS 0x08
55#define PX_VT100_CHAR_TAB 0x09
56#define PX_VT100_CHAR_CR 0x0D // '\r'
57#define PX_VT100_CHAR_LF 0x0A // '\n'
58#define PX_VT100_CHAR_ESC 0x1B
59#define PX_VT100_CHAR_DEL 0x7F
60/// @}
61
62/// @name VT100 escape sequences
63/// @{
64#define PX_VT100_CURSOR_HOME "\x1B[H"
65#define PX_VT100_CURSOR_UP "\x1B[1A"
66#define PX_VT100_CURSOR_DOWN "\x1B[1B"
67#define PX_VT100_CURSOR_FORWARD "\x1B[1C"
68#define PX_VT100_CURSOR_BACKWARD "\x1B[1D"
69#define PX_VT100_SAVE_CURSOR "\x1B[s"
70#define PX_VT100_UNSAVE_CURSOR "\x1B[u"
71#define PX_VT100_SAVE_CURSOR_AND_ATTRS "\x1B7"
72#define PX_VT100_RESTORE_CURSOR_AND_ATTRS "\x1B8"
73
74#define PX_VT100_SCROLL_SCREEN "\x1B[r"
75#define PX_VT100_SCROLL_DOWN "\x1BD"
76#define PX_VT100_SCROLL_UP "\x1BM"
77
78#define PX_VT100_SET_TAB "\x1BH"
79#define PX_VT100_CLR_TAB "\x1B[g"
80#define PX_VT100_CLR_ALL_TABS "\x1B[3g"
81
82#define PX_VT100_ERASE_END_OF_LINE "\x1B[K"
83#define PX_VT100_ERASE_START_OF_LINE "\x1B[1K"
84#define PX_VT100_ERASE_LINE "\x1B[2K"
85#define PX_VT100_ERASE_DOWN "\x1B[J"
86#define PX_VT100_ERASE_UP "\x1B[1J"
87#define PX_VT100_ERASE_SCREEN "\x1B[2J"
88
89#define PX_VT100_ATTR_RST "\x1B[0m"
90#define PX_VT100_ATTR_BRIGHT "\x1B[1m"
91#define PX_VT100_ATTR_DIM "\x1B[2m"
92#define PX_VT100_ATTR_UNDERSCORE "\x1B[4m"
93#define PX_VT100_ATTR_BLINK "\x1B[5m"
94#define PX_VT100_ATTR_REVERSE "\x1B[7m"
95#define PX_VT100_ATTR_HIDDEN "\x1B[8m"
96
97#define PX_VT100_FG_BLACK "\x1B[30m"
98#define PX_VT100_FG_RED "\x1B[31m"
99#define PX_VT100_FG_GREEN "\x1B[32m"
100#define PX_VT100_FG_YELLOW "\x1B[33m"
101#define PX_VT100_FG_BLUE "\x1B[34m"
102#define PX_VT100_FG_MAGENTA "\x1B[35m"
103#define PX_VT100_FG_CYAN "\x1B[36m"
104#define PX_VT100_FG_WHITE "\x1B[37m"
105
106#define PX_VT100_BG_BLACK "\x1B[40m"
107#define PX_VT100_BG_RED "\x1B[41m"
108#define PX_VT100_BG_GREEN "\x1B[42m"
109#define PX_VT100_BG_YELLOW "\x1B[43m"
110#define PX_VT100_BG_BLUE "\x1B[44m"
111#define PX_VT100_BG_MAGENTA "\x1B[45m"
112#define PX_VT100_BG_CYAN "\x1B[46m"
113#define PX_VT100_BG_WHITE "\x1B[47m"
114/// @}
115
116/* _____TYPE DEFINITIONS_____________________________________________________ */
117/// VT100 Terminal receive state
118typedef enum
119{
120 PX_VT100_CHAR_NORMAL, ///< A normal key has been pressed and must be used
121 PX_VT100_CHAR_INVALID, ///< An invalid key code has been sent and must be discarded
122 PX_VT100_ESC_SEQ_BUSY, ///< Busy with escape sequence; data must be discarded
123 PX_VT100_ESC_SEQ_ARROW_UP, ///< Up Arrow has been pressed
124 PX_VT100_ESC_SEQ_ARROW_DN, ///< Down Arrow has been pressed
125 PX_VT100_ESC_SEQ_ARROW_LEFT, ///< Left Arrow has been pressed
126 PX_VT100_ESC_SEQ_ARROW_RIGHT, ///< Right Arrow has been pressed
128
129/* _____GLOBAL VARIABLES_____________________________________________________ */
130
131/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
132/**
133 * Initialise module.
134 */
135extern void px_vt100_init(void);
136
137/**
138 * Process a received character byte.
139 *
140 * If an ANSI escape sequence is detected, PX_VT100_ESC_SEQ_BUSY will be returned
141 * to indicate that received character should be ignored, otherwise
142 * PX_VT100_CHAR_NORMAL is returned to indicate that the character must be used.
143 *
144 * If an ANSI escape sequence is decoded it is indicated with a
145 * PX_VT100_ESC_SEQ... state.
146 *
147 * @param data Received character to be process for ANSI Escape Sequences
148 *
149 * @return px_vt100_state_t PX_VT100_CHAR_NORMAL if the character must be used as
150 * normal; PX_VT100_CHAR_INVALID or PX_VT100_ESC_SEQ_BUSY if
151 * character should be ignored; otherwise the decoded
152 * ANSI Escape Sequence.
153 *
154 */
155extern px_vt100_state_t px_vt100_on_rx_char(char data);
156
157/**
158 * Send 'clear screen' command to terminal.
159 */
160extern void px_vt100_clr_display(void);
161
162/**
163 * Send 'erase line' command to terminal.
164 */
165extern void px_vt100_erase_line(void);
166
167/**
168 * Delete specified number of characters
169 *
170 * @param nr_of_chars Number of characters to delete
171 */
172extern void px_vt100_del_chars(uint8_t nr_of_chars);
173
174/* _____MACROS_______________________________________________________________ */
175
176#ifdef __cplusplus
177}
178#endif
179
180/// @}
181#endif
void px_vt100_clr_display(void)
Send 'clear screen' command to terminal.
Definition: px_vt100.c:111
void px_vt100_erase_line(void)
Send 'erase line' command to terminal.
Definition: px_vt100.c:119
void px_vt100_del_chars(uint8_t nr_of_chars)
Delete specified number of characters.
Definition: px_vt100.c:127
void px_vt100_init(void)
Initialise module.
Definition: px_vt100.c:43
px_vt100_state_t px_vt100_on_rx_char(char data)
Process a received character byte.
Definition: px_vt100.c:57
px_vt100_state_t
VT100 Terminal receive state.
Definition: px_vt100.h:119
@ PX_VT100_ESC_SEQ_BUSY
Busy with escape sequence; data must be discarded.
Definition: px_vt100.h:122
@ PX_VT100_CHAR_INVALID
An invalid key code has been sent and must be discarded.
Definition: px_vt100.h:121
@ PX_VT100_ESC_SEQ_ARROW_LEFT
Left Arrow has been pressed.
Definition: px_vt100.h:125
@ PX_VT100_ESC_SEQ_ARROW_UP
Up Arrow has been pressed.
Definition: px_vt100.h:123
@ PX_VT100_ESC_SEQ_ARROW_DN
Down Arrow has been pressed.
Definition: px_vt100.h:124
@ PX_VT100_CHAR_NORMAL
A normal key has been pressed and must be used.
Definition: px_vt100.h:120
@ PX_VT100_ESC_SEQ_ARROW_RIGHT
Right Arrow has been pressed.
Definition: px_vt100.h:126