mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
fix #144 added bin and hex formatter with #digits
This commit is contained in:
parent
00c7809986
commit
d99faee209
@ -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
|
||||
|
61
libraries/MathHelpers/examples/binhex_test/binhex_test.ino
Normal file
61
libraries/MathHelpers/examples/binhex_test/binhex_test.ino
Normal file
@ -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()
|
||||
{
|
||||
|
||||
}
|
@ -17,6 +17,8 @@ weeks KEYWORD2
|
||||
days KEYWORD2
|
||||
hours KEYWORD2
|
||||
minutes KEYWORD2
|
||||
hex KEYWORD2
|
||||
bin KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
|
@ -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": {
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=MathHelpers
|
||||
version=0.1.0
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library with representation functions for Arduino.
|
||||
|
Loading…
Reference in New Issue
Block a user