Skip to content

Commit

Permalink
make smoothstep use less flash memory
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Dec 31, 2023
1 parent b752519 commit e37a5ed
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions functions/smoothstep.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,37 @@
// From there it has a smooth transition to 32768, which will be reached at 75% of
// the blade. If WIDTH is negative, the transition will go the other way.

class SmoothStepBase {
public:
int getInteger(int led) {
int x = led * mult_ - location_;
if (x < 0) return 0;
if (x > 32768) return 32768;
return (((x * x) >> 14) * ((3<<14) - x)) >> 15;
}
protected:
int mult_;
int location_;
};

template<class POS, class WIDTH>
class SmoothStep {
class SmoothStep : public SmoothStepBase {
public:
void run(BladeBase* blade) {
pos_.run(blade);
width_.run(blade);
int width = width_.calculate(blade);
float mult;
if (width == 0) {
mult = 32768;
mult_ = 32768;
location_ = blade->num_leds() * pos_.calculate(blade);
} else {
mult = 32768 * 32768.0 / width / blade->num_leds();
mult_ = 32768 * 32768 / width / blade->num_leds();
location_ = 32768 * pos_.calculate(blade) / width - 16384;
}
mult_ = mult;
location_ = blade->num_leds() * mult * (pos_.calculate(blade) - width/2) / 32768;
}

int getInteger(int led) {
int x = led * mult_ - location_;
if (x < 0) return 0;
if (x > 32768) return 32768;
return (((x * x) >> 14) * ((3<<14) - x)) >> 15;
}

PONUA SVFWrapper<POS> pos_;
PONUA SVFWrapper<WIDTH> width_;
int mult_;
int location_;
};

#endif

0 comments on commit e37a5ed

Please sign in to comment.