mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.3 printhelpers
This commit is contained in:
parent
ecf84c538b
commit
b6aff9cca8
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
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
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
# printHelpers
|
# printHelpers
|
||||||
|
|
||||||
Arduino library to help formatting data for printing
|
Arduino library to help formatting data for printing.
|
||||||
|
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
@ -36,10 +36,10 @@ Base 10 (DEC) and 16 (HEX) are supported and bases up to 36 can be used.
|
|||||||
char array.
|
char array.
|
||||||
E.g. print(sci(f, 4)) ==> results in "6.7407E+21".
|
E.g. print(sci(f, 4)) ==> results in "6.7407E+21".
|
||||||
The existing Arduino print library only supports printing of floats and
|
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
|
The smallest float values will often be printed as 0.00 while floats
|
||||||
support down to about 1E-38 (subnormal even to 1E-45).
|
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.
|
and **dtostre()** (AVR) is limited to 7 decimals.
|
||||||
|
|
||||||
- **char \* eng(double value, uint8_t decimals)** converts a float or double to a
|
- **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
|
## Operation
|
||||||
|
|
||||||
See examples
|
See examples.
|
||||||
|
|
||||||
|
|
||||||
## Future
|
## Future
|
||||||
|
|
||||||
- Investigate the precision of **sci()** and **eng()**.
|
- Investigate the precision of **sci()** and **eng()**.
|
||||||
- Investigate performance 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 option to pass char buffer as parameter (improve threadsafe)
|
||||||
- Add more print helpers.
|
- Add more print helpers.
|
||||||
- improve readability of the code (even more)
|
- improve readability of the code (even more)
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
//
|
//
|
||||||
// FILE: print64.ino
|
// FILE: print64.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.0
|
|
||||||
// PURPOSE: demo print 64 bit integers
|
// PURPOSE: demo print 64 bit integers
|
||||||
// DATE: 2020-06-24
|
// DATE: 2020-06-24
|
||||||
// (c) : MIT
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
#include "printHelpers.h"
|
#include "printHelpers.h"
|
||||||
@ -57,3 +54,4 @@ void loop()
|
|||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
// FILE: print_sci.ino
|
// FILE: print_sci.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.0.1
|
|
||||||
// PURPOSE: demo program SCI
|
// PURPOSE: demo program SCI
|
||||||
|
|
||||||
|
|
||||||
@ -73,3 +72,4 @@ void loop()
|
|||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
//
|
//
|
||||||
// FILE: sci_test.ino
|
// FILE: sci_test.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.0
|
|
||||||
// PURPOSE: test different values with sci function
|
// PURPOSE: test different values with sci function
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
//
|
//
|
||||||
// FILE: toBytes.ino
|
// FILE: toBytes.ino
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.1.0
|
|
||||||
// PURPOSE: demo toBytes(double val);
|
// PURPOSE: demo toBytes(double val);
|
||||||
// DATE: 2020-07-03
|
// DATE: 2020-07-03
|
||||||
|
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/printHelpers"
|
"url": "https://github.com/RobTillaart/printHelpers"
|
||||||
},
|
},
|
||||||
"version": "0.2.2",
|
"version": "0.2.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*"
|
"platforms": "*",
|
||||||
|
"headers": "printHelpers.h"
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=printHelpers
|
name=printHelpers
|
||||||
version=0.2.2
|
version=0.2.3
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=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.
|
sentence=Arduino library to help formatting data for printing. 64 bit integers (base 10 and 16). Engineering and scientific notation.
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
// FILE: printHelpers.h
|
// FILE: printHelpers.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// DATE: 2018-01-21
|
// DATE: 2018-01-21
|
||||||
// VERSION: 0.2.2
|
// VERSION: 0.2.3
|
||||||
// PUPROSE: Arduino library to help formatting for printing.
|
// PUPROSE: Arduino library to help formatting for printing.
|
||||||
// URL: https://github.com/RobTillaart/printHelpers
|
// URL: https://github.com/RobTillaart/printHelpers
|
||||||
|
|
||||||
|
|
||||||
@ -12,7 +12,7 @@
|
|||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
|
|
||||||
|
|
||||||
#define PRINTHELPERS_VERSION (F("0.2.2"))
|
#define PRINTHELPERS_VERSION (F("0.2.3"))
|
||||||
|
|
||||||
// 24 is a pretty safe minimum
|
// 24 is a pretty safe minimum
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ char __printbuffer[PRINTBUFFERSIZE];
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// PRINT 64 BIT
|
// PRINT 64 BIT
|
||||||
//
|
//
|
||||||
|
|
||||||
// print64 note
|
// print64 note
|
||||||
@ -141,7 +141,7 @@ char * print64(uint64_t value, uint8_t base = 10)
|
|||||||
// SCIENTIFIC NOTATIION
|
// 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
|
// 15 bytes mantissa, sign dot E-xxx
|
||||||
// em = exponentMultiple.
|
// em = exponentMultiple.
|
||||||
char * scieng(double value, uint8_t decimals, uint8_t em)
|
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
|
// Handling these costs 13 bytes RAM
|
||||||
// shorten them with N, I, -I ?
|
// shorten them with N, I, -I ?
|
||||||
if (isnan(value))
|
if (isnan(value))
|
||||||
{
|
{
|
||||||
strcpy(buffer, "nan");
|
strcpy(buffer, "nan");
|
||||||
return buffer;
|
return buffer;
|
||||||
@ -178,7 +178,7 @@ char * scieng(double value, uint8_t decimals, uint8_t em)
|
|||||||
value = -value;
|
value = -value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale exponent to multiple of em
|
// Scale exponent to multiple of em
|
||||||
// TODO: can we remove loop to reduce rounding errors
|
// TODO: can we remove loop to reduce rounding errors
|
||||||
while (value >= e1)
|
while (value >= e1)
|
||||||
{
|
{
|
||||||
@ -220,7 +220,7 @@ char * scieng(double value, uint8_t decimals, uint8_t em)
|
|||||||
buffer[pos++] = '.'; // decimal point
|
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
|
// to prevent missing leading zero's
|
||||||
// TODO: can we remove loop to reduce rounding errors
|
// TODO: can we remove loop to reduce rounding errors
|
||||||
while (decimals-- > 0)
|
while (decimals-- > 0)
|
||||||
@ -262,18 +262,19 @@ void sci(Stream &str, double value, uint8_t decimals)
|
|||||||
str.print(sci(value, decimals));
|
str.print(sci(value, decimals));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// toBytes
|
// toBytes
|
||||||
//
|
//
|
||||||
|
|
||||||
// official support to UDA == 1024^12
|
// official support to UDA == 1024^12
|
||||||
// kilo mega giga tera peta exa (1024^6)
|
// kilo mega giga tera peta exa (1024^6)
|
||||||
// zetta yotta xona weka vunda uda (1024^12)
|
// 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.
|
// 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
|
// 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)
|
// treda sorta rinta quexa pepta ocha nena minga luma (1024 ^21 ~~ 10^63)
|
||||||
char * toBytes(double value, uint8_t decimals = 2)
|
char * toBytes(double value, uint8_t decimals = 2)
|
||||||
@ -281,7 +282,7 @@ char * toBytes(double value, uint8_t decimals = 2)
|
|||||||
static char buffer[12];
|
static char buffer[12];
|
||||||
char t[] = " KMGTPEZYXWVUtsrqponml";
|
char t[] = " KMGTPEZYXWVUtsrqponml";
|
||||||
uint8_t i = 0; // i is index of the array == powers of 1024.
|
uint8_t i = 0; // i is index of the array == powers of 1024.
|
||||||
if (isinf(value))
|
if (isinf(value))
|
||||||
{
|
{
|
||||||
strcpy(buffer, "<inf>");
|
strcpy(buffer, "<inf>");
|
||||||
return buffer;
|
return buffer;
|
||||||
@ -298,7 +299,7 @@ char * toBytes(double value, uint8_t decimals = 2)
|
|||||||
// WHOLE PART iv
|
// WHOLE PART iv
|
||||||
int integerPart = value;
|
int integerPart = value;
|
||||||
itoa(integerPart, &buffer[0], 10);
|
itoa(integerPart, &buffer[0], 10);
|
||||||
|
|
||||||
// DECIMALS
|
// DECIMALS
|
||||||
value -= integerPart;
|
value -= integerPart;
|
||||||
uint8_t pos = strlen(buffer);
|
uint8_t pos = strlen(buffer);
|
||||||
@ -312,7 +313,7 @@ char * toBytes(double value, uint8_t decimals = 2)
|
|||||||
value -= int(value);
|
value -= int(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UNITS
|
// UNITS
|
||||||
if (i <= strlen(t))
|
if (i <= strlen(t))
|
||||||
{
|
{
|
||||||
@ -330,3 +331,4 @@ char * toBytes(double value, uint8_t decimals = 2)
|
|||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
|
|
||||||
unittest_setup()
|
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)
|
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 * 1000, 6));
|
||||||
fprintf(stderr, "%s\n", sci(PI * 100, 6));
|
fprintf(stderr, "%s\n", sci(PI * 100, 6));
|
||||||
fprintf(stderr, "%s\n", sci(PI * 10, 6));
|
fprintf(stderr, "%s\n", sci(PI * 10, 6));
|
||||||
@ -66,9 +70,6 @@ unittest(test_sci)
|
|||||||
|
|
||||||
unittest(test_eng)
|
unittest(test_eng)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
|
|
||||||
fprintf(stderr, "PRINTBUFFERSIZE: %d\n", PRINTBUFFERSIZE);
|
|
||||||
|
|
||||||
int32_t value32 = 1UL << 25;
|
int32_t value32 = 1UL << 25;
|
||||||
|
|
||||||
fprintf(stderr, "VALUE32 = %ld\n", value32);
|
fprintf(stderr, "VALUE32 = %ld\n", value32);
|
||||||
@ -87,9 +88,6 @@ unittest(test_eng)
|
|||||||
|
|
||||||
unittest(test_print64)
|
unittest(test_print64)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
|
|
||||||
fprintf(stderr, "PRINTBUFFERSIZE: %d\n", PRINTBUFFERSIZE);
|
|
||||||
|
|
||||||
int64_t value64 = 1ULL << 35;
|
int64_t value64 = 1ULL << 35;
|
||||||
|
|
||||||
fprintf(stderr, "%ld\n", value64);
|
fprintf(stderr, "%ld\n", value64);
|
||||||
@ -103,8 +101,6 @@ unittest(test_print64)
|
|||||||
|
|
||||||
unittest(test_toBytes)
|
unittest(test_toBytes)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "VERSION: %s\n", (char *) PRINTHELPERS_VERSION);
|
|
||||||
|
|
||||||
for (int i = 0; i < 30; i++)
|
for (int i = 0; i < 30; i++)
|
||||||
{
|
{
|
||||||
uint32_t t = random(pow(2, i));
|
uint32_t t = random(pow(2, i));
|
||||||
|
Loading…
Reference in New Issue
Block a user