PrintSize, a library to help with right alignment of printing

Today I wrote one of my smallest libs ever, but still useful.

The Class calculates the length of something that needs to be printed. Imagine a float or int with an unknown number of digits. Can be used e.g. for right alignment either on serial or any other stream or to position it nicely on a display. An example sketch shows how right alignment can be done.

The Class is called PrintSize, derives from Print and is experimental as it is not tested extensively. Also I want to investigate other applications. I will publish the class on github asap.

//
//    FILE: PrintSize1.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo printSize
//
// HISTORY:

#include "PrintSize.h"

PrintSize ps;

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

  Serial.println("Determine length of 10 random numbers and right ");
  Serial.println("align the numbers in a table with their sum.");
  Serial.println();

  uint32_t sum = 0;
  for (int i = 0; i < 10; i++)
  {
    uint32_t rn = random(100000000);
    int length = ps.println(rn);
    printSpaces(15 - length);
    sum += rn;
    Serial.println(rn);
  }
  Serial.print("================ +\n");
  int length = ps.println(sum);
  printSpaces(15 - length);
  Serial.println(sum);
}

void loop()
{

}

void printSpaces(int n)
{
  if (n <= 0) return;
  while (n)
  {
    Serial.print(' ');
    n--;
  }
}

And the library (no there is nothing missing, this is all :slight_smile:

//
//    FILE: printSize.h
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Class that determines printSize
//    DATE: 2017-12-09
//     URL:
//
// Released to the public domain
//

#ifndef PrintSize_h
#define PrintSize_h

#include "Print.h"

#define PRINTSIZE_VERSION "0.1.0"

class PrintSize: public Print
{
public:
  PrintSize() {};

  size_t write(uint8_t c) { return 1; }
};
#endif
// -- END OF FILE --

Source on Github: Arduino/libraries/PrintSize at master · RobTillaart/Arduino · GitHub

as always comment and remarks are welcome.

Added PrintCharArray - Arduino/libraries/PrintCharArray at master · RobTillaart/Arduino · GitHub

Difference with PrintSize is that it prints to a char array (size=256 in current version) that can be accessed again and cleared. This class allows also to determine the size of what is printed like PrintSize. Another application is to buffer slow generated output and send it as fast as possible over Serial or Ethernet as numbers need not to be 'rendered' again.

A quick test that prints 25 floats to Serial:
@115200 baud the communication part speeds up around 25%. [~19 millis versus ~14]
@230400 baud the difference goes up to almost 50% [~14 millis versus ~7]
(note: these numbers are indicative at best)

The communication time gained can be useful at the receiving end, e.g. Time outs before completing a packet of data can be smaller. The sender (Arduino) needs to do extra work and uses extra RAM, so it depends on the application if this is useful.

Added PrintString - Arduino/libraries/PrintString at master · RobTillaart/Arduino · GitHub

on request, functionally somewhat equivalent to PrintCharArray.

I advise people use the PrintCharArray as it is more memory friendly for the Arduino.