0.2.3 printhelpers

This commit is contained in:
rob tillaart 2021-12-24 13:26:40 +01:00
parent ecf84c538b
commit b6aff9cca8
10 changed files with 35 additions and 40 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018-2021 Rob Tillaart
Copyright (c) 2018-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -8,7 +8,7 @@
# printHelpers
Arduino library to help formatting data for printing
Arduino library to help formatting data for printing.
## Description
@ -36,10 +36,10 @@ Base 10 (DEC) and 16 (HEX) are supported and bases up to 36 can be used.
char array.
E.g. print(sci(f, 4)) ==> results in "6.7407E+21".
The existing Arduino print library only supports printing of floats and
doubles up toabout 4E9 while the range of floats goes up to ~1E38.
doubles up to about 4E9 while the range of floats goes up to ~1E38.
The smallest float values will often be printed as 0.00 while floats
support down to about 1E-38 (subnormal even to 1E-45).
Exisiting library functions **dtostrf()** has no scientific notation
Existing library functions **dtostrf()** has no scientific notation
and **dtostre()** (AVR) is limited to 7 decimals.
- **char \* eng(double value, uint8_t decimals)** converts a float or double to a
@ -114,14 +114,14 @@ When functions are added, the recommended minimum size might increase.
## Operation
See examples
See examples.
## Future
- Investigate the precision of **sci()** and **eng()**.
- Investigate performance of **sci()** and **eng()**.
- Investigate performance (local variables iso modifying parameters)
- 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)

View File

@ -1,11 +1,8 @@
//
// FILE: print64.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo print 64 bit integers
// DATE: 2020-06-24
// (c) : MIT
//
#include "printHelpers.h"
@ -57,3 +54,4 @@ void loop()
// -- END OF FILE --

View File

@ -1,6 +1,5 @@
// FILE: print_sci.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: demo program SCI
@ -73,3 +72,4 @@ void loop()
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: sci_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: test different values with sci function

View File

@ -1,7 +1,6 @@
//
// FILE: toBytes.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo toBytes(double val);
// DATE: 2020-07-03

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/printHelpers"
},
"version": "0.2.2",
"version": "0.2.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "printHelpers.h"
}

View File

@ -1,5 +1,5 @@
name=printHelpers
version=0.2.2
version=0.2.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library to help formatting data for printing. 64 bit integers (base 10 and 16). Engineering and scientific notation.

View File

@ -3,8 +3,8 @@
// FILE: printHelpers.h
// AUTHOR: Rob Tillaart
// DATE: 2018-01-21
// VERSION: 0.2.2
// PUPROSE: Arduino library to help formatting for printing.
// VERSION: 0.2.3
// PUPROSE: Arduino library to help formatting for printing.
// URL: https://github.com/RobTillaart/printHelpers
@ -12,7 +12,7 @@
#include "stdlib.h"
#define PRINTHELPERS_VERSION (F("0.2.2"))
#define PRINTHELPERS_VERSION (F("0.2.3"))
// 24 is a pretty safe minimum
@ -29,7 +29,7 @@ char __printbuffer[PRINTBUFFERSIZE];
////////////////////////////////////////////////////////////
//
// PRINT 64 BIT
// PRINT 64 BIT
//
// print64 note
@ -141,7 +141,7 @@ char * print64(uint64_t value, uint8_t base = 10)
// SCIENTIFIC NOTATIION
//
// typical buffer size for 8 byte double is 22 bytes
// typical buffer size for 8 byte double is 22 bytes
// 15 bytes mantissa, sign dot E-xxx
// em = exponentMultiple.
char * scieng(double value, uint8_t decimals, uint8_t em)
@ -159,7 +159,7 @@ char * scieng(double value, uint8_t decimals, uint8_t em)
// Handling these costs 13 bytes RAM
// shorten them with N, I, -I ?
if (isnan(value))
if (isnan(value))
{
strcpy(buffer, "nan");
return buffer;
@ -178,7 +178,7 @@ char * scieng(double value, uint8_t decimals, uint8_t em)
value = -value;
}
// Scale exponent to multiple of em
// Scale exponent to multiple of em
// TODO: can we remove loop to reduce rounding errors
while (value >= e1)
{
@ -220,7 +220,7 @@ char * scieng(double value, uint8_t decimals, uint8_t em)
buffer[pos++] = '.'; // decimal point
}
// Extract decimals from the remainder one at a time
// Extract decimals from the remainder one at a time
// to prevent missing leading zero's
// TODO: can we remove loop to reduce rounding errors
while (decimals-- > 0)
@ -262,18 +262,19 @@ void sci(Stream &str, double value, uint8_t decimals)
str.print(sci(value, decimals));
}
////////////////////////////////////////////////////////////
//
// toBytes
// toBytes
//
// official support to UDA == 1024^12
// kilo mega giga tera peta exa (1024^6)
// zetta yotta xona weka vunda uda (1024^12)
//
// (treda Byte == TDB is the next one and it is 2 char
// (treda Byte == TDB is the next one and it is 2 char
// so code wise difficult and as it is seldom used, support stops there.
//
//
// 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)
char * toBytes(double value, uint8_t decimals = 2)
@ -281,7 +282,7 @@ char * toBytes(double value, uint8_t decimals = 2)
static char buffer[12];
char t[] = " KMGTPEZYXWVUtsrqponml";
uint8_t i = 0; // i is index of the array == powers of 1024.
if (isinf(value))
if (isinf(value))
{
strcpy(buffer, "<inf>");
return buffer;
@ -298,7 +299,7 @@ char * toBytes(double value, uint8_t decimals = 2)
// WHOLE PART iv
int integerPart = value;
itoa(integerPart, &buffer[0], 10);
// DECIMALS
value -= integerPart;
uint8_t pos = strlen(buffer);
@ -312,7 +313,7 @@ char * toBytes(double value, uint8_t decimals = 2)
value -= int(value);
}
}
// UNITS
if (i <= strlen(t))
{
@ -330,3 +331,4 @@ char * toBytes(double value, uint8_t decimals = 2)
// -- END OF FILE --

View File

@ -38,6 +38,7 @@
unittest_setup()
{
fprintf(stderr, "PRINTHELPERS_VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
}
@ -46,11 +47,14 @@ unittest_teardown()
}
unittest(test_constants)
{
assertEqual(PRINTBUFFERSIZE, 66);
}
unittest(test_sci)
{
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
fprintf(stderr, "PRINTBUFFERSIZE: %d\n", PRINTBUFFERSIZE);
fprintf(stderr, "%s\n", sci(PI * 1000, 6));
fprintf(stderr, "%s\n", sci(PI * 100, 6));
fprintf(stderr, "%s\n", sci(PI * 10, 6));
@ -66,9 +70,6 @@ unittest(test_sci)
unittest(test_eng)
{
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
fprintf(stderr, "PRINTBUFFERSIZE: %d\n", PRINTBUFFERSIZE);
int32_t value32 = 1UL << 25;
fprintf(stderr, "VALUE32 = %ld\n", value32);
@ -87,9 +88,6 @@ unittest(test_eng)
unittest(test_print64)
{
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
fprintf(stderr, "PRINTBUFFERSIZE: %d\n", PRINTBUFFERSIZE);
int64_t value64 = 1ULL << 35;
fprintf(stderr, "%ld\n", value64);
@ -103,8 +101,6 @@ unittest(test_print64)
unittest(test_toBytes)
{
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
for (int i = 0; i < 30; i++)
{
uint32_t t = random(pow(2, i));