mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
initial version SCI notation
This commit is contained in:
parent
6563bb4526
commit
d630debbed
208
libraries/MathHelper/MathHelpers.h
Normal file
208
libraries/MathHelper/MathHelpers.h
Normal file
@ -0,0 +1,208 @@
|
||||
//
|
||||
// FILE: MathHelpers.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2018-01-21
|
||||
//
|
||||
//
|
||||
// PUPROSE: misc functions for math and time
|
||||
//
|
||||
#ifndef MATHHELPERS
|
||||
#define MATHHELPERS
|
||||
|
||||
#define MATHHELPERS_VERSION (F("0.1.0"))
|
||||
|
||||
// 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];
|
||||
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
//
|
||||
// FLOAT REPRESENTATION HELPERS
|
||||
//
|
||||
char * sci(double number, int digits)
|
||||
{
|
||||
int exponent = 0;
|
||||
int pos = 0;
|
||||
|
||||
// Handling these costs 13 bytes RAM
|
||||
// shorten them with N, I, -I ?
|
||||
if (isnan(number))
|
||||
{
|
||||
strcpy(__mathHelperBuffer, "nan");
|
||||
return __mathHelperBuffer;
|
||||
}
|
||||
if (isinf(number))
|
||||
{
|
||||
if (number < 0) strcpy(__mathHelperBuffer, "-inf");
|
||||
strcpy(__mathHelperBuffer, "inf");
|
||||
return __mathHelperBuffer;
|
||||
}
|
||||
|
||||
// Handle negative numbers
|
||||
bool neg = (number < 0.0);
|
||||
if (neg)
|
||||
{
|
||||
__mathHelperBuffer[pos++] = '-';
|
||||
number = -number;
|
||||
}
|
||||
|
||||
while (number >= 10.0)
|
||||
{
|
||||
number /= 10;
|
||||
exponent++;
|
||||
}
|
||||
while (number < 1 && number != 0.0)
|
||||
{
|
||||
number *= 10;
|
||||
exponent--;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
double rounding = 0.5;
|
||||
for (uint8_t i = 0; i < digits; ++i)
|
||||
{
|
||||
rounding *= 0.1;
|
||||
}
|
||||
number += rounding;
|
||||
if (number >= 10)
|
||||
{
|
||||
exponent++;
|
||||
number /= 10;
|
||||
}
|
||||
|
||||
|
||||
// Extract the integer part of the number and print it
|
||||
uint8_t d = (uint8_t)number;
|
||||
double remainder = number - d;
|
||||
__mathHelperBuffer[pos++] = d + '0'; // 1 digit before decimal point
|
||||
if (digits > 0)
|
||||
{
|
||||
__mathHelperBuffer[pos++] = '.'; // decimal point TODO:rvdt CONFIG?
|
||||
}
|
||||
|
||||
|
||||
// Extract digits from the remainder one at a time to prevent missing leading zero's
|
||||
while (digits-- > 0)
|
||||
{
|
||||
remainder *= 10.0;
|
||||
d = (uint8_t)remainder;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
remainder -= d;
|
||||
}
|
||||
|
||||
|
||||
// print exponent
|
||||
__mathHelperBuffer[pos++] = 'E';
|
||||
neg = exponent < 0;
|
||||
if (neg)
|
||||
{
|
||||
__mathHelperBuffer[pos++] = '-';
|
||||
exponent = -exponent;
|
||||
}
|
||||
else __mathHelperBuffer[pos++] = '+';
|
||||
|
||||
|
||||
// 3 digits for exponent; // needed for double
|
||||
// d = exponent / 100;
|
||||
// __mathHelperBuffer[pos++] = d + '0';
|
||||
// exponent -= d * 100;
|
||||
|
||||
// 2 digits for exponent
|
||||
d = exponent / 10;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
d = exponent - d*10;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
|
||||
__mathHelperBuffer[pos] = '\0';
|
||||
|
||||
return __mathHelperBuffer;
|
||||
}
|
||||
|
||||
|
||||
void sci(Stream &str, float f, uint8_t digits)
|
||||
{
|
||||
str.print(sci(f, digits));
|
||||
}
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
//
|
||||
// TIME HELPERS
|
||||
//
|
||||
|
||||
// (true) 00:00:00 .. 23:59:59
|
||||
// (false) 00:00 .. 23:59
|
||||
char * seconds2clock(uint32_t seconds, bool displaySeconds=false)
|
||||
{
|
||||
uint16_t days = seconds / 86400UL; // strips the days
|
||||
seconds -= (days * 86400UL);
|
||||
uint8_t hours = seconds / 3600UL;
|
||||
seconds -= (hours * 3600UL);
|
||||
uint8_t minutes = seconds / 60UL;
|
||||
seconds -= (minutes * 60UL);
|
||||
|
||||
uint8_t pos = 0;
|
||||
__mathHelperBuffer[pos++] = hours/10 + '0';
|
||||
__mathHelperBuffer[pos++] = hours%10 + '0';
|
||||
__mathHelperBuffer[pos++] = ':';
|
||||
|
||||
__mathHelperBuffer[pos++] = minutes/10 + '0';
|
||||
__mathHelperBuffer[pos++] = minutes%10 + '0';
|
||||
if (displaySeconds)
|
||||
{
|
||||
__mathHelperBuffer[pos++] = ':';
|
||||
__mathHelperBuffer[pos++] = seconds/10 + '0';
|
||||
__mathHelperBuffer[pos++] = seconds%10 + '0';
|
||||
}
|
||||
__mathHelperBuffer[pos] = '\0';
|
||||
|
||||
return __mathHelperBuffer;
|
||||
}
|
||||
|
||||
char * millis2clock(uint32_t millis)
|
||||
{
|
||||
uint32_t t = millis/1000;
|
||||
seconds2clock(t, true);
|
||||
uint16_t m = millis - t*1000;
|
||||
|
||||
__mathHelperBuffer[8] = '.';
|
||||
uint8_t pos = 9;
|
||||
uint8_t d = m/100;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
m = m - d * 100;
|
||||
d = m/10;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
d = m - d * 10;
|
||||
__mathHelperBuffer[pos++] = d + '0';
|
||||
__mathHelperBuffer[pos] = '\0';
|
||||
|
||||
return __mathHelperBuffer;
|
||||
}
|
||||
|
||||
float weeks(uint32_t seconds)
|
||||
{
|
||||
return seconds * 1.653439153439e-6; // /604800
|
||||
}
|
||||
|
||||
float days(uint32_t seconds)
|
||||
{
|
||||
return seconds * 1.157407407407e-5; // /86400
|
||||
}
|
||||
|
||||
float hours(uint32_t seconds)
|
||||
{
|
||||
return seconds * 2.777777777778e-4; // /3600
|
||||
}
|
||||
|
||||
float minutes(uint32_t seconds)
|
||||
{
|
||||
return seconds * 1.666666666667e-2; // /60
|
||||
}
|
||||
|
||||
#endif // MATHHELPERS
|
||||
|
||||
// END OF FILE
|
208
libraries/MathHelper/examples/mathHelperTest/mathHelperTest.ino
Normal file
208
libraries/MathHelper/examples/mathHelperTest/mathHelperTest.ino
Normal file
@ -0,0 +1,208 @@
|
||||
//
|
||||
// FILE: mathHelperTest.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.0.1
|
||||
// PURPOSE:
|
||||
//
|
||||
// HISTORY:
|
||||
|
||||
#include "MathHelpers.h"
|
||||
|
||||
uint32_t start;
|
||||
uint32_t stop;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("MATHHELPERS_VERSION: ");
|
||||
Serial.println(MATHHELPERS_VERSION);
|
||||
|
||||
test1();
|
||||
test2();
|
||||
test3();
|
||||
test4();
|
||||
test5();
|
||||
|
||||
test10();
|
||||
test11();
|
||||
test12();
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{}
|
||||
|
||||
|
||||
void test1()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
float f = 1;
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
f *= 10;
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
f = 1;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
f /= 10;
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
f = -1;
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
f *= 10;
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
f = -1;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
f /= 10;
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test2()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
float f = 1;
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
f *= (PI * PI);
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
f = 1;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
f /= (PI * PI);
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
f = -1;
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
f *= (PI * PI);
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
f = -1;
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
f /= (PI * PI);
|
||||
Serial.println(sci(f, 6));
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test3()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
float f = PI;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
Serial.println(sci(f, i));
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test4()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
float f = PI;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
sci(Serial, f, i);
|
||||
Serial.println();
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void test5()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
float f = 1.0 / 0.0;
|
||||
Serial.println(sci(f, 6));
|
||||
f = 0.0 / 0.0;
|
||||
Serial.println(sci(f, 6));
|
||||
f = -1.0 / 0.0;
|
||||
Serial.println(sci(f, 6));
|
||||
// TODO find a -inf
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void test10()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
Serial.println("HH:MM:SS");
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
uint32_t now = micros();
|
||||
Serial.println(seconds2clock(now));
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test11()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
Serial.println("HH:MM:SS.nnn");
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
uint32_t now = micros();
|
||||
Serial.println(millis2clock(now));
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void test12()
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println(__FUNCTION__);
|
||||
|
||||
Serial.println("time\tweeks\tdays\thours\tminutes");
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
uint32_t now = micros();
|
||||
Serial.print(now);
|
||||
Serial.print('\t');
|
||||
Serial.print(weeks(now), 3);
|
||||
Serial.print('\t');
|
||||
Serial.print(days(now), 3);
|
||||
Serial.print('\t');
|
||||
Serial.print(hours(now), 2);
|
||||
Serial.print('\t');
|
||||
Serial.println(minutes(now), 2);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
// END OF FILE
|
24
libraries/MathHelper/keywords.txt
Normal file
24
libraries/MathHelper/keywords.txt
Normal file
@ -0,0 +1,24 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map for Troolean
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
sci KEYWORD2
|
||||
seconds2clock KEYWORD2
|
||||
millis2clock KEYWORD2
|
||||
weeks KEYWORD2
|
||||
days KEYWORD2
|
||||
hours KEYWORD2
|
||||
minutes KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
24
libraries/MathHelper/library.json
Normal file
24
libraries/MathHelper/library.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "MathHelpers",
|
||||
"keywords": "Math, scientific, exponential, notation, clock, weeks, days, hours, minutes",
|
||||
"description": "Library with representation function for Arduino.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Arduino.git"
|
||||
},
|
||||
"version":"0.1.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"export": {
|
||||
"include": "libraries/MathHelpers"
|
||||
}
|
||||
}
|
9
libraries/MathHelper/library.properties
Normal file
9
libraries/MathHelper/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=MathHelpers
|
||||
version=0.1.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library with representation functions for Arduino.
|
||||
paragraph=
|
||||
category=Uncategorized
|
||||
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
||||
architectures=*
|
Loading…
Reference in New Issue
Block a user