Float and better 3rd party library compatibility (#2534)

* define as float (not double)

* Avoid #define of 1 or 2 char symbols

Having this file define 'A' and 'C' pollutes
the global namespace, and causes conflicts
with other libraries that also pollute the
global namespace with short #defines.
It's easier to fix this header.

* unused variable warning
This commit is contained in:
Henry Gabryjelski 2022-02-09 00:46:54 -08:00 committed by GitHub
parent 00b0193a43
commit 38bc618ee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 26 deletions

View File

@ -22,12 +22,12 @@
// 10 bits // 10 bits
#ifndef USERMOD_SN_PHOTORESISTOR_ADC_PRECISION #ifndef USERMOD_SN_PHOTORESISTOR_ADC_PRECISION
#define USERMOD_SN_PHOTORESISTOR_ADC_PRECISION 1024.0 #define USERMOD_SN_PHOTORESISTOR_ADC_PRECISION 1024.0f
#endif #endif
// resistor size 10K hms // resistor size 10K hms
#ifndef USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE #ifndef USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE
#define USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE 10000.0 #define USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE 10000.0f
#endif #endif
// only report if differance grater than offset value // only report if differance grater than offset value

View File

@ -21,10 +21,10 @@
#ifndef USERMOD_BATTERY_ADC_PRECISION #ifndef USERMOD_BATTERY_ADC_PRECISION
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
// 12 bits // 12 bits
#define USERMOD_BATTERY_ADC_PRECISION 4095.0 #define USERMOD_BATTERY_ADC_PRECISION 4095.0f
#else #else
// 10 bits // 10 bits
#define USERMOD_BATTERY_ADC_PRECISION 1024.0 #define USERMOD_BATTERY_ADC_PRECISION 1024.0f
#endif #endif
#endif #endif
@ -39,11 +39,11 @@
// https://batterybro.com/blogs/18650-wholesale-battery-reviews/18852515-when-to-recycle-18650-batteries-and-how-to-start-a-collection-center-in-your-vape-shop // https://batterybro.com/blogs/18650-wholesale-battery-reviews/18852515-when-to-recycle-18650-batteries-and-how-to-start-a-collection-center-in-your-vape-shop
// Discharge voltage: 2.5 volt + .1 for personal safety // Discharge voltage: 2.5 volt + .1 for personal safety
#ifndef USERMOD_BATTERY_MIN_VOLTAGE #ifndef USERMOD_BATTERY_MIN_VOLTAGE
#define USERMOD_BATTERY_MIN_VOLTAGE 2.6 #define USERMOD_BATTERY_MIN_VOLTAGE 2.6f
#endif #endif
#ifndef USERMOD_BATTERY_MAX_VOLTAGE #ifndef USERMOD_BATTERY_MAX_VOLTAGE
#define USERMOD_BATTERY_MAX_VOLTAGE 4.2 #define USERMOD_BATTERY_MAX_VOLTAGE 4.2f
#endif #endif
class UsermodBatteryBasic : public Usermod class UsermodBatteryBasic : public Usermod

View File

@ -80,36 +80,41 @@ float asin_t(float x) {
return res; return res;
} }
//https://stackoverflow.com/a/42542593 // declare a template with no implementation, and only one specialization
#define A 0.0776509570923569 // this allows hiding the constants, while ensuring ODR causes optimizations
#define B -0.287434475393028 // to still apply. (Fixes issues with conflicting 3rd party #define's)
#define C ((HALF_PI/2) - A - B) template <typename T> T atan_t(T x);
template<>
//polynominal factors for approximation between 1 and 5
#define C0 0.089494f
#define C1 0.974207f
#define C2 -0.326175f
#define C3 0.05375f
#define C4 -0.003445f
float atan_t(float x) { float atan_t(float x) {
bool neg = (x < 0); //For A/B/C, see https://stackoverflow.com/a/42542593
static const double A { 0.0776509570923569 };
static const double B { -0.287434475393028 };
static const double C { ((HALF_PI/2) - A - B) };
// polynominal factors for approximation between 1 and 5
static const float C0 { 0.089494f };
static const float C1 { 0.974207f };
static const float C2 { -0.326175f };
static const float C3 { 0.05375f };
static const float C4 { -0.003445f };
#ifdef WLED_DEBUG_MATH #ifdef WLED_DEBUG_MATH
float xinput = x; float xinput = x;
#endif #endif
bool neg = (x < 0);
x = std::abs(x); x = std::abs(x);
float res; float res;
if (x > 5.0f) { //atan(x) converges to pi/2 - (1/x) for large values if (x > 5.0f) { // atan(x) converges to pi/2 - (1/x) for large values
res = HALF_PI - (1.0f/x); res = HALF_PI - (1.0f/x);
} } else if (x > 1.0f) { //1 < x < 5
else if (x > 1.0f) { //1 < x < 5
float xx = x * x; float xx = x * x;
res = (C4*xx*xx)+(C3*xx*x)+(C2*xx)+(C1*x)+C0; res = (C4*xx*xx)+(C3*xx*x)+(C2*xx)+(C1*x)+C0;
} else { //this approximation is only for x <= 1 } else { // this approximation is only for x <= 1
float xx = x * x; float xx = x * x;
res = ((A*xx + B)*xx + C)*x; res = ((A*xx + B)*xx + C)*x;
} }
if (neg) res = -res; if (neg) {
res = -res;
}
#ifdef WLED_DEBUG_MATH #ifdef WLED_DEBUG_MATH
Serial.printf("atan,%f,%f,%f\n",xinput,res,atan(xinput)); Serial.printf("atan,%f,%f,%f\n",xinput,res,atan(xinput));
#endif #endif

View File

@ -46,8 +46,6 @@ void handleSerial()
static byte red = 0x00; static byte red = 0x00;
static byte green = 0x00; static byte green = 0x00;
uint16_t nBytes = 0;
while (Serial.available() > 0) while (Serial.available() > 0)
{ {
yield(); yield();