From b6aff9cca8538ee3af6c506ee4dcefb9941af3cb Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Fri, 24 Dec 2021 13:26:40 +0100 Subject: [PATCH] 0.2.3 printhelpers --- libraries/printHelpers/LICENSE | 2 +- libraries/printHelpers/README.md | 10 +++---- .../printHelpers/examples/print64/print64.ino | 4 +-- .../examples/print_sci/print_sci.ino | 2 +- .../examples/sci_test/sci_test.ino | 1 - .../printHelpers/examples/toBytes/toBytes.ino | 1 - libraries/printHelpers/library.json | 5 ++-- libraries/printHelpers/library.properties | 2 +- libraries/printHelpers/printHelpers.h | 30 ++++++++++--------- libraries/printHelpers/test/unit_test_001.cpp | 18 +++++------ 10 files changed, 35 insertions(+), 40 deletions(-) diff --git a/libraries/printHelpers/LICENSE b/libraries/printHelpers/LICENSE index 0feff1d7..2ede7a75 100644 --- a/libraries/printHelpers/LICENSE +++ b/libraries/printHelpers/LICENSE @@ -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 diff --git a/libraries/printHelpers/README.md b/libraries/printHelpers/README.md index 22c8305b..e77e9694 100644 --- a/libraries/printHelpers/README.md +++ b/libraries/printHelpers/README.md @@ -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) diff --git a/libraries/printHelpers/examples/print64/print64.ino b/libraries/printHelpers/examples/print64/print64.ino index 2598cab7..869617a6 100644 --- a/libraries/printHelpers/examples/print64/print64.ino +++ b/libraries/printHelpers/examples/print64/print64.ino @@ -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 -- + diff --git a/libraries/printHelpers/examples/print_sci/print_sci.ino b/libraries/printHelpers/examples/print_sci/print_sci.ino index 084a6733..2aec14d4 100644 --- a/libraries/printHelpers/examples/print_sci/print_sci.ino +++ b/libraries/printHelpers/examples/print_sci/print_sci.ino @@ -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 -- + diff --git a/libraries/printHelpers/examples/sci_test/sci_test.ino b/libraries/printHelpers/examples/sci_test/sci_test.ino index 0a1e4858..5ee1b6d0 100644 --- a/libraries/printHelpers/examples/sci_test/sci_test.ino +++ b/libraries/printHelpers/examples/sci_test/sci_test.ino @@ -1,7 +1,6 @@ // // FILE: sci_test.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: test different values with sci function diff --git a/libraries/printHelpers/examples/toBytes/toBytes.ino b/libraries/printHelpers/examples/toBytes/toBytes.ino index ecb45f36..644de271 100644 --- a/libraries/printHelpers/examples/toBytes/toBytes.ino +++ b/libraries/printHelpers/examples/toBytes/toBytes.ino @@ -1,7 +1,6 @@ // // FILE: toBytes.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo toBytes(double val); // DATE: 2020-07-03 diff --git a/libraries/printHelpers/library.json b/libraries/printHelpers/library.json index 35ef1f51..edc01321 100644 --- a/libraries/printHelpers/library.json +++ b/libraries/printHelpers/library.json @@ -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" } diff --git a/libraries/printHelpers/library.properties b/libraries/printHelpers/library.properties index 1ea274dd..e5f46e53 100644 --- a/libraries/printHelpers/library.properties +++ b/libraries/printHelpers/library.properties @@ -1,5 +1,5 @@ name=printHelpers -version=0.2.2 +version=0.2.3 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library to help formatting data for printing. 64 bit integers (base 10 and 16). Engineering and scientific notation. diff --git a/libraries/printHelpers/printHelpers.h b/libraries/printHelpers/printHelpers.h index 46eb9226..3686e993 100644 --- a/libraries/printHelpers/printHelpers.h +++ b/libraries/printHelpers/printHelpers.h @@ -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, ""); 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 -- + diff --git a/libraries/printHelpers/test/unit_test_001.cpp b/libraries/printHelpers/test/unit_test_001.cpp index d1673e43..9ef256f1 100644 --- a/libraries/printHelpers/test/unit_test_001.cpp +++ b/libraries/printHelpers/test/unit_test_001.cpp @@ -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));