|
1 | 1 | //
|
2 | 2 | // FILE: ansi.cpp
|
3 | 3 | // AUTHOR: Rob Tillaart
|
4 |
| -// VERSION: 0.1.5 |
| 4 | +// VERSION: 0.1.6 |
5 | 5 | // PURPOSE: Arduino library to send ANSI escape sequences
|
6 | 6 | // DATE: 2020-04-28
|
7 | 7 | // URL: https://github.com/RobTillaart/ANSI
|
8 | 8 | //
|
9 |
| -// 0.1.0 2020-04-28 initial version |
10 |
| -// 0.1.1 2020-05-27 update library.json |
11 |
| -// 0.1.2 2020-07-08 added clearLine + color support (thanks airbornemint) |
12 |
| -// 0.1.3 2020-12-11 added Arduino-CI + unit test (minimal) |
13 |
| -// 0.1.4 2020-10-18 updated Arduino-CI (esp32) + examples |
14 |
| -// 0.1.5 2020-12-13 update library.json, license, minor edits |
15 |
| -// add write(array, length); |
| 9 | +// HISTORY: See CHANGELOG.md |
16 | 10 |
|
17 | 11 |
|
18 | 12 | #include "ansi.h"
|
@@ -70,42 +64,6 @@ enum {
|
70 | 64 | };
|
71 | 65 |
|
72 | 66 |
|
73 |
| -void ANSI::color4_code(uint8_t base, uint8_t color) { |
74 |
| - if (color < 8) { |
75 |
| - print(base + color); |
76 |
| - } else { |
77 |
| - print(base + bright_color + color); |
78 |
| - } |
79 |
| -} |
80 |
| - |
81 |
| - |
82 |
| -void ANSI::color4(uint8_t base, uint8_t color) { |
83 |
| - print("\033["); |
84 |
| - this->color4_code(base, color); |
85 |
| - print("m"); |
86 |
| -} |
87 |
| - |
88 |
| - |
89 |
| -void ANSI::colors4(uint8_t fgcolor, uint8_t bgcolor) { |
90 |
| - print("\033["); |
91 |
| - this->color4_code(fg_normal, fgcolor); |
92 |
| - print(";"); |
93 |
| - this->color4_code(bg_normal, bgcolor); |
94 |
| - print("m"); |
95 |
| -} |
96 |
| - |
97 |
| - |
98 |
| -void ANSI::color8(uint8_t base, uint8_t color) { |
99 |
| - print("\033["); |
100 |
| - print(base + extended_color); |
101 |
| - print(";"); |
102 |
| - print(extended_color8); |
103 |
| - print(";"); |
104 |
| - print(color); |
105 |
| - print("m"); |
106 |
| -} |
107 |
| - |
108 |
| - |
109 | 67 | void ANSI::foreground(uint8_t fgcolor)
|
110 | 68 | {
|
111 | 69 | if (fgcolor < 16) {
|
@@ -187,17 +145,81 @@ void ANSI::cursorBack(uint8_t x)
|
187 | 145 | }
|
188 | 146 |
|
189 | 147 |
|
| 148 | +int ANSI::deviceType(uint32_t timeout) |
| 149 | +{ |
| 150 | + int type = -1; // -1 = unknown |
| 151 | + print("\033[0c"); |
| 152 | + |
| 153 | + uint32_t start = millis(); |
| 154 | + int read_len = 0; |
| 155 | + char buffer[8]; |
| 156 | + while ((read_len != 3) && ((millis() - start) < timeout)) |
| 157 | + { |
| 158 | + delay(1); |
| 159 | + read_len = Serial.readBytes(buffer, 3); |
| 160 | + if ((buffer[0] == '1') && (buffer[1] == ';')) |
| 161 | + { |
| 162 | + type = buffer[2] - '0'; |
| 163 | + } |
| 164 | + // Serial.write(buffer, 3); |
| 165 | + // Serial.println(); |
| 166 | + } |
| 167 | + return type; |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +////////////////////////////////////////////////// |
| 172 | +// |
| 173 | +// PRIVATE |
| 174 | +// |
190 | 175 | size_t ANSI::write(uint8_t c)
|
191 | 176 | {
|
192 | 177 | // TODO add line buffer? - interference with write(array, length) !?
|
193 | 178 | return _stream->write(c);
|
194 | 179 | }
|
195 | 180 |
|
| 181 | + |
196 | 182 | size_t ANSI::write(uint8_t * array, uint8_t length)
|
197 | 183 | {
|
198 | 184 | return _stream->write(array, length);
|
199 | 185 | }
|
200 | 186 |
|
201 | 187 |
|
| 188 | + |
| 189 | +void ANSI::color4_code(uint8_t base, uint8_t color) { |
| 190 | + if (color < 8) { |
| 191 | + print(base + color); |
| 192 | + } else { |
| 193 | + print(base + bright_color + color); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +void ANSI::color4(uint8_t base, uint8_t color) { |
| 199 | + print("\033["); |
| 200 | + this->color4_code(base, color); |
| 201 | + print("m"); |
| 202 | +} |
| 203 | + |
| 204 | + |
| 205 | +void ANSI::colors4(uint8_t fgcolor, uint8_t bgcolor) { |
| 206 | + print("\033["); |
| 207 | + this->color4_code(fg_normal, fgcolor); |
| 208 | + print(";"); |
| 209 | + this->color4_code(bg_normal, bgcolor); |
| 210 | + print("m"); |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +void ANSI::color8(uint8_t base, uint8_t color) { |
| 215 | + print("\033["); |
| 216 | + print(base + extended_color); |
| 217 | + print(";"); |
| 218 | + print(extended_color8); |
| 219 | + print(";"); |
| 220 | + print(color); |
| 221 | + print("m"); |
| 222 | +} |
| 223 | + |
202 | 224 | // -- END OF FILE --
|
203 | 225 |
|
0 commit comments