Normally, if you use printf to print debug information to the serial port, you can only see one color of characters in the interface of the serial debug tool, and the default color of the characters is different when you use different serial debug tools.
In fact, the debugging information output on the terminal can be colorful. The different types of debugging information are distinguished by different colors, which makes it easy to quickly locate the problem in the program.
So can we use the printf function to output colored debugging information through the serial port? The answer is yes, it just requires a few tricks.
Before describing how to print colored debug messages, let's get some background knowledge - ANSI escape sequences.
When we print the information to the terminal for display, we can control the information to be displayed in bold, italic, underline, etc., and we can also control the characters to be displayed in different colors.
For example, executing the following code will display red on the terminal ---testing---:
printf("\033[31m ---testing---\n").
Why are some characters displayed as messages and others treated as commands? Actually, it all comes down to escape characters.
In C, the backslash "\" is used as an escape character. Normally, both "\" and "n" are ordinary characters, but when combined into a sequence of characters "\n", they indicate a line break.
There is a similar usage in the terminal, except that instead of the backslash "\", the escape character in the terminal is "ESC". The terminal uses "ESC" as the escape character, and the serial number of "ESC" in the ASCII table is 27, that is, 0x1b. Therefore, the sequence of characters combined with 0x1b in the terminal will be escaped into commands, and different combinations of characters represent different terminal commands, and after these escape rules are normalized, they are called ANSI Escape Sequences. Sequences), mainly used to control the cursor position, color and other options on the terminal.
ANSI escape sequence start flag
The ANSI escape sequence start flag consists of two bytes, the first byte is the terminal escape character "ESC" which is 0x1b, and the second byte has a value range of 0x40-0x5F (i.e. ASCII: @ A - Z [ \ ] ^ _), that is to say, the ANSI escape sequence start flag has the following Several types:
- x1b@ (ESC@)
- x1bA (ESCA)
- x1b- (ESC-)
- x1bZ (ESCZ)
- x1b[(ESC[)
- x1b\(ESC\)
- x1b](ESC])
- x1b^(ESC^)
- x1b_(ESC_)
With so many kinds of start flags mentioned above, the common start flag is " x1b[ ", and all the other start flags are rarely used nowadays.
Control Sequence Introducer CSI
When 0x1b is combined with "[", it is called the Control Sequence Introducer, or CSI for short, and has the following basic format:
x1b[<code><tail>
code: the specific content of the escape sequence, separated by a semicolon ";" between multiple content
tail: the end of the escape sequence, different tail means different functions of the sequence
There are many escape sequences starting with CSI, and different sequences indicate different terminal instructions, which can be roughly divided into the following categories:
- Character rendering commands (SGR)
- Cursor move instructions
- Clear screen commands
- Terminal control commands
Character Rendition Commands
The Select Graphic Rendition command, or SGR for short, is used to set the display of characters. It is mainly used to set the character display effect, and its basic format is as follows:
CSI n m
CSI: control character start flag " 0x1b[ "
n : value range 0 ~ 107
m: the end of the escape sequence flag, the letter m as the end, indicating that it is a character rendering sequence
Example:
x1b[3m
The value range of n is 0 ~ 107, and is divided into two main types of functions as follows:
- Character style control
- Character color control
Multiple SGR arguments can always be concatenated using another, and they will be applied in the order they are encountered. It’s especially common to see before some other argument, in order to reset the state before applying our own.
Colour PalettesThe basic colour palette has 8 entries:
- 0: black
- 1: red
- 2: green
- 3: yellow
- 4: blue
- 5: magenta
- 6: cyan
- 7: white
A useful way to help remember this, or at least to select colours for use, is that, with the exception of 0/black, the colours are ordered by usefulness, with highest first: red text is very useful for indicating failures, green is useful for indicating extreme success, yellow for warnings, and then blue, magenta, and cyan for progressively more obscure conditions or decoration.
0 and 7 are less useful for text because one or the other will generally look nearly-unreadable depending on whether the user has a light or a dark background.
Terminals will also have a “bright” version of this palette (activated using 90–97 / 100–107). These are the same (black/red/green/etc.) but generally noticeably brighter than their regular counterparts.
For practice, you might try to figure out how this string would display:
printf("\x1b[31m" "Hello World\n" "\x1b[0m");
printf("\x1b[32m" "Hello World\n" "\x1b[0m");
printf("\x1b[33m" "Hello World\n" "\x1b[0m");
printf("\x1b[34m" "Hello World\n" "\x1b[0m");
printf("\x1b[35m" "Hello World\n" "\x1b[0m");
printf("\x1b[36m" "Hello World\n" "\x1b[0m");
Miscellany
Another pair of useful escapes is and. These show and hide the cursor, respectively. Try not to think too hard about the syntax here: means something to do with the cursor and and stand for “high” and “low”: imagine a bit indicating whether the cursor should be visible. The “high” value (1) would indicate “show”; the “low” value (0) would indicate “hide”.
\x1b[?25h\x1b[?25l?25hl
Show/hide is useful when you’re going to draw some stuff that’ll cause the cursor to jump around like crazy, for example, repainting a couple of the last few lines to update them with new content.
One other thing that we use frequently is, or Carriage Return, which is functionally similar or identical to. It just moves the cursor to the start of the line.
\r\x1b[1G
Caution
If the serial debugging tool used does not support ANSI / VT100 escape sequence parsing, the received serial data will be partially garbled and only the default color of the tool will be used to display the information。
Comments
Please log in or sign up to comment.