Skip to content

Commit

Permalink
0.3.0 printHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Nov 29, 2022
1 parent 4a9c5b4 commit 7342eb2
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 17 deletions.
10 changes: 8 additions & 2 deletions libraries/printHelpers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2022-11-29
- add hex(value, digits) + bin(value, digits) 32 and 64 bit
- leading zero's - no separators - no prefix.
- add example show the difference
- update readme.md
- update unit test


## [0.2.5] - 2022-11-22
- add changelog.md
- add RP2040 to build-CI


## [0.2.4] - 2022-04-15
- no info

## [0.2.3] - 2021-24-12


## [0.2.2] - 2021-11-13

----
Expand Down
71 changes: 64 additions & 7 deletions libraries/printHelpers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ Arduino library to help formatting data for printing.
The printHelpers library contains a number of functions that help to print
data in a way not possible in the standard print library of the Arduino.


#### thread safety

Note the functions of this library all share an internal buffer, so the
library is not thread safe. Therefore one should copy / print the data
(returned pointer) as fast as possible.

Thread-safe versions of these print functions might be made in the future.


## Interface

The following functions are implemented:

#### print64()

- **char \* print64(int64_t value, uint8_t base)** converts a 64 bit integer
number to a char array.
The plus sign is not printed, neither are leading zero's.
Expand All @@ -30,7 +44,7 @@ int number to a char array.
No sign is printed, neither are leading zero's.
Base 10 (DEC) and 16 (HEX) are supported and bases up to 36 can be used.

----
#### sci() eng()

- **char \* sci(double value, uint8_t decimals)** converts a float or double to a
char array.
Expand Down Expand Up @@ -59,7 +73,7 @@ The usability of other values than 1 and 3 are not known.
Personally I like the multiple of 2 as I get 2 orders of magnitude in the
mantissa.

----
#### toBytes()

- **char \* toBytes(double value, uint8_t decimals = 2)** makes from a big number
representing an amount of bytes a shorter string usable for displaying.
Expand All @@ -78,6 +92,29 @@ Should be big enough for some time.
To have some support the code uses lowercase for the next 8 levels:
treda sorta rinta quexa pepta ocha nena minga luma (1024\^21 ~~ 10\^63)

#### hex() bin()

The default print() function of Arduino does not have leading zero's
for HEX and BIN. This often causes a "broken" layout especially if one
wants to print in columns or so.

To solve this the following functions are added that will generate a
constant length char array.

- **char \* hex(uint64_t value, uint8_t digits = 16)**
- **char \* hex(uint32_t value, uint8_t digits = 8)**
- **char \* hex(uint16_t value, uint8_t digits = 4)**
- **char \* hex(uint8_t value, uint8_t digits = 2)**
- **char \* bin(uint64_t value, uint8_t digits = 64)**
- **char \* bin(uint32_t value, uint8_t digits = 32)**
- **char \* bin(uint16_t value, uint8_t digits = 16)**
- **char \* bin(uint8_t value, uint8_t digits = 8)**

Note: Data types not supported, must be cast to an supported type.

Note: There is overlap between **hex(value)** and **print64(value, HEX)**.
The latter does not produce the leading zero's or fixed length output.

----

More formatting functions might be added in the future.
Expand Down Expand Up @@ -119,11 +156,31 @@ See examples.

## Future

#### must
- check TODO's in the code

#### should
- Add distant print helpers.
- feet(float cm) as 3'2" or 3-7/8 feet
- inch(float cm) as 3'2" or 3-7/8 feet
- yards(), miles()

#### could
- Investigate the precision of **sci()** and **eng()**.
- Investigate performance of **sci()** and **eng()**.
- Investigate performance (local variables instead of modifying parameters)
- Add option to pass char buffer as parameter (improve threadsafe)
- Add more print helpers.
- improve readability of the code (even more)
- check TODO's in the code
-
- Investigate thread safe version
- pass char buffer as parameter (breaking)
- improve readability of the code
- investigate separators in bin() and hex()
- investigate sci() version based upon use of log()
- performance
- accuracy

#### wont
- add **float()** as Arduino limits floats to "MAXLONG" by code.
- use dtostrf() - is that portable
- use sci() or eng()
- add base(value, digits, base) for any base > 1.
- only upon request.

90 changes: 90 additions & 0 deletions libraries/printHelpers/examples/print_hex_bin/print_hex_bin.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// FILE: print_hex_bin.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo hex(value, sep);


#include "printHelpers.h"

volatile uint32_t n = 0;

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

Serial.println();
uint8_t a = 111;
int8_t b = 112;
uint16_t c = 113;
int16_t d = 114;
uint32_t e = 113;
int32_t f = 114;
Serial.print(hex(a));
Serial.print("\t");
Serial.println(bin(a));

Serial.print(hex((uint8_t)b));
Serial.print("\t");
Serial.println(bin((uint8_t)b));

Serial.print(hex(c));
Serial.print("\t");
Serial.println(bin(c));

Serial.print(hex((uint16_t)d));
Serial.print("\t");
Serial.println(bin((uint16_t)d));

Serial.print(hex(e));
Serial.print("\t");
Serial.println(bin(e));

Serial.print(hex((uint32_t)f));
Serial.print("\t");
Serial.println(bin((uint32_t)f));

Serial.println(hex((uint64_t)a));
Serial.println(bin((uint64_t)a));


Serial.println();
Serial.println("10 random() HEX values");
for (uint8_t i = 0; i < 10; i++)
{
n = 2 * random(2000000000) + random(2); // 0 .. 2^32-1
Serial.print(n);
Serial.print('\t');
Serial.print(hex(n));
Serial.print('\t');
Serial.println(n, HEX);
}
Serial.println();





Serial.println("10 random() BIN values");
for (uint8_t i = 0; i < 10; i++)
{
n = 2 * random(2000000000) + random(2); // 0 .. 2^32-1
Serial.print(n);
Serial.print('\t');
Serial.print(bin(n));
Serial.print('\t');
Serial.println(n, BIN);
}
Serial.println();

Serial.println("\ndone...");
}


void loop()
{
}


// -- END OF FILE --
1 change: 0 additions & 1 deletion libraries/printHelpers/examples/toBytes/toBytes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// FILE: toBytes.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo toBytes(double val);
// DATE: 2020-07-03


#include "printHelpers.h"
Expand Down
2 changes: 1 addition & 1 deletion libraries/printHelpers/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/printHelpers"
},
"version": "0.2.5",
"version": "0.3.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion libraries/printHelpers/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=printHelpers
version=0.2.5
version=0.3.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library to help formatting data for printing. 64 bit integers (base 10 and 16). Engineering and scientific notation.
Expand Down
79 changes: 78 additions & 1 deletion libraries/printHelpers/printHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: printHelpers.cpp
// AUTHOR: Rob Tillaart
// DATE: 2018-01-21
// VERSION: 0.2.5
// VERSION: 0.3.0
// PUPROSE: Arduino library to help formatting for printing.
// URL: https://github.com/RobTillaart/printHelpers

Expand Down Expand Up @@ -321,6 +321,83 @@ char * toBytes(double value, uint8_t decimals)
}


////////////////////////////////////////////////////////////
//
// HEX
//
// always leading zero's - no prefix - no separator
char * hex(uint64_t value, uint8_t digits)
{
uint64_t val = value;
char * buffer = __printbuffer;
buffer[digits] = '\0';
while (digits > 0)
{
uint8_t v = val & 0x0F;
val >>= 4;
digits--;
buffer[digits] = (v < 10) ? '0' + v : ('A' - 10) + v;
}
return buffer;
}

char * hex(uint32_t value, uint8_t digits)
{
uint32_t val = value;
char * buffer = __printbuffer;
buffer[digits] = '\0';
while (digits > 0)
{
uint8_t v = val & 0x0F;
val >>= 4;
digits--;
buffer[digits] = (v < 10) ? '0' + v : ('A' - 10) + v;
}
return buffer;
}

char * hex(uint16_t value, uint8_t digits) { return hex((uint32_t) value, digits); };
char * hex(uint8_t value, uint8_t digits) { return hex((uint32_t) value, digits); };


////////////////////////////////////////////////////////////
//
// BIN
//
// always leading zero's - no prefix - no separator

char * bin(uint64_t value, uint8_t digits)
{
uint64_t val = value;
char * buffer = __printbuffer;
buffer[digits] = '\0';
while (digits > 0)
{
digits--;
buffer[digits] = '0' + (val & 1);
val >>= 1;
}
return buffer;
}

char * bin(uint32_t value, uint8_t digits)
{
uint64_t val = value;
char * buffer = __printbuffer;
buffer[digits] = '\0';
while (digits > 0)
{
digits--;
buffer[digits] = '0' + (val & 1);
val >>= 1;
}
return buffer;
}

char * bin(uint16_t value, uint8_t digits) { return bin((uint32_t) value, digits); };
char * bin(uint8_t value, uint8_t digits) { return bin((uint32_t) value, digits); };


// -- END OF FILE --


Expand Down
27 changes: 25 additions & 2 deletions libraries/printHelpers/printHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: printHelpers.h
// AUTHOR: Rob Tillaart
// DATE: 2018-01-21
// VERSION: 0.2.5
// VERSION: 0.3.0
// PUPROSE: Arduino library to help formatting for printing.
// URL: https://github.com/RobTillaart/printHelpers

Expand All @@ -12,7 +12,7 @@
#include "stdlib.h"


#define PRINTHELPERS_VERSION (F("0.2.5"))
#define PRINTHELPERS_VERSION (F("0.3.0"))


// global buffer used by all functions so no static buffer in every function
Expand Down Expand Up @@ -76,5 +76,28 @@ void sci(Stream &str, double value, uint8_t decimals);
char * toBytes(double value, uint8_t decimals = 2);


////////////////////////////////////////////////////////////
//
// HEX BIN
//
// always leading zero's - no prefix - no separator
// cast if needed.
char * hex(uint64_t value, uint8_t digits = 16);
char * hex(uint32_t value, uint8_t digits = 8);
char * hex(uint16_t value, uint8_t digits = 4);
char * hex(uint8_t value, uint8_t digits = 2);

////////////////////////////////////////////////////////////
//
// HEX BIN
//
// always leading zero's - no prefix - no separator
// cast if needed.
char * bin(uint64_t value, uint8_t digits = 64);
char * bin(uint32_t value, uint8_t digits = 32);
char * bin(uint16_t value, uint8_t digits = 16);
char * bin(uint8_t value, uint8_t digits = 8);


// -- END OF FILE --

Loading

0 comments on commit 7342eb2

Please sign in to comment.