added readme.md, updated examples

This commit is contained in:
RobTillaart 2020-02-19 10:38:09 +01:00
parent 82a8a0da9d
commit 77635d6463
3 changed files with 81 additions and 12 deletions

View File

@ -6,6 +6,7 @@
//
// HISTORY:
// 0.1.0 2017-12-09 initial version
// 0.1.1 2020-02-19 refactored, simpler printSpaces()
//
#include "PrintSize.h"
@ -20,8 +21,12 @@ void setup()
Serial.println("Determine length of 10 random numbers and right ");
Serial.println("align the numbers in a table with their sum.");
Serial.println();
}
void loop()
{
uint32_t sum = 0;
for (int i = 0; i < 10; i++)
{
uint32_t rn = random(100000000);
@ -30,24 +35,16 @@ void setup()
sum += rn;
Serial.println(rn);
}
Serial.print("================ +\n");
int length = ps.println(sum);
printSpaces(15 - length);
Serial.println(sum);
Serial.println();
}
void loop()
void printSpaces(uint8_t n)
{
}
void printSpaces(int n)
{
if (n <= 0) return;
while (n)
{
Serial.print(' ');
n--;
}
while (n--) Serial.print(' ');
}

View File

@ -0,0 +1,54 @@
//
// FILE: PrintSize_printf.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo printSize printf
//
// NOTE:
// - UNO does not support printf, - ESP32 does
//
// HISTORY:
// 0.1.0 2020-02-16 initial version
//
#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();
}
void loop()
{
uint32_t sum = 0;
for (int i = 0; i < 10; i++)
{
uint32_t rn = random(100000000);
int length = ps.printf("%u\n", rn);
printSpaces(15 - length);
sum += rn;
Serial.printf("%u\n", rn);
}
Serial.print("================ +\n");
int length = ps.printf("%u\n", sum);
printSpaces(15 - length);
Serial.printf("%u\n\n", sum);
delay(1000);
}
void printSpaces(uint8_t n)
{
while (n--) Serial.print(' ');
}

View File

@ -0,0 +1,18 @@
# PrintSize
PrintSize is a minimal library to determine the length of a variable when printed.
This includes printing of floats
## Examples
Example show the alignment of 10 random numbers,
both print(), println() and if supported printf().
### notes
Can be used to calculate the needed space.
- to properly do a right alignment e.g. for numbers.
- do left alignement and overwrite previous output with spaces.
- centering of numbers
- see if output will fit into a line.