Skip to content

Commit 7342eb2

Browse files
committed
0.3.0 printHelpers
1 parent 4a9c5b4 commit 7342eb2

File tree

9 files changed

+294
-17
lines changed

9 files changed

+294
-17
lines changed

libraries/printHelpers/CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.0] - 2022-11-29
10+
- add hex(value, digits) + bin(value, digits) 32 and 64 bit
11+
- leading zero's - no separators - no prefix.
12+
- add example show the difference
13+
- update readme.md
14+
- update unit test
15+
16+
917
## [0.2.5] - 2022-11-22
1018
- add changelog.md
1119
- add RP2040 to build-CI
1220

13-
1421
## [0.2.4] - 2022-04-15
1522
- no info
1623

1724
## [0.2.3] - 2021-24-12
1825

19-
2026
## [0.2.2] - 2021-11-13
2127

2228
----

libraries/printHelpers/README.md

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@ Arduino library to help formatting data for printing.
1616
The printHelpers library contains a number of functions that help to print
1717
data in a way not possible in the standard print library of the Arduino.
1818

19+
20+
#### thread safety
21+
22+
Note the functions of this library all share an internal buffer, so the
23+
library is not thread safe. Therefore one should copy / print the data
24+
(returned pointer) as fast as possible.
25+
26+
Thread-safe versions of these print functions might be made in the future.
27+
28+
29+
## Interface
30+
1931
The following functions are implemented:
2032

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

33-
----
47+
#### sci() eng()
3448

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

62-
----
76+
#### toBytes()
6377

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

95+
#### hex() bin()
96+
97+
The default print() function of Arduino does not have leading zero's
98+
for HEX and BIN. This often causes a "broken" layout especially if one
99+
wants to print in columns or so.
100+
101+
To solve this the following functions are added that will generate a
102+
constant length char array.
103+
104+
- **char \* hex(uint64_t value, uint8_t digits = 16)**
105+
- **char \* hex(uint32_t value, uint8_t digits = 8)**
106+
- **char \* hex(uint16_t value, uint8_t digits = 4)**
107+
- **char \* hex(uint8_t value, uint8_t digits = 2)**
108+
- **char \* bin(uint64_t value, uint8_t digits = 64)**
109+
- **char \* bin(uint32_t value, uint8_t digits = 32)**
110+
- **char \* bin(uint16_t value, uint8_t digits = 16)**
111+
- **char \* bin(uint8_t value, uint8_t digits = 8)**
112+
113+
Note: Data types not supported, must be cast to an supported type.
114+
115+
Note: There is overlap between **hex(value)** and **print64(value, HEX)**.
116+
The latter does not produce the leading zero's or fixed length output.
117+
81118
----
82119

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

120157
## Future
121158

159+
#### must
160+
- check TODO's in the code
161+
162+
#### should
163+
- Add distant print helpers.
164+
- feet(float cm) as 3'2" or 3-7/8 feet
165+
- inch(float cm) as 3'2" or 3-7/8 feet
166+
- yards(), miles()
167+
168+
#### could
122169
- Investigate the precision of **sci()** and **eng()**.
123170
- Investigate performance of **sci()** and **eng()**.
124171
- Investigate performance (local variables instead of modifying parameters)
125-
- Add option to pass char buffer as parameter (improve threadsafe)
126-
- Add more print helpers.
127-
- improve readability of the code (even more)
128-
- check TODO's in the code
129-
-
172+
- Investigate thread safe version
173+
- pass char buffer as parameter (breaking)
174+
- improve readability of the code
175+
- investigate separators in bin() and hex()
176+
- investigate sci() version based upon use of log()
177+
- performance
178+
- accuracy
179+
180+
#### wont
181+
- add **float()** as Arduino limits floats to "MAXLONG" by code.
182+
- use dtostrf() - is that portable
183+
- use sci() or eng()
184+
- add base(value, digits, base) for any base > 1.
185+
- only upon request.
186+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// FILE: print_hex_bin.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo hex(value, sep);
5+
6+
7+
#include "printHelpers.h"
8+
9+
volatile uint32_t n = 0;
10+
11+
void setup()
12+
{
13+
Serial.begin(115200);
14+
Serial.println();
15+
Serial.println(__FILE__);
16+
17+
Serial.println();
18+
uint8_t a = 111;
19+
int8_t b = 112;
20+
uint16_t c = 113;
21+
int16_t d = 114;
22+
uint32_t e = 113;
23+
int32_t f = 114;
24+
Serial.print(hex(a));
25+
Serial.print("\t");
26+
Serial.println(bin(a));
27+
28+
Serial.print(hex((uint8_t)b));
29+
Serial.print("\t");
30+
Serial.println(bin((uint8_t)b));
31+
32+
Serial.print(hex(c));
33+
Serial.print("\t");
34+
Serial.println(bin(c));
35+
36+
Serial.print(hex((uint16_t)d));
37+
Serial.print("\t");
38+
Serial.println(bin((uint16_t)d));
39+
40+
Serial.print(hex(e));
41+
Serial.print("\t");
42+
Serial.println(bin(e));
43+
44+
Serial.print(hex((uint32_t)f));
45+
Serial.print("\t");
46+
Serial.println(bin((uint32_t)f));
47+
48+
Serial.println(hex((uint64_t)a));
49+
Serial.println(bin((uint64_t)a));
50+
51+
52+
Serial.println();
53+
Serial.println("10 random() HEX values");
54+
for (uint8_t i = 0; i < 10; i++)
55+
{
56+
n = 2 * random(2000000000) + random(2); // 0 .. 2^32-1
57+
Serial.print(n);
58+
Serial.print('\t');
59+
Serial.print(hex(n));
60+
Serial.print('\t');
61+
Serial.println(n, HEX);
62+
}
63+
Serial.println();
64+
65+
66+
67+
68+
69+
Serial.println("10 random() BIN values");
70+
for (uint8_t i = 0; i < 10; i++)
71+
{
72+
n = 2 * random(2000000000) + random(2); // 0 .. 2^32-1
73+
Serial.print(n);
74+
Serial.print('\t');
75+
Serial.print(bin(n));
76+
Serial.print('\t');
77+
Serial.println(n, BIN);
78+
}
79+
Serial.println();
80+
81+
Serial.println("\ndone...");
82+
}
83+
84+
85+
void loop()
86+
{
87+
}
88+
89+
90+
// -- END OF FILE --

libraries/printHelpers/examples/toBytes/toBytes.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// FILE: toBytes.ino
33
// AUTHOR: Rob Tillaart
44
// PURPOSE: demo toBytes(double val);
5-
// DATE: 2020-07-03
65

76

87
#include "printHelpers.h"

libraries/printHelpers/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/printHelpers"
1717
},
18-
"version": "0.2.5",
18+
"version": "0.3.0",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

libraries/printHelpers/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=printHelpers
2-
version=0.2.5
2+
version=0.3.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library to help formatting data for printing. 64 bit integers (base 10 and 16). Engineering and scientific notation.

libraries/printHelpers/printHelpers.cpp

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: printHelpers.cpp
33
// AUTHOR: Rob Tillaart
44
// DATE: 2018-01-21
5-
// VERSION: 0.2.5
5+
// VERSION: 0.3.0
66
// PUPROSE: Arduino library to help formatting for printing.
77
// URL: https://github.com/RobTillaart/printHelpers
88

@@ -321,6 +321,83 @@ char * toBytes(double value, uint8_t decimals)
321321
}
322322

323323

324+
////////////////////////////////////////////////////////////
325+
//
326+
// HEX
327+
//
328+
// always leading zero's - no prefix - no separator
329+
char * hex(uint64_t value, uint8_t digits)
330+
{
331+
uint64_t val = value;
332+
char * buffer = __printbuffer;
333+
buffer[digits] = '\0';
334+
while (digits > 0)
335+
{
336+
uint8_t v = val & 0x0F;
337+
val >>= 4;
338+
digits--;
339+
buffer[digits] = (v < 10) ? '0' + v : ('A' - 10) + v;
340+
}
341+
return buffer;
342+
}
343+
344+
char * hex(uint32_t value, uint8_t digits)
345+
{
346+
uint32_t val = value;
347+
char * buffer = __printbuffer;
348+
buffer[digits] = '\0';
349+
while (digits > 0)
350+
{
351+
uint8_t v = val & 0x0F;
352+
val >>= 4;
353+
digits--;
354+
buffer[digits] = (v < 10) ? '0' + v : ('A' - 10) + v;
355+
}
356+
return buffer;
357+
}
358+
359+
char * hex(uint16_t value, uint8_t digits) { return hex((uint32_t) value, digits); };
360+
char * hex(uint8_t value, uint8_t digits) { return hex((uint32_t) value, digits); };
361+
362+
363+
////////////////////////////////////////////////////////////
364+
//
365+
// BIN
366+
//
367+
// always leading zero's - no prefix - no separator
368+
369+
char * bin(uint64_t value, uint8_t digits)
370+
{
371+
uint64_t val = value;
372+
char * buffer = __printbuffer;
373+
buffer[digits] = '\0';
374+
while (digits > 0)
375+
{
376+
digits--;
377+
buffer[digits] = '0' + (val & 1);
378+
val >>= 1;
379+
}
380+
return buffer;
381+
}
382+
383+
char * bin(uint32_t value, uint8_t digits)
384+
{
385+
uint64_t val = value;
386+
char * buffer = __printbuffer;
387+
buffer[digits] = '\0';
388+
while (digits > 0)
389+
{
390+
digits--;
391+
buffer[digits] = '0' + (val & 1);
392+
val >>= 1;
393+
}
394+
return buffer;
395+
}
396+
397+
char * bin(uint16_t value, uint8_t digits) { return bin((uint32_t) value, digits); };
398+
char * bin(uint8_t value, uint8_t digits) { return bin((uint32_t) value, digits); };
399+
400+
324401
// -- END OF FILE --
325402

326403

libraries/printHelpers/printHelpers.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// FILE: printHelpers.h
44
// AUTHOR: Rob Tillaart
55
// DATE: 2018-01-21
6-
// VERSION: 0.2.5
6+
// VERSION: 0.3.0
77
// PUPROSE: Arduino library to help formatting for printing.
88
// URL: https://github.com/RobTillaart/printHelpers
99

@@ -12,7 +12,7 @@
1212
#include "stdlib.h"
1313

1414

15-
#define PRINTHELPERS_VERSION (F("0.2.5"))
15+
#define PRINTHELPERS_VERSION (F("0.3.0"))
1616

1717

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

7878

79+
////////////////////////////////////////////////////////////
80+
//
81+
// HEX BIN
82+
//
83+
// always leading zero's - no prefix - no separator
84+
// cast if needed.
85+
char * hex(uint64_t value, uint8_t digits = 16);
86+
char * hex(uint32_t value, uint8_t digits = 8);
87+
char * hex(uint16_t value, uint8_t digits = 4);
88+
char * hex(uint8_t value, uint8_t digits = 2);
89+
90+
////////////////////////////////////////////////////////////
91+
//
92+
// HEX BIN
93+
//
94+
// always leading zero's - no prefix - no separator
95+
// cast if needed.
96+
char * bin(uint64_t value, uint8_t digits = 64);
97+
char * bin(uint32_t value, uint8_t digits = 32);
98+
char * bin(uint16_t value, uint8_t digits = 16);
99+
char * bin(uint8_t value, uint8_t digits = 8);
100+
101+
79102
// -- END OF FILE --
80103

0 commit comments

Comments
 (0)