WLED math optimisations.

This commit is contained in:
Blaz Kristan 2021-04-16 20:07:54 +02:00
parent 1070d8d3fa
commit 585f8f4683
6 changed files with 42 additions and 26 deletions

View File

@ -2,6 +2,11 @@
### Builds after release 0.12.0
#### Build 2104151
- `NUM_STRIPS` no longer required with compile-time strip defaults
- Further optimizations in wled_math.h
#### Build 2104150
- Added ability to add multiple busses as compile time defaults using the esp32_multistrip usermod define syntax

View File

@ -70,13 +70,13 @@ void WS2812FX::finalizeInit(void)
if (busses.getNumBusses() == 0) {
const uint8_t defDataPins[] = {DATA_PINS};
const uint16_t defCounts[] = {PIXEL_COUNTS};
const uint8_t defDataPinsNo = ((sizeof defDataPins) / (sizeof defDataPins[0]));
const uint8_t defCountsNo = ((sizeof defCounts) / (sizeof defCounts[0]));
const uint8_t defNumBusses = ((sizeof defDataPins) / (sizeof defDataPins[0])); // min 1
const uint8_t defNumCounts = ((sizeof defCounts) / (sizeof defCounts[0])); // min 1
uint16_t prevLen = 0;
for (uint8_t i = 0; i < defDataPinsNo; i++) {
for (uint8_t i = 0; i < defNumBusses && i < WLED_MAX_BUSSES; i++) {
uint8_t defPin[] = {defDataPins[i]};
uint16_t start = prevLen;
uint16_t count = (i < defCountsNo) ? defCounts[i] : defCounts[i>0?i-1:0];
uint16_t count = (i < defNumCounts) ? defCounts[i] : defCounts[i>0?i-1:0];
prevLen += count;
BusConfig defCfg = BusConfig(DEFAULT_LED_TYPE, defPin, start, count, COL_ORDER_GRB);
busses.add(defCfg);

View File

@ -464,7 +464,6 @@ void serializeConfig() {
Bus *bus = busses.getBus(s);
if (!bus || bus->getLength()==0) break;
JsonObject ins = hw_led_ins.createNestedObject();
ins["en"] = true;
ins[F("start")] = bus->getStart();
ins[F("len")] = bus->getLength();
JsonArray ins_pin = ins.createNestedArray("pin");

View File

@ -319,32 +319,32 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
//1. first calculate the day of the year
float N1 = 275 * month / 9;
float N2 = (month + 9) / 12;
float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3));
float N3 = (1 + floor_t((year - 4 * floor_t(year / 4) + 2) / 3));
float N = N1 - (N2 * N3) + day - 30;
//2. convert the longitude to hour value and calculate an approximate time
float lngHour = lon / 15.0;
float lngHour = lon / 15.0f;
float t = N + (((sunset ? 18 : 6) - lngHour) / 24);
//3. calculate the Sun's mean anomaly
float M = (0.9856 * t) - 3.289;
float M = (0.9856f * t) - 3.289f;
//4. calculate the Sun's true longitude
float L = fmod(M + (1.916 * sin_t(DEG_TO_RAD*M)) + (0.020 * sin_t(2*DEG_TO_RAD*M)) + 282.634, 360.0);
float L = fmod_t(M + (1.916f * sin_t(DEG_TO_RAD*M)) + (0.020f * sin_t(2*DEG_TO_RAD*M)) + 282.634f, 360.0f);
//5a. calculate the Sun's right ascension
float RA = fmod(RAD_TO_DEG*atan_t(0.91764 * tan_t(DEG_TO_RAD*L)), 360.0);
float RA = fmod_t(RAD_TO_DEG*atan_t(0.91764f * tan_t(DEG_TO_RAD*L)), 360.0f);
//5b. right ascension value needs to be in the same quadrant as L
float Lquadrant = floor( L/90) * 90;
float RAquadrant = floor(RA/90) * 90;
float Lquadrant = floor_t( L/90) * 90;
float RAquadrant = floor_t(RA/90) * 90;
RA = RA + (Lquadrant - RAquadrant);
//5c. right ascension value needs to be converted into hours
RA /= 15.;
RA /= 15.0f;
//6. calculate the Sun's declination
float sinDec = 0.39782 * sin_t(DEG_TO_RAD*L);
float sinDec = 0.39782f * sin_t(DEG_TO_RAD*L);
float cosDec = cos_t(asin_t(sinDec));
//7a. calculate the Sun's local hour angle
@ -354,13 +354,13 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse
//7b. finish calculating H and convert into hours
float H = sunset ? RAD_TO_DEG*acos_t(cosH) : 360 - RAD_TO_DEG*acos_t(cosH);
H /= 15.;
H /= 15.0f;
//8. calculate local mean time of rising/setting
float T = H + RA - (0.06571 * t) - 6.622;
float T = H + RA - (0.06571f * t) - 6.622f;
//9. adjust back to UTC
float UT = fmod(T - lngHour, 24.0);
float UT = fmod_t(T - lngHour, 24.0f);
// return in minutes from midnight
return UT*60;

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2104150
#define VERSION 2104151
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG

View File

@ -13,7 +13,7 @@
#define modd(x, y) ((x) - (int)((x) / (y)) * (y))
double cos_t(double x)
float cos_t(float x)
{
x = modd(x, TWO_PI);
char sign = 1;
@ -22,17 +22,17 @@ double cos_t(double x)
x -= PI;
sign = -1;
}
double xx = x * x;
float xx = x * x;
return sign * (1 - ((xx) / (2)) + ((xx * xx) / (24)) - ((xx * xx * xx) / (720)) + ((xx * xx * xx * xx) / (40320)) - ((xx * xx * xx * xx * xx) / (3628800)) + ((xx * xx * xx * xx * xx * xx) / (479001600)));
}
double sin_t(double x) {
float sin_t(float x) {
return cos_t(HALF_PI - x);
}
double tan_t(double x) {
double c = cos_t(x);
float tan_t(float x) {
float c = cos_t(x);
if (c==0.0) return 0;
return sin_t(x) / c;
}
@ -63,9 +63,21 @@ float asin_t(float x) {
#define B -0.287434475393028
#define C ((HALF_PI/2) - A - B)
double atan_t(double x) {
double xx = x * x;
float atan_t(float x) {
float xx = x * x;
return ((A*xx + B)*xx + C)*x;
}
#endif
float floor_t(float x) {
bool neg = x < 0;
int val = x;
if (neg) val--;
return val;
}
float fmod_t(float num, float denom) {
int tquot = num / denom;
return num - tquot * denom;
}
#endif