From d99faee209ca5ef44de57da87f689203af0e6e04 Mon Sep 17 00:00:00 2001 From: RobTillaart Date: Sat, 29 Feb 2020 15:30:57 +0100 Subject: [PATCH] fix #144 added bin and hex formatter with #digits --- libraries/MathHelpers/MathHelpers.h | 43 ++++++++++++- .../examples/binhex_test/binhex_test.ino | 61 +++++++++++++++++++ libraries/MathHelpers/keywords.txt | 2 + libraries/MathHelpers/library.json | 4 +- libraries/MathHelpers/library.properties | 2 +- 5 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 libraries/MathHelpers/examples/binhex_test/binhex_test.ino diff --git a/libraries/MathHelpers/MathHelpers.h b/libraries/MathHelpers/MathHelpers.h index e667f69f..4f5c9b57 100644 --- a/libraries/MathHelpers/MathHelpers.h +++ b/libraries/MathHelpers/MathHelpers.h @@ -2,6 +2,7 @@ // FILE: MathHelpers.h // AUTHOR: Rob Tillaart // DATE: 2018-01-21 +// VERSION: 0.1.1 // // // PUPROSE: misc functions for math and time @@ -9,13 +10,13 @@ #ifndef MATHHELPERS #define MATHHELPERS -#define MATHHELPERS_VERSION (F("0.1.0")) +#define MATHHELPERS_VERSION (F("0.1.1")) // global buffer used by all functions // so we do not need a static buffer in every function // not usable in multi-threading environments // results need to be printed/copied asap -char __mathHelperBuffer[16]; +char __mathHelperBuffer[17]; ////////////////////////////////////////////////// @@ -203,6 +204,44 @@ float minutes(uint32_t seconds) return seconds * 1.666666666667e-2; // /60 } +////////////////////////////////////////////////// +// +// HEX BIN HELPERS +// +// notes: +// - d should not exceed 16 otherwise __mathHelperBuffer overflows... +// - no 64 bit support + +char * hex(uint32_t value, uint8_t d = 8) +{ + if (d > 16) d = 16; + __mathHelperBuffer[d] = '\0'; + while (d > 0) + { + uint8_t v = value & 0x0F; + value >>= 4; + __mathHelperBuffer[--d] = (v < 10) ? '0' + v : 'A' - 10 + v; + } + return __mathHelperBuffer; +} + + +char * bin(uint32_t value, uint8_t d = 8) +{ + if (d > 16) d = 16; + __mathHelperBuffer[d] = '\0'; + while (d > 0) + { + uint8_t v = value & 0x01; + value >>= 1; + __mathHelperBuffer[--d] = '0' + v; + } + return __mathHelperBuffer; +} + + + + #endif // MATHHELPERS // END OF FILE diff --git a/libraries/MathHelpers/examples/binhex_test/binhex_test.ino b/libraries/MathHelpers/examples/binhex_test/binhex_test.ino new file mode 100644 index 00000000..57deb9b7 --- /dev/null +++ b/libraries/MathHelpers/examples/binhex_test/binhex_test.ino @@ -0,0 +1,61 @@ +// +// FILE: binhex_test.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2020-02-29 +// (c) : - +// + +#include "MathHelpers.h" + +uint16_t x = 15; + +void setup() +{ + Serial.begin(115200); + for (uint8_t digits = 1; digits < 9; digits += 2) + { + Serial.println(hex(x, digits)); + } + Serial.println(); + + Serial.println(hex(12345, 4)); + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint32_t z = random(4E9); + Serial.print(hex(z)); + Serial.print('\t'); + Serial.println(z, HEX); + } + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint8_t z = random(256); + Serial.print(bin(z)); + Serial.print('\t'); + Serial.println(z, BIN); + } + Serial.println(); + + for (int i = 0; i < 10; i++) + { + uint8_t z = random(256); + Serial.print(bin(z, 4)); + Serial.print('\t'); + Serial.println(z, BIN); + } + Serial.println(); + + + Serial.println(bin(-5)); + Serial.println(hex(-16)); +} + +void loop() +{ + +} \ No newline at end of file diff --git a/libraries/MathHelpers/keywords.txt b/libraries/MathHelpers/keywords.txt index f944404d..185b898f 100644 --- a/libraries/MathHelpers/keywords.txt +++ b/libraries/MathHelpers/keywords.txt @@ -17,6 +17,8 @@ weeks KEYWORD2 days KEYWORD2 hours KEYWORD2 minutes KEYWORD2 +hex KEYWORD2 +bin KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/libraries/MathHelpers/library.json b/libraries/MathHelpers/library.json index 9cc21771..ab37e8b3 100644 --- a/libraries/MathHelpers/library.json +++ b/libraries/MathHelpers/library.json @@ -1,6 +1,6 @@ { "name": "MathHelpers", - "keywords": "Math, scientific, exponential, notation, clock, weeks, days, hours, minutes", + "keywords": "Math, scientific, exponential, notation, clock, weeks, days, hours, minutes, hex, bin", "description": "Library with representation function for Arduino.", "authors": [ @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/Arduino.git" }, - "version":"0.1.0", + "version":"0.1.1", "frameworks": "arduino", "platforms": "*", "export": { diff --git a/libraries/MathHelpers/library.properties b/libraries/MathHelpers/library.properties index 5b4bc8f0..f2f28791 100644 --- a/libraries/MathHelpers/library.properties +++ b/libraries/MathHelpers/library.properties @@ -1,5 +1,5 @@ name=MathHelpers -version=0.1.0 +version=0.1.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library with representation functions for Arduino.