Skip to content

Commit 7d22ccd

Browse files
committed
0.1.6 ANSI
1 parent c5860b1 commit 7d22ccd

File tree

8 files changed

+222
-52
lines changed

8 files changed

+222
-52
lines changed

libraries/ANSI/CHANGELOG.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Change Log bitHelpers
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.1.6] - 2022-04-11
10+
11+
### Added
12+
- add CHANGELOG.md
13+
- add **int deviceType()**
14+
15+
### Changed
16+
- edit README.md
17+
18+
### Fixed
19+
20+
21+
## [0.1.5] - 2021-12-13
22+
23+
### Added
24+
- add write(array, length)
25+
26+
### Changed
27+
- update library.json, license, minor edits
28+
29+
30+
## [0.1.4] - 2021-10-18
31+
32+
### Changed
33+
- update build-CI (e.g. ESP32)
34+
- update examples
35+
36+
37+
## [0.1.3] - 2020-12-11
38+
39+
### Added
40+
- add Arduino-CI
41+
- add unit test (minimal)
42+
43+
44+
## [0.1.2] - 2020-07-08
45+
46+
### Added
47+
- add clearLine
48+
= add color support (thanks to airbornemint)
49+
50+
51+
## [0.1.1] - 2020-05-27
52+
53+
### Changed
54+
- update library.json
55+
56+
57+
## [0.1.0] - 2020-04-28
58+
59+
### Added
60+
- initial release
61+
62+
63+

libraries/ANSI/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ Using **ansi.print()** and **ansi.println()** for printing text and numbers is
4545
improved a bit since 0.1.4 by adding the private **write(array, length)**.
4646

4747

48+
## Experimental 0.1.6
49+
50+
Version 0.1.6 added a number of experimental function that need more testing.
51+
Some are working, others are unclear, but the user can uncomment these and
52+
experiment with them if needed.
53+
54+
Also added is the **int deviceType()** function which also need more testing
55+
56+
As always, constructive feedback is welcome.
57+
58+
4859
## Future
4960

5061
- test more terminal programs (Linux mac)

libraries/ANSI/ansi.cpp

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
//
22
// FILE: ansi.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.5
4+
// VERSION: 0.1.6
55
// PURPOSE: Arduino library to send ANSI escape sequences
66
// DATE: 2020-04-28
77
// URL: https://github.com/RobTillaart/ANSI
88
//
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
1610

1711

1812
#include "ansi.h"
@@ -70,42 +64,6 @@ enum {
7064
};
7165

7266

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-
10967
void ANSI::foreground(uint8_t fgcolor)
11068
{
11169
if (fgcolor < 16) {
@@ -187,17 +145,81 @@ void ANSI::cursorBack(uint8_t x)
187145
}
188146

189147

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+
//
190175
size_t ANSI::write(uint8_t c)
191176
{
192177
// TODO add line buffer? - interference with write(array, length) !?
193178
return _stream->write(c);
194179
}
195180

181+
196182
size_t ANSI::write(uint8_t * array, uint8_t length)
197183
{
198184
return _stream->write(array, length);
199185
}
200186

201187

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+
202224
// -- END OF FILE --
203225

libraries/ANSI/ansi.h

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: ansi.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.5
5+
// VERSION: 0.1.6
66
// PURPOSE: Arduino library to send ANSI escape sequences
77
// DATE: 2020-04-28
88
// URL: https://github.com/RobTillaart/ANSI
@@ -11,22 +11,22 @@
1111

1212
#include "Arduino.h"
1313

14-
#define ANSI_LIB_VERSION (F("0.1.5"))
14+
#define ANSI_LIB_VERSION (F("0.1.6"))
1515

1616

1717
class ANSI : public Stream
1818
{
1919
public:
2020
ANSI(Stream * stream = &Serial);
2121

22-
// Stream interface
22+
// Stream interface
2323
int available();
2424
int read();
2525
int peek();
2626
void flush() { return; }; // placeholder to keep CI happy
2727

2828

29-
// CHAR MODES
29+
// CHAR MODES
3030
void normal() { print("\033[0m"); };
3131
void bold() { print("\033[1m"); };
3232
void low() { print("\033[2m"); };
@@ -35,7 +35,7 @@ class ANSI : public Stream
3535
void reverse() { print("\033[7m"); };
3636

3737

38-
// COLOR
38+
// COLOR
3939
enum {
4040
black = 0,
4141
red,
@@ -70,7 +70,7 @@ class ANSI : public Stream
7070
uint8_t rgb2color(uint8_t r, uint8_t g, uint8_t b);
7171

7272

73-
// POSITIONING
73+
// POSITIONING
7474
enum {
7575
toEnd = 0,
7676
toStart = 1,
@@ -89,6 +89,41 @@ class ANSI : public Stream
8989
void cursorBack(uint8_t x);
9090

9191

92+
// META
93+
// deviceType is discussed
94+
// - https://github.com/RobTillaart/ANSI/issues/9
95+
// timeout in milliseconds.
96+
// note this function blocks for timeout or less.
97+
// -1 = unknown;
98+
// 1 = VT52, 2 = VT100, 3 = VT220,
99+
int deviceType(uint32_t timeout = 100);
100+
101+
102+
// EXPERIMENTAL SECTION
103+
// use at own risk
104+
// check if it works on your terminal Tera
105+
/*
106+
void set132() { print("\033[?3h"); }; // +
107+
void set80() { print("\033[?3l"); }; // +
108+
void setSmoothScroll() { print("\033[?4h"); }; // -
109+
void setJumpScroll() { print("\033[?4l"); }; // -
110+
void moveWindowDown() { print("\033M"); }; // +
111+
void moveWindowUp() { print("\033D"); }; // +
112+
// to be used for password?
113+
void invisible() { print("\033[8m"); }; // -
114+
115+
// PRINTING
116+
// use at own risk
117+
// check if it works on your terminal Tera
118+
void printScreen() { print("\033[i"); }; // +
119+
void printLine() { print("\033[1i"); }; // ?
120+
void startPrintLog() { print("\033[4i"); }; // ?
121+
void stopPrintLog() { print("\033[5i"); }; // ?
122+
123+
// Reset terminal to initial state
124+
void reset() { print("\033c"); }; // +
125+
*/
126+
92127
private:
93128
size_t write(uint8_t c);
94129
size_t write(uint8_t * array, uint8_t length);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// FILE: ansi_deviceType.ino
2+
// AUTHOR: Rob Tillaart
3+
// PURPOSE: demo
4+
// URL: https://github.com/RobTillaart/ANSI/issues/9
5+
6+
// NOT TESTED
7+
// - WITH REAL TERMINAL
8+
// - with dummies
9+
10+
#include "ansi.h"
11+
12+
13+
ANSI ansi(&Serial);
14+
int deviceType = -1;
15+
16+
void setup()
17+
{
18+
Serial.begin(115200);
19+
while (!Serial);
20+
Serial.println(__FILE__);
21+
}
22+
23+
24+
void loop()
25+
{
26+
if (deviceType == -1)
27+
{
28+
deviceType = ansi.deviceType(100);
29+
Serial.print("DEV_TYPE: ");
30+
Serial.println(deviceType);
31+
}
32+
delay(1000);
33+
}
34+
35+
36+
// -- END OF FILE --

libraries/ANSI/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ gray2color KEYWORD2
3131
grey2color KEYWORD2
3232
rgb2color KEYWORD2
3333

34+
deviceType KEYWORD2
35+
36+
3437

3538
# Constants (LITERAL1)
3639
toEnd LITERAL1

libraries/ANSI/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/ANSI.git"
1717
},
18-
"version": "0.1.5",
18+
"version": "0.1.6",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

libraries/ANSI/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ANSI
2-
version=0.1.5
2+
version=0.1.6
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library to send ANSI escape sequences

0 commit comments

Comments
 (0)