Git Product home page Git Product logo

ansi's Introduction

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

ANSI

Arduino library with basic ANSI display codes for terminal applications.

Description

ANSI codes are special codes that are send to a terminal e.g. VT100 to add attributes to displayed characters. Typical examples are bold, blink or colour. ANSI codes are also known as escape codes. The set of codes is large, however not all terminal types do support all codes.

Sending these ANSI codes to a simple ASCII only terminal like the one in the Arduino IDE might result in garbage. So use with care.

Breaking change 0.2.0

The gotoXY(x, y) has changed as the X and Y coordinates were swapped.

The code has been updated to explicitly mention which is row and which is column.

  • gotoXY(uint8_t column, uint8_t row)

Related

Terminals tested

Tests are done with

  • TeraTerm 4.102 + 4.106 (VT100, VT202, VT525 mode)
  • Putty 0.71

See supportMatrix.md (limited)

Other terminal program's exist so please let me know if yours is working too. If not, please open an issue.

Interface

#include "ansi.h"

Constructors

  • ANSI(Stream * stream = &Serial) wrapper around Serial. Can be a software serial too.
  • VT100(Stream * stream = &Serial) derived class (wrapper)

Stream interface

  • int available() to check if chars are available on the stream.
  • int read() read a byte from the stream.
  • int peek() preview the byte in the stream without fetching.
  • void flush()

Stream interface also includes print(), println(), write().

Character modi

Most of these are supported (more or less) in terminal apps.

  • void normal() normal intensity.
  • void bold() bold or high intensity.
  • void low() low intensity.
  • void underline() idem.
  • void blink() idem.
  • void blinkFast() idem.
  • void reverse() idem.

Colour

  • void foreground(uint8_t fgcolor)
  • void background(uint8_t bgcolor)
  • void color(uint8_t fgcolor, uint8_t bgcolor)

Three helpers to map to the nearest colour.

Positioning

  • void clearScreen() clears screen and sets cursor to 0,0.
  • void clearLine(uint8_t clear = toEnd) toEnd = 0, toStart = 1, entireLine = 2,
  • void home() set cursor to 0, 0
  • void gotoXY(uint8_t column, uint8_t row) set cursor to position. Note X == row and Y == column. See #13.
  • void cursorUp(uint8_t x) idem.
  • void cursorDown(uint8_t x) idem.
  • void cursorForward(uint8_t x) idem.
  • void cursorBack(uint8_t x) idem.

Experimental

Look into ansi.h for experimental functions and notes.

Version 0.1.6 added a number of experimental function that need more testing. Some are working with TeraTerm, others are unclear of fail. The user can uncomment these and verify if these work with their terminal.

Experimental getScreenSize()

Added in 0.2.2 (From PR #16 refactored) use with care.

  • bool readCursorPosition(uint16_t &w, uint16_t &h, uint32_t timeout = 100) returns true if width and height are reported. Be sure to test the return value!
  • bool getScreenSize(uint16_t &w, uint16_t &h, uint32_t timeout = 100) returns true if width and height are reported. Be sure to test the return value!
  • inline uint16_t screenWidth() used after successful call of getScreenSize();
  • inline uint16_t screenHeight() used after successful call of getScreenSize();
Experimental deviceType()

The int deviceType() function needs more testing.

See - #9

Type Description
-1 unknown
1 VT52
2 VT100
3 VT220
other unknown

As always, constructive feedback is welcome.

Experimental (working TeraTerm)

These functions need more testing and might work on your favourite terminal. As always feedback is welcome.

COLUMNS

  • void set132columns()
  • void set80columns()

MOVE WINDOW

  • void moveWindowDown()
  • void moveWindowUp()

PRINTING

  • void printScreen()
  • void setPrintingMode(bool on)

RESET

  • void reset() terminal to initial state
Experimental (NOT working TeraTerm)
  • void setSmoothScroll()
  • void setJumpScroll()
  • void printLine()
  • void invisible() to be used for password?
  • void strikeThrough()
  • void setRGBforeground(uint8_t r, uint8_t g, uint8_t b)
  • void setRGBbackground(uint8_t r, uint8_t g, uint8_t b)

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).

Since 0.2.0 the (internal) print() statements are replaced by write(). Although it are small improvements these add up.

Future

Must

  • improve documentation
    • elaborate interface
    • colour info

Should

  • test experimental functions
  • test more terminal programs (Linux mac)
  • add examples
    • DOS emulator?
    • experimental section

Could

  • investigate a class hierarchy?
    • base class vs more extended class? (footprint?)
  • increase functionality
    • which codes are generic / useful ?
  • investigate performance.
    • add line buffer in write(c) to improve throughput?
    • need for flush() with line buffer?
  • move code from .h to .cpp
  • more derived classes

Wont

  • move static strings to PROGMEM? as defines? roughly ~20 bytes PROGMEM for 4 bytes RAM...

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

ansi's People

Contributors

chlordk avatar d0m1n1qu3 avatar innermatrix avatar robtillaart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ansi's Issues

X & Y reversed in library? (gotoXY function)

Good day,

I think there is an issue in the gotoXY function, namely that the x and y are reversed. This is in the library:
void ANSI::gotoXY(uint8_t x, uint8_t y) { print("\033["); print(x); print(";"); print(y); print("H"); }
But as per wikipedia:
CSI n ; m H | CUP | Cursor Position | Moves the cursor to row n, column m.
https://en.wikipedia.org/wiki/ANSI_escape_code
this is incorrect.
If I keep the library as is, I see issues in the output, if I swap the x and y in the function all is ok...

refactor getScreensize code

  • bool getScreenSize(x, y, timeout = 100)
  • bool readCursorPosition(x, y, timeout = 100); // remove the "screen struct to make interface simpler
  • remove String class from readCursorPosition()
  • update readme
  • bump version number (==> 0.2.2)
  • update keywords.txt
  • _stream iso Serial
  • get access level right
    • x and y may not be writeable
    • ==> inline access functions ansi.screenWidth() + ansi.screenHeight()
  • check 9x9 screen . Minimal buffer 7?

normal does not work

bug should be here

void ANSI::normal()
{
_stream->write("\033[0m", 3);
}

it should be

void ANSI::normal()
{
_stream->write("\033[m", 3);
}

testet with VSCode and teraterm

Consider adding code device ready check to ANSI term library.

One of the problems I've been encountering with many of the little arduino boards that are using native serial connections on the USB, is that some don't reset when a serial monitor such as putty connects. The little Seeed Xiao / Adafruit similar boards, start up the serial connect very quickly. That can result in people thinking the little boards are dead or not working.

The real issue is that they are fast, boot fast, and you just can't see the early text flying by.

There is a simple solution that can help with this -- add a "Device Status Check" to the library such that if the serial monitor supports emulation of a ANSI, VT52, VT100 - the library checks the type... blocking until a device status is returned.

The check is quite simple, it works also for the Arduino IDE serial monitor -- because it repeats the same escape sequence over and over again until there is a response... with the IDE serial monitor, you just simply hit the "SEND" button and it continues - but of course all the "escape" codes are sent as unknown character boxes.

Here is the short code for testing - added into your example for the ANSI library.

  Serial.begin(115200);
  delay (100);
  int read_len = 0;
  delay(300);
  Serial.setTimeout(500);
  do {
    Serial.write(27);
    Serial.print("[0c");
    read_len = Serial.readBytes(buffer,3);
  } while (read_len == 0);
  
  ansi.clearScreen();

The delays could possibly be remoted.

If the code is added to a routine that is called say ANSI.deviceType It could block and return simple code 0, for dumb device does not support ANSI codes, 1 = VT52, 2= VT100, 3= VT220, etc.

I have the test sequences for about a dozen different terminal types. And can work with you to provide the list.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.