Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update library.json, performance print(), license #8

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2021 Rob Tillaart
Copyright (c) 2020-2022 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ Tests are done with
Other terminal program's exist so please let me know if yours is working too.
If not, please open an issue.

# Operation

## Operation

See examples


## Performance

Since 0.1.5 there is some focus on performance.
Using **ansi.print()** and **ansi.println()** for printing text and numbers is
improved a bit since 0.1.4 by adding the private **write(array, length)**.


## Future

- test more terminal programs (Linux mac)
- write more examples
- DOS emulator?
- increase functionality
- which codes are useful ?
- investigate performance. (first step made in 0.1.5 but more possible)
- add line buffer in write(c) to improve throughput?
- need for flush() with line buffer?
- rewrite functions, replace print() by **\_stream->write()** calls? (effect on size?)

42 changes: 35 additions & 7 deletions ansi.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: ansi.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.1.5
// PURPOSE: Arduino library to send ANSI escape sequences
// DATE: 2020-04-28
// URL: https://github.com/RobTillaart/ANSI
Expand All @@ -11,6 +11,8 @@
// 0.1.2 2020-07-08 added clearLine + color support (thanks airbornemint)
// 0.1.3 2020-12-11 added Arduino-CI + unit test (minimal)
// 0.1.4 2020-10-18 updated Arduino-CI (esp32) + examples
// 0.1.5 2020-12-13 update library.json, license, minor edits
// add write(array, length);


#include "ansi.h"
Expand All @@ -21,11 +23,13 @@ ANSI::ANSI(Stream * stream)
_stream = stream;
}


int ANSI::available()
{
return _stream->available();
}


int ANSI::read()
{
return _stream->read();
Expand All @@ -36,18 +40,22 @@ int ANSI::peek()
return _stream->peek();
}


void ANSI::clearScreen()
{
print("\033[2J");
home();
}


void ANSI::clearLine(uint8_t clear)
{
print("\033[");
print(clear);
print("K");
}


// ANSI has three different color spaces: 4-bit color, 8-bit color, and 24-bit color
// The 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
Expand All @@ -61,6 +69,7 @@ enum {
extended_color24 = 2,
};


void ANSI::color4_code(uint8_t base, uint8_t color) {
if (color < 8) {
print(base + color);
Expand All @@ -69,12 +78,14 @@ void ANSI::color4_code(uint8_t base, uint8_t 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);
Expand All @@ -83,6 +94,7 @@ void ANSI::colors4(uint8_t fgcolor, uint8_t bgcolor) {
print("m");
}


void ANSI::color8(uint8_t base, uint8_t color) {
print("\033[");
print(base + extended_color);
Expand All @@ -93,6 +105,7 @@ void ANSI::color8(uint8_t base, uint8_t color) {
print("m");
}


void ANSI::foreground(uint8_t fgcolor)
{
if (fgcolor < 16) {
Expand All @@ -102,6 +115,7 @@ void ANSI::foreground(uint8_t fgcolor)
}
}


void ANSI::background(uint8_t bgcolor)
{
if (bgcolor < 16) {
Expand All @@ -111,6 +125,7 @@ void ANSI::background(uint8_t bgcolor)
}
}


void ANSI::color(uint8_t fgcolor, uint8_t bgcolor)
{
if (fgcolor < 16 && bgcolor < 16) {
Expand All @@ -121,13 +136,15 @@ void ANSI::color(uint8_t fgcolor, uint8_t bgcolor)
}
}


uint8_t ANSI::rgb2color(uint8_t r, uint8_t g, uint8_t b) {
return 16 +
36 * (uint16_t(r) * 6 / 256) +
6 * (uint16_t(g) * 6 / 256) +
(uint16_t(b) * 6 / 256);
6 * (uint16_t(g) * 6 / 256) +
(uint16_t(b) * 6 / 256);
}


void ANSI::gotoXY(uint8_t x, uint8_t y)
{
print("\033[");
Expand All @@ -137,39 +154,50 @@ void ANSI::gotoXY(uint8_t x, uint8_t y)
print("H");
}


void ANSI::cursorUp(uint8_t x)
{
print("\033[");
print(x);
print("A");
}


void ANSI::cursorDown(uint8_t x)
{
print("\033[");
print(x);
print("B");
}


void ANSI::cursorForward(uint8_t x)
{
print("\033[");
print(x);
print("C");
}


void ANSI::cursorBack(uint8_t x)
{
print("\033[");
print(x);
print("D");
}


size_t ANSI::write(uint8_t c)
{
// todo add line buffer
_stream->write(c);
return 1;
// TODO add line buffer? - interference with write(array, length) !?
return _stream->write(c);
}


size_t ANSI::write(uint8_t * array, uint8_t length)
{
return _stream->write(array, length);
}


// -- END OF FILE --

5 changes: 3 additions & 2 deletions ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: ansi.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.1.5
// PURPOSE: Arduino library to send ANSI escape sequences
// DATE: 2020-04-28
// URL: https://github.com/RobTillaart/ANSI
Expand All @@ -11,7 +11,7 @@

#include "Arduino.h"

#define ANSI_LIB_VERSION (F("0.1.4"))
#define ANSI_LIB_VERSION (F("0.1.5"))


class ANSI : public Stream
Expand Down Expand Up @@ -91,6 +91,7 @@ class ANSI : public Stream

private:
size_t write(uint8_t c);
size_t write(uint8_t * array, uint8_t length);
void color4(uint8_t base, uint8_t color);
void color4_code(uint8_t base, uint8_t color);
void colors4(uint8_t fgcolor, uint8_t bgcolor);
Expand Down
1 change: 1 addition & 0 deletions examples/ansiDemo01/ansiDemo01.ino
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ void loop()
{
}


// -- END OF FILE --
4 changes: 4 additions & 0 deletions examples/ansiDemo02/ansiDemo02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ANSI ansi(&Serial);
int t;
double d;


void setup()
{
Serial.begin(115200);
Expand All @@ -30,6 +31,7 @@ void setup()
ansi.clearScreen();
}


void loop()
{
// DISPLAY TEMPERATURE (dummy)
Expand Down Expand Up @@ -92,4 +94,6 @@ void loop()
delay(1000);
}


// -- END OF FILE --

1 change: 1 addition & 0 deletions examples/ansi_IO/ansi_IO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ANSI ansi(&Serial);
String user = "";
String password = "";


void setup()
{
Serial.begin(115200);
Expand Down
2 changes: 2 additions & 0 deletions examples/ansi_char_test/ansi_char_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ void loop()
{
}


// -- END OF FILE --

3 changes: 3 additions & 0 deletions examples/ansi_clearline/ansi_clearline.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ void setup()
delay(1000);
}


void loop()
{
}


// -- END OF FILE --

64 changes: 64 additions & 0 deletions examples/ansi_performance/ansi_performance.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// FILE: ansi_performance.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: start of performance print statement.
// DATE: 2021-12-13
// URL: https://github.com/RobTillaart/ANSI
// (c) : MIT
//


#include "ansi.h"

ANSI ansi(&Serial);

int t;
double d;

uint32_t start, stop;

void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.println(ANSI_LIB_VERSION);
Serial.println();
delay(100);

start = micros();
ansi.println("PERFORMANCE TEST ANSI TERMINAL PRINT");
stop = micros();
Serial.print("ANSITERM:\t");
Serial.println(stop - start);
delay(100);

start = micros();
Serial.println("PERFORMANCE TEST ANSI TERMINAL PRINT");
stop = micros();
Serial.print("SERIAL:\t\t");
Serial.println(stop - start);
Serial.println();
delay(100);

start = micros();
ansi.clearScreen();
stop = micros();
Serial.print("ANSITERM:\t");
Serial.println(stop - start);
delay(100);

start = micros();
Serial.print("\033[2J\033[H");
stop = micros();
Serial.print("SERIAL:\t\t");
Serial.println(stop - start);
delay(100);

}

void loop()
{
}

// -- END OF FILE --
7 changes: 7 additions & 0 deletions examples/ansi_performance/performance_0.1.4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ansi_performance.ino
0.1.4

PERFORMANCE TEST ANSI TERMINAL PRINT
ANSITERM: 376
PERFORMANCE TEST ANSI TERMINAL PRINT
SERIAL: 300
11 changes: 11 additions & 0 deletions examples/ansi_performance/performance_0.1.5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ansi_performance.ino
0.1.5

PERFORMANCE TEST ANSI TERMINAL PRINT
ANSITERM: 352
PERFORMANCE TEST ANSI TERMINAL PRINT
SERIAL: 300

(cleaned output of clearScreen(); test)
ANSITERM: 72
SERIAL: 56
Loading