Skip to content

Commit 56d7d84

Browse files
committed
cleanup and minor improvements
- removed local buffer for ESP8266 in 1D system to save on RAM - increased particle brightness in PS Impact - minor tweak in collision binning (might improve speed) - removed comments and some other unused stuff - fixed a few compiler wranings
1 parent 66e2e4d commit 56d7d84

3 files changed

Lines changed: 95 additions & 75 deletions

File tree

wled00/FX.cpp

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9338,7 +9338,7 @@ uint16_t mode_particlefire(void) {
93389338
uint32_t i; // index variable
93399339
uint32_t numFlames; // number of flames: depends on fire width. for a fire width of 16 pixels, about 25-30 flames give good results
93409340

9341-
if (SEGMENT.call == 0) { // initialization TODO: make this a PSinit function, this is needed in every particle FX but first, get this working.
9341+
if (SEGMENT.call == 0) { // initialization
93429342
if (!initParticleSystem2D(PartSys, SEGMENT.virtualWidth(), 4)) //maximum number of source (PS may limit based on segment size); need 4 additional bytes for time keeping (uint32_t lastcall)
93439343
return mode_static(); // allocation failed or not 2D
93449344
SEGENV.aux0 = hw_random16(); // aux0 is wind position (index) in the perlin noise
@@ -9430,7 +9430,7 @@ uint16_t mode_particlepit(void) {
94309430
ParticleSystem2D *PartSys = nullptr;
94319431

94329432
if (SEGMENT.call == 0) { // initialization
9433-
if (!initParticleSystem2D(PartSys, 1, 0, true, false)) // init, request one source (actually dont really need one TODO: test if using zero sources also works)
9433+
if (!initParticleSystem2D(PartSys, 0, 0, true, false)) // init
94349434
return mode_static(); // allocation failed or not 2D
94359435
PartSys->setKillOutOfBounds(true);
94369436
PartSys->setGravity(); // enable with default gravity
@@ -9501,7 +9501,7 @@ uint16_t mode_particlewaterfall(void) {
95019501
uint8_t numSprays;
95029502
uint32_t i = 0;
95039503

9504-
if (SEGMENT.call == 0) { // initialization TODO: make this a PSinit function, this is needed in every particle FX but first, get this working.
9504+
if (SEGMENT.call == 0) { // initialization
95059505
if (!initParticleSystem2D(PartSys, 12)) // init, request 12 sources, no additional data needed
95069506
return mode_static(); // allocation failed or not 2D
95079507

@@ -9524,7 +9524,7 @@ uint16_t mode_particlewaterfall(void) {
95249524
else
95259525
PartSys = reinterpret_cast<ParticleSystem2D *>(SEGENV.data); // if not first call, just set the pointer to the PS
95269526
if (PartSys == nullptr)
9527-
return mode_static(); // something went wrong, no data! (TODO: ask how to handle this so it always works)
9527+
return mode_static(); // something went wrong, no data!
95289528

95299529
// Particle System settings
95309530
PartSys->updateSystem(); // update system properties (dimensions and data pointers)
@@ -9653,7 +9653,7 @@ uint16_t mode_particleperlin(void) {
96539653
ParticleSystem2D *PartSys = nullptr;
96549654
uint32_t i;
96559655

9656-
if (SEGMENT.call == 0) { // initialization TODO: make this a PSinit function, this is needed in every particle FX but first, get this working.
9656+
if (SEGMENT.call == 0) { // initialization
96579657
if (!initParticleSystem2D(PartSys, 1, 0, true)) // init with 1 source and advanced properties
96589658
return mode_static(); // allocation failed or not 2D
96599659

@@ -9714,20 +9714,19 @@ static const char _data_FX_MODE_PARTICLEPERLIN[] PROGMEM = "PS Fuzzy Noise@Speed
97149714
uint16_t mode_particleimpact(void) {
97159715
ParticleSystem2D *PartSys = nullptr;
97169716
uint32_t i = 0;
9717-
uint8_t MaxNumMeteors;
9717+
uint32_t numMeteors;
97189718
PSsettings2D meteorsettings;
97199719
meteorsettings.asByte = 0b00101000; // PS settings for meteors: bounceY and gravity enabled
97209720

9721-
if (SEGMENT.call == 0) { // initialization TODO: make this a PSinit function, this is needed in every particle FX but first, get this working.
9721+
if (SEGMENT.call == 0) { // initialization
97229722
if (!initParticleSystem2D(PartSys, NUMBEROFSOURCES)) // init, no additional data needed
97239723
return mode_static(); // allocation failed or not 2D
97249724
PartSys->setKillOutOfBounds(true);
97259725
PartSys->setGravity(); // enable default gravity
97269726
PartSys->setBounceY(true); // always use ground bounce
97279727
PartSys->setWallRoughness(220); // high roughness
9728-
MaxNumMeteors = min(PartSys->numSources, (uint32_t)NUMBEROFSOURCES);
9729-
for (i = 0; i < MaxNumMeteors; i++) {
9730-
// PartSys->sources[i].source.y = 500;
9728+
numMeteors = min(PartSys->numSources, (uint32_t)NUMBEROFSOURCES);
9729+
for (i = 0; i < numMeteors; i++) {
97319730
PartSys->sources[i].source.ttl = hw_random16(10 * i); // set initial delay for meteors
97329731
PartSys->sources[i].source.vy = 10; // at positive speeds, no particles are emitted and if particle dies, it will be relaunched
97339732
}
@@ -9736,7 +9735,7 @@ uint16_t mode_particleimpact(void) {
97369735
PartSys = reinterpret_cast<ParticleSystem2D *>(SEGENV.data); // if not first call, just set the pointer to the PS
97379736

97389737
if (PartSys == nullptr)
9739-
return mode_static(); // something went wrong, no data! (TODO: ask how to handle this so it always works)
9738+
return mode_static(); // something went wrong, no data!
97409739

97419740
// Particle System settings
97429741
PartSys->updateSystem(); // update system properties (dimensions and data pointers)
@@ -9746,29 +9745,18 @@ uint16_t mode_particleimpact(void) {
97469745
uint8_t hardness = map(SEGMENT.custom2, 0, 255, PS_P_MINSURFACEHARDNESS - 2, 255);
97479746
PartSys->setWallHardness(hardness);
97489747
PartSys->enableParticleCollisions(SEGMENT.check3, hardness); // enable collisions and set particle collision hardness
9749-
MaxNumMeteors = min(PartSys->numSources, (uint32_t)NUMBEROFSOURCES);
9750-
uint8_t numMeteors = MaxNumMeteors; // TODO: clean this up map(SEGMENT.custom3, 0, 31, 1, MaxNumMeteors); // number of meteors to use for animation
9751-
9748+
numMeteors = min(PartSys->numSources, (uint32_t)NUMBEROFSOURCES);
97529749
uint32_t emitparticles; // number of particles to emit for each rocket's state
97539750

97549751
for (i = 0; i < numMeteors; i++) {
97559752
// determine meteor state by its speed:
9756-
if ( PartSys->sources[i].source.vy < 0) { // moving down, emit sparks
9757-
#ifdef ESP8266
9753+
if ( PartSys->sources[i].source.vy < 0) // moving down, emit sparks
97589754
emitparticles = 1;
9759-
#else
9760-
emitparticles = 2;
9761-
#endif
9762-
}
97639755
else if ( PartSys->sources[i].source.vy > 0) // moving up means meteor is on 'standby'
97649756
emitparticles = 0;
97659757
else { // speed is zero, explode!
97669758
PartSys->sources[i].source.vy = 10; // set source speed positive so it goes into timeout and launches again
9767-
#ifdef ESP8266
9768-
emitparticles = hw_random16(SEGMENT.intensity >> 3) + 5; // defines the size of the explosion
9769-
#else
9770-
emitparticles = map(SEGMENT.intensity, 0, 255, 10, hw_random16(PartSys->usedParticles>>2)); // defines the size of the explosion !!!TODO: check if this works on ESP8266, drop esp8266 def if it does
9771-
#endif
9759+
emitparticles = map(SEGMENT.intensity, 0, 255, 10, hw_random16(PartSys->usedParticles>>2)); // defines the size of the explosion
97729760
}
97739761
for (int e = emitparticles; e > 0; e--) {
97749762
PartSys->sprayEmit(PartSys->sources[i]);
@@ -9789,13 +9777,13 @@ uint16_t mode_particleimpact(void) {
97899777
PartSys->sources[i].source.vx = 0;
97909778
PartSys->sources[i].sourceFlags.collide = true;
97919779
#ifdef ESP8266
9792-
PartSys->sources[i].maxLife = 180;
9793-
PartSys->sources[i].minLife = 20;
9780+
PartSys->sources[i].maxLife = 900;
9781+
PartSys->sources[i].minLife = 100;
97949782
#else
9795-
PartSys->sources[i].maxLife = 250;
9796-
PartSys->sources[i].minLife = 50;
9783+
PartSys->sources[i].maxLife = 1250;
9784+
PartSys->sources[i].minLife = 250;
97979785
#endif
9798-
PartSys->sources[i].source.ttl = hw_random16((512 - (SEGMENT.speed << 1))) + 40; // standby time til next launch (in frames)
9786+
PartSys->sources[i].source.ttl = hw_random16((768 - (SEGMENT.speed << 1))) + 40; // standby time til next launch (in frames)
97999787
PartSys->sources[i].vy = (SEGMENT.custom1 >> 2); // emitting speed y
98009788
PartSys->sources[i].var = (SEGMENT.custom1 >> 2); // speed variation around vx,vy (+/- var)
98019789
}
@@ -9810,13 +9798,17 @@ uint16_t mode_particleimpact(void) {
98109798
PartSys->sources[i].source.hue = hw_random16(); // random color
98119799
PartSys->sources[i].source.ttl = 500; // long life, will explode at bottom
98129800
PartSys->sources[i].sourceFlags.collide = false; // trail particles will not collide
9813-
PartSys->sources[i].maxLife = 60; // spark particle life
9814-
PartSys->sources[i].minLife = 20;
9801+
PartSys->sources[i].maxLife = 300; // spark particle life
9802+
PartSys->sources[i].minLife = 100;
98159803
PartSys->sources[i].vy = -9; // emitting speed (down)
98169804
PartSys->sources[i].var = 3; // speed variation around vx,vy (+/- var)
98179805
}
98189806
}
98199807

9808+
for (uint32_t i = 0; i < PartSys->usedParticles; i++) {
9809+
if (PartSys->particles[i].ttl > 5) PartSys->particles[i].ttl -= 5; //ttl is linked to brightness, this allows to use higher brightness but still a short spark lifespan
9810+
}
9811+
98209812
PartSys->update(); // update and render
98219813
return FRAMETIME;
98229814
}
@@ -10220,7 +10212,7 @@ uint16_t mode_particleghostrider(void) {
1022010212
// emit two particles
1022110213
PartSys->angleEmit(PartSys->sources[0], emitangle, speed);
1022210214
PartSys->angleEmit(PartSys->sources[0], emitangle, speed);
10223-
if (SEGMENT.call % (11 - (SEGMENT.custom2 / 25)) == 0) { // every nth frame, cycle color and emit particles //TODO: make this a segment call % SEGMENT.custom2 for better control
10215+
if (SEGMENT.call % (11 - (SEGMENT.custom2 / 25)) == 0) { // every nth frame, cycle color and emit particles
1022410216
PartSys->sources[0].source.hue++;
1022510217
}
1022610218
if (SEGMENT.custom2 > 190) //fast color change
@@ -10240,7 +10232,7 @@ uint16_t mode_particleblobs(void) {
1024010232
ParticleSystem2D *PartSys = nullptr;
1024110233

1024210234
if (SEGMENT.call == 0) {
10243-
if (!initParticleSystem2D(PartSys, 1, 0, true, true)) //init, request one source, no additional bytes, advanced size & size control (actually dont really need one TODO: test if using zero sources also works)
10235+
if (!initParticleSystem2D(PartSys, 0, 0, true, true)) //init, no additional bytes, advanced size & size control
1024410236
return mode_static(); // allocation failed or not 2D
1024510237
PartSys->setBounceX(true);
1024610238
PartSys->setBounceY(true);
@@ -10909,7 +10901,7 @@ uint16_t mode_particleHourglass(void) {
1090910901
}
1091010902

1091110903
// re-order particles in case collisions flipped particles (highest number index particle is on the "bottom")
10912-
for (int i = 0; i < PartSys->usedParticles - 1; i++) {
10904+
for (uint32_t i = 0; i < PartSys->usedParticles - 1; i++) {
1091310905
if (PartSys->particles[i].x < PartSys->particles[i+1].x && PartSys->particleFlags[i].fixed == false && PartSys->particleFlags[i+1].fixed == false) {
1091410906
std::swap(PartSys->particles[i].x, PartSys->particles[i+1].x);
1091510907
}
@@ -11020,10 +11012,7 @@ uint16_t mode_particleBalance(void) {
1102011012
if (SEGMENT.call == 0) { // initialization
1102111013
if (!initParticleSystem1D(PartSys, 1, 128)) // init, no additional data needed, use half of max particles
1102211014
return mode_static(); // allocation failed or is single pixel
11023-
//PartSys->setKillOutOfBounds(true);
1102411015
PartSys->setParticleSize(1);
11025-
SEGENV.aux0 = 0;
11026-
SEGENV.aux1 = 0; //TODO: really need to set to zero or is it calloc'd?
1102711016
}
1102811017
else
1102911018
PartSys = reinterpret_cast<ParticleSystem1D *>(SEGENV.data); // if not first call, just set the pointer to the PS
@@ -11175,7 +11164,7 @@ uint16_t mode_particleChase(void) {
1117511164
globalhuestep = 2; // global hue change to add some color variation
1117611165
if ((SEGMENT.call & 0x1F) == 0)
1117711166
SEGENV.step += *stepdir; // change density
11178-
for(int32_t i = 0; i < PartSys->usedParticles; i++) {
11167+
for(uint32_t i = 0; i < PartSys->usedParticles; i++) {
1117911168
PartSys->particles[i].hue -= globalhuestep; // shift global hue (both directions)
1118011169
PartSys->particles[i].vx = 1 + (SEGMENT.speed >> 2) + ((int32_t(sin16_t(strip.now >> 1) + 32767) * (SEGMENT.speed >> 2)) >> 16);
1118111170
}
@@ -11347,7 +11336,7 @@ uint16_t mode_particleFire1D(void) {
1134711336
PartSys->setColorByAge(true);
1134811337
uint32_t emitparticles = 1;
1134911338
uint32_t j = hw_random16();
11350-
for (uint i = 0; i < 3; i++) { // 3 base flames TODO: check if this is ok or needs adjustments
11339+
for (uint i = 0; i < 3; i++) { // 3 base flames
1135111340
if (PartSys->sources[i].source.ttl > 50)
1135211341
PartSys->sources[i].source.ttl -= 10; // TODO: in 2D making the source fade out slow results in much smoother flames, need to check if it can be done the same
1135311342
else
@@ -11368,7 +11357,7 @@ uint16_t mode_particleFire1D(void) {
1136811357
}
1136911358
}
1137011359
else {
11371-
PartSys->sources[j].minLife = PartSys->sources[j].source.ttl + SEGMENT.intensity; // TODO: in 2D, emitted particle ttl depends on source TTL, mimic here the same way? OR: change 2D to the same way it is done here and ditch special fire treatment in emit?
11360+
PartSys->sources[j].minLife = PartSys->sources[j].source.ttl + SEGMENT.intensity;
1137211361
PartSys->sources[j].maxLife = PartSys->sources[j].minLife + 50;
1137311362
PartSys->sources[j].v = SEGMENT.speed >> 2;
1137411363
if (SEGENV.call & 0x01) // every second frame
@@ -11629,7 +11618,7 @@ uint16_t mode_particleSpringy(void) {
1162911618
PartSys->particles[0].x = dxlimit; // limit the spring length
1163011619
springforce[0] += ((springlength >> 1) - (PartSys->particles[0].x)) * springK; // first particle anchors to x=0
1163111620

11632-
for (int32_t i = 1; i < PartSys->usedParticles; i++) {
11621+
for (uint32_t i = 1; i < PartSys->usedParticles; i++) {
1163311622
// reorder particles if they are out of order to prevent chaos
1163411623
if (PartSys->particles[i].x < PartSys->particles[i-1].x)
1163511624
std::swap(PartSys->particles[i].x, PartSys->particles[i-1].x); // swap particle positions to maintain order
@@ -11650,7 +11639,7 @@ uint16_t mode_particleSpringy(void) {
1165011639
}
1165111640
// apply spring forces to particles
1165211641
bool dampenoscillations = (SEGMENT.call % (9 - (SEGMENT.speed >> 5))) == 0; // dampen oscillation if particles are slow, more damping on stiffer springs
11653-
for (int32_t i = 0; i < PartSys->usedParticles; i++) {
11642+
for (uint32_t i = 0; i < PartSys->usedParticles; i++) {
1165411643
springforce[i] = springforce[i] / 64; // scale spring force (cannot use shifts because of negative values)
1165511644
int maxforce = 120; // limit spring force
1165611645
springforce[i] = springforce[i] > maxforce ? maxforce : springforce[i] < -maxforce ? -maxforce : springforce[i]; // limit spring force
@@ -11667,7 +11656,7 @@ uint16_t mode_particleSpringy(void) {
1166711656
PartSys->applyFriction((SEGMENT.intensity >> 2));
1166811657

1166911658
// add a small resetting force so particles return to resting position even under high damping
11670-
for (int32_t i = 1; i < PartSys->usedParticles - 1; i++) {
11659+
for (uint32_t i = 1; i < PartSys->usedParticles - 1; i++) {
1167111660
int restposition = (springlength >> 1) + i * springlength; // resting position
1167211661
int dx = restposition - PartSys->particles[i].x; // distance, always positive
1167311662
PartSys->applyForce(PartSys->particles[i], dx > 0 ? 1 : (dx < 0 ? -1 : 0), PartSys->advPartProps[i].forcecounter);
@@ -11717,7 +11706,7 @@ uint16_t mode_particleSpringy(void) {
1171711706
}
1171811707
}
1171911708

11720-
for (int32_t i = 0; i < PartSys->usedParticles; i++) {
11709+
for (uint32_t i = 0; i < PartSys->usedParticles; i++) {
1172111710
if (SEGMENT.custom2 == 255) { // map speed to hue
1172211711
int speedclr = ((int8_t(abs(PartSys->particles[i].vx))) >> 2) << 4; // scale for greater color variation, dump small values to avoid flickering
1172311712
//int speed = PartSys->particles[i].vx << 2; // +/- 512

0 commit comments

Comments
 (0)