2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: ansi.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-31 14:16:44 -05:00
|
|
|
// VERSION: 0.1.8
|
2021-01-29 06:31:58 -05:00
|
|
|
// PURPOSE: Arduino library to send ANSI escape sequences
|
|
|
|
// DATE: 2020-04-28
|
|
|
|
// URL: https://github.com/RobTillaart/ANSI
|
2021-10-18 10:35:48 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#include "ansi.h"
|
|
|
|
|
2021-10-18 10:35:48 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
ANSI::ANSI(Stream * stream)
|
|
|
|
{
|
|
|
|
_stream = stream;
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// STREAM INTERFACE
|
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
int ANSI::available()
|
|
|
|
{
|
2021-10-18 10:35:48 -04:00
|
|
|
return _stream->available();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int ANSI::read()
|
|
|
|
{
|
2021-10-18 10:35:48 -04:00
|
|
|
return _stream->read();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int ANSI::peek()
|
|
|
|
{
|
2021-10-18 10:35:48 -04:00
|
|
|
return _stream->peek();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
void ANSI::flush()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CHAR MODES
|
|
|
|
//
|
|
|
|
void ANSI::normal()
|
|
|
|
{
|
|
|
|
_stream->write("\033[0m", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::bold()
|
|
|
|
{
|
|
|
|
_stream->write("\033[1m", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::low()
|
|
|
|
{
|
|
|
|
_stream->write("\033[2m", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::underline()
|
|
|
|
{
|
|
|
|
_stream->write("\033[4m", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::blink()
|
|
|
|
{
|
|
|
|
_stream->write("\033[5m", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::reverse()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2023-02-27 05:33:43 -05:00
|
|
|
_stream->write("\033[7m", 3);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// POSITION COMMANDS
|
|
|
|
//
|
|
|
|
void ANSI::clearScreen()
|
|
|
|
{
|
|
|
|
_stream->write("\033[2J\033[H", 7);
|
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ANSI::clearLine(uint8_t clear)
|
|
|
|
{
|
2023-02-27 05:33:43 -05:00
|
|
|
_stream->write("\033[", 2);
|
2021-01-29 06:31:58 -05:00
|
|
|
print(clear);
|
2023-02-27 05:33:43 -05:00
|
|
|
_stream->write('K');
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2023-01-31 14:16:44 -05:00
|
|
|
void ANSI::home()
|
|
|
|
{
|
2023-02-27 05:33:43 -05:00
|
|
|
_stream->write("\033[H", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// changed 0.2.0 see #13
|
|
|
|
void ANSI::gotoXY(uint8_t column, uint8_t row)
|
|
|
|
{
|
|
|
|
_stream->write("\033[", 2);
|
|
|
|
print(row);
|
|
|
|
_stream->write(';');
|
|
|
|
print(column);
|
|
|
|
_stream->write('H');
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::cursorUp(uint8_t x)
|
|
|
|
{
|
|
|
|
_stream->write("\033[", 2);
|
|
|
|
print(x);
|
|
|
|
_stream->write('A');
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::cursorDown(uint8_t x)
|
|
|
|
{
|
|
|
|
_stream->write("\033[", 2);
|
|
|
|
print(x);
|
|
|
|
_stream->write('B');
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANSI::cursorForward(uint8_t x)
|
|
|
|
{
|
|
|
|
_stream->write("\033[", 2);
|
|
|
|
print(x);
|
|
|
|
_stream->write('C');
|
|
|
|
}
|
2023-01-31 14:16:44 -05:00
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
void ANSI::cursorBack(uint8_t x)
|
|
|
|
{
|
|
|
|
_stream->write("\033[", 2);
|
|
|
|
print(x);
|
|
|
|
_stream->write('D');
|
|
|
|
}
|
2023-01-31 14:16:44 -05:00
|
|
|
|
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// COLOR COMMANDS
|
|
|
|
//
|
|
|
|
|
2022-10-29 06:44:27 -04:00
|
|
|
// ANSI has three different color spaces: 4-bit color, 8-bit color, and 24-bit color
|
|
|
|
// These are rendered with SGR 30-37,90-97/40-47,100-107, SGR 38;5/48;5, and SGR 38;2/48;2, respectively
|
|
|
|
// The 4-bit color space is the most widely compatible and the most compactly transmitted
|
2021-01-29 06:31:58 -05:00
|
|
|
enum {
|
|
|
|
fg_normal = 30,
|
|
|
|
bg_normal = 40,
|
|
|
|
bright_color = 52,
|
|
|
|
|
|
|
|
extended_color = 8,
|
|
|
|
extended_color8 = 5,
|
|
|
|
extended_color24 = 2,
|
|
|
|
};
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ANSI::foreground(uint8_t fgcolor)
|
|
|
|
{
|
|
|
|
if (fgcolor < 16) {
|
|
|
|
this->color4(fg_normal, fgcolor);
|
|
|
|
} else {
|
|
|
|
this->color8(fg_normal, fgcolor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ANSI::background(uint8_t bgcolor)
|
|
|
|
{
|
|
|
|
if (bgcolor < 16) {
|
|
|
|
this->color4(bg_normal, bgcolor);
|
|
|
|
} else {
|
|
|
|
this->color8(bg_normal, bgcolor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ANSI::color(uint8_t fgcolor, uint8_t bgcolor)
|
|
|
|
{
|
|
|
|
if (fgcolor < 16 && bgcolor < 16) {
|
|
|
|
this->colors4(fgcolor, bgcolor);
|
|
|
|
} else {
|
|
|
|
this->color8(fg_normal, fgcolor);
|
|
|
|
this->color8(bg_normal, bgcolor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t ANSI::rgb2color(uint8_t r, uint8_t g, uint8_t b) {
|
|
|
|
return 16 +
|
2023-01-31 14:16:44 -05:00
|
|
|
36 * (uint16_t(r) * 6 / 256) +
|
|
|
|
6 * (uint16_t(g) * 6 / 256) +
|
2021-12-13 10:45:59 -05:00
|
|
|
(uint16_t(b) * 6 / 256);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
|
|
|
|
|
2022-08-24 05:00:54 -04:00
|
|
|
int ANSI::deviceType(uint32_t timeout)
|
|
|
|
{
|
2022-10-29 06:44:27 -04:00
|
|
|
int type = -1; // -1 = unknown
|
2022-08-24 05:00:54 -04:00
|
|
|
print("\033[0c");
|
2023-01-31 14:16:44 -05:00
|
|
|
|
2022-08-24 05:00:54 -04:00
|
|
|
uint32_t start = millis();
|
|
|
|
int read_len = 0;
|
|
|
|
char buffer[8];
|
|
|
|
while ((read_len != 3) && ((millis() - start) < timeout))
|
|
|
|
{
|
|
|
|
delay(1);
|
|
|
|
read_len = Serial.readBytes(buffer, 3);
|
|
|
|
if ((buffer[0] == '1') && (buffer[1] == ';'))
|
|
|
|
{
|
|
|
|
type = buffer[2] - '0';
|
|
|
|
}
|
2023-01-31 14:16:44 -05:00
|
|
|
// Serial.write(buffer, 3);
|
|
|
|
// Serial.println();
|
2022-08-24 05:00:54 -04:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-27 05:33:43 -05:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// EXPERIMENTAL
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-24 05:00:54 -04:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
size_t ANSI::write(uint8_t c)
|
|
|
|
{
|
2023-01-31 14:16:44 -05:00
|
|
|
// add line buffer? - interference with write(array, length) !?
|
2021-12-13 10:45:59 -05:00
|
|
|
return _stream->write(c);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-12-13 10:45:59 -05:00
|
|
|
|
2022-08-24 05:00:54 -04:00
|
|
|
|
2021-12-13 10:45:59 -05:00
|
|
|
size_t ANSI::write(uint8_t * array, uint8_t length)
|
|
|
|
{
|
|
|
|
return _stream->write(array, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-24 05:00:54 -04:00
|
|
|
|
|
|
|
void ANSI::color4_code(uint8_t base, uint8_t color) {
|
|
|
|
if (color < 8) {
|
|
|
|
print(base + color);
|
|
|
|
} else {
|
|
|
|
print(base + bright_color + color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ANSI::color4(uint8_t base, uint8_t color) {
|
|
|
|
print("\033[");
|
|
|
|
this->color4_code(base, color);
|
|
|
|
print("m");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ANSI::colors4(uint8_t fgcolor, uint8_t bgcolor) {
|
|
|
|
print("\033[");
|
|
|
|
this->color4_code(fg_normal, fgcolor);
|
|
|
|
print(";");
|
|
|
|
this->color4_code(bg_normal, bgcolor);
|
|
|
|
print("m");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ANSI::color8(uint8_t base, uint8_t color) {
|
|
|
|
print("\033[");
|
|
|
|
print(base + extended_color);
|
|
|
|
print(";");
|
|
|
|
print(extended_color8);
|
|
|
|
print(";");
|
|
|
|
print(color);
|
|
|
|
print("m");
|
|
|
|
}
|
|
|
|
|
2022-10-29 06:44:27 -04:00
|
|
|
|
2023-01-31 14:16:44 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-13 10:45:59 -05:00
|
|
|
|