mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
added readme.md, updated examples
This commit is contained in:
parent
82a8a0da9d
commit
77635d6463
@ -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(' ');
|
||||
}
|
||||
|
||||
|
@ -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(' ');
|
||||
}
|
18
libraries/PrintSize/readme.md
Normal file
18
libraries/PrintSize/readme.md
Normal 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.
|
||||
|
Loading…
Reference in New Issue
Block a user