Skip to content

Commit

Permalink
More tuning
Browse files Browse the repository at this point in the history
- replaced POD new/delete with malloc/free
- some more SEGLEN <= 1
- some gnu::pure
- more const attributes
- some static attributes
  • Loading branch information
blazoncek committed Jan 9, 2025
1 parent 6099197 commit aa2c4f8
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 108 deletions.
70 changes: 35 additions & 35 deletions wled00/FX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static const char _data_FX_MODE_STROBE_RAINBOW[] PROGMEM = "Strobe Rainbow@!;,!;
* if (bool rev == true) then LEDs are turned off in reverse order
*/
uint16_t color_wipe(bool rev, bool useRandomColors) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
uint32_t cycleTime = 750 + (255 - SEGMENT.speed)*150;
uint32_t perc = strip.now % cycleTime;
unsigned prog = (perc * 65535) / cycleTime;
Expand Down Expand Up @@ -410,7 +410,7 @@ static const char _data_FX_MODE_FADE[] PROGMEM = "Fade@!;!,!;!;01";
* Scan mode parent function
*/
uint16_t scan(bool dual) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
uint32_t cycleTime = 750 + (255 - SEGMENT.speed)*150;
uint32_t perc = strip.now % cycleTime;
int prog = (perc * 65535) / cycleTime;
Expand Down Expand Up @@ -1023,7 +1023,7 @@ static const char _data_FX_MODE_COLORFUL[] PROGMEM = "Colorful@!,Saturation;1,2,
* Emulates a traffic light.
*/
uint16_t mode_traffic_light(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
for (unsigned i=0; i < SEGLEN; i++)
SEGMENT.setPixelColor(i, SEGMENT.color_from_palette(i, true, PALETTE_SOLID_WRAP, 1));
uint32_t mdelay = 500;
Expand Down Expand Up @@ -1056,7 +1056,7 @@ static const char _data_FX_MODE_TRAFFIC_LIGHT[] PROGMEM = "Traffic Light@!,US st
*/
#define FLASH_COUNT 4
uint16_t mode_chase_flash(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned flash_step = SEGENV.call % ((FLASH_COUNT * 2) + 1);

for (unsigned i = 0; i < SEGLEN; i++) {
Expand Down Expand Up @@ -1086,7 +1086,7 @@ static const char _data_FX_MODE_CHASE_FLASH[] PROGMEM = "Chase Flash@!;Bg,Fx;!";
* Prim flashes running, followed by random color.
*/
uint16_t mode_chase_flash_random(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned flash_step = SEGENV.call % ((FLASH_COUNT * 2) + 1);

for (int i = 0; i < SEGENV.aux1; i++) {
Expand Down Expand Up @@ -1168,7 +1168,7 @@ static const char _data_FX_MODE_RUNNING_RANDOM[] PROGMEM = "Stream@!,Zone size;;
* K.I.T.T.
*/
uint16_t mode_larson_scanner(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();

const unsigned speed = FRAMETIME * map(SEGMENT.speed, 0, 255, 96, 2); // map into useful range
const unsigned pixels = SEGLEN / speed; // how many pixels to advance per frame
Expand Down Expand Up @@ -1226,7 +1226,7 @@ static const char _data_FX_MODE_DUAL_LARSON_SCANNER[] PROGMEM = "Scanner Dual@!,
* Firing comets from one end. "Lighthouse"
*/
uint16_t mode_comet(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned counter = (strip.now * ((SEGMENT.speed >>2) +1)) & 0xFFFF;
unsigned index = (counter * SEGLEN) >> 16;
if (SEGENV.call == 0) SEGENV.aux0 = index;
Expand Down Expand Up @@ -1254,7 +1254,7 @@ static const char _data_FX_MODE_COMET[] PROGMEM = "Lighthouse@!,Fade rate;!,!;!"
* Fireworks function.
*/
uint16_t mode_fireworks() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const uint16_t width = SEGMENT.is2D() ? SEG_W : SEGLEN;
const uint16_t height = SEG_H;

Expand Down Expand Up @@ -1296,7 +1296,7 @@ static const char _data_FX_MODE_FIREWORKS[] PROGMEM = "Fireworks@,Frequency;!,!;

//Twinkling LEDs running. Inspired by https://github.com/kitesurfer1404/WS2812FX/blob/master/src/custom/Rain.h
uint16_t mode_rain() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const unsigned width = SEG_W;
const unsigned height = SEG_H;
SEGENV.step += FRAMETIME;
Expand Down Expand Up @@ -1362,7 +1362,7 @@ static const char _data_FX_MODE_FIRE_FLICKER[] PROGMEM = "Fire Flicker@!,!;!;!;0
* Gradient run base function
*/
uint16_t gradient_base(bool loading) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
uint16_t counter = strip.now * ((SEGMENT.speed >> 2) + 1);
uint16_t pp = (counter * SEGLEN) >> 16;
if (SEGENV.call == 0) pp = 0;
Expand Down Expand Up @@ -1407,7 +1407,7 @@ static const char _data_FX_MODE_LOADING[] PROGMEM = "Loading@!,Fade;!,!;!;;ix=16
* Two dots running
*/
uint16_t mode_two_dots() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned delay = 1 + (FRAMETIME<<3) / SEGLEN; // longer segments should change faster
uint32_t it = strip.now / map(SEGMENT.speed, 0, 255, delay<<4, delay);
unsigned offset = it % SEGLEN;
Expand Down Expand Up @@ -1858,7 +1858,7 @@ static const char _data_FX_MODE_OSCILLATE[] PROGMEM = "Oscillate";

//TODO
uint16_t mode_lightning(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned ledstart = hw_random16(SEGLEN); // Determine starting location of flash
unsigned ledlen = 1 + hw_random16(SEGLEN -ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
uint8_t bri = 255/hw_random8(1, 3);
Expand Down Expand Up @@ -1944,7 +1944,7 @@ static const char _data_FX_MODE_PRIDE_2015[] PROGMEM = "Pride 2015@!;;";

//eight colored dots, weaving in and out of sync with each other
uint16_t mode_juggle(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();

SEGMENT.fadeToBlackBy(192 - (3*SEGMENT.intensity/4));
CRGB fastled_col;
Expand Down Expand Up @@ -2089,7 +2089,7 @@ static const char _data_FX_MODE_PALETTE[] PROGMEM = "Palette@Shift,Size,Rotation
// feel of your fire: COOLING (used in step 1 above) (Speed = COOLING), and SPARKING (used
// in step 3 above) (Effect Intensity = Sparking).
uint16_t mode_fire_2012() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const unsigned strips = SEGMENT.nrOfVStrips();
if (!SEGENV.allocateData(strips * SEGLEN)) return mode_static(); //allocation failed
byte* heat = SEGENV.data;
Expand Down Expand Up @@ -2436,7 +2436,7 @@ static const char _data_FX_MODE_METEOR[] PROGMEM = "Meteor@!,Trail,,,,Gradient,,

//Railway Crossing / Christmas Fairy lights
uint16_t mode_railway() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned dur = (256 - SEGMENT.speed) * 40;
uint16_t rampdur = (dur * SEGMENT.intensity) >> 8;
if (SEGENV.step > dur)
Expand Down Expand Up @@ -2727,7 +2727,7 @@ uint16_t mode_halloween_eyes()
uint32_t blinkEndTime;
};

if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const unsigned maxWidth = strip.isMatrix ? SEG_W : SEGLEN;
const unsigned HALLOWEEN_EYE_SPACE = MAX(2, strip.isMatrix ? SEG_W>>4: SEGLEN>>5);
const unsigned HALLOWEEN_EYE_WIDTH = HALLOWEEN_EYE_SPACE/2;
Expand Down Expand Up @@ -2912,7 +2912,7 @@ static const char _data_FX_MODE_TRI_STATIC_PATTERN[] PROGMEM = "Solid Pattern Tr

static uint16_t spots_base(uint16_t threshold)
{
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
if (!SEGMENT.check2) SEGMENT.fill(SEGCOLOR(1));

unsigned maxZones = SEGLEN >> 2;
Expand Down Expand Up @@ -2968,7 +2968,7 @@ typedef struct Ball {
* Bouncing Balls Effect
*/
uint16_t mode_bouncing_balls(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
//allocate segment data
const unsigned strips = SEGMENT.nrOfVStrips(); // adapt for 2D
const size_t maxNumBalls = 16;
Expand Down Expand Up @@ -3146,7 +3146,7 @@ static const char _data_FX_MODE_ROLLINGBALLS[] PROGMEM = "Rolling Balls@!,# of b
* Sinelon stolen from FASTLED examples
*/
static uint16_t sinelon_base(bool dual, bool rainbow=false) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
SEGMENT.fade_out(SEGMENT.intensity);
unsigned pos = beatsin16_t(SEGMENT.speed/10,0,SEGLEN-1);
if (SEGENV.call == 0) SEGENV.aux0 = pos;
Expand Down Expand Up @@ -3251,7 +3251,7 @@ typedef struct Spark {
* modified from https://github.com/kitesurfer1404/WS2812FX/blob/master/src/custom/Popcorn.h
*/
uint16_t mode_popcorn(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
//allocate segment data
unsigned strips = SEGMENT.nrOfVStrips();
unsigned usablePopcorns = maxNumPopcorn;
Expand Down Expand Up @@ -3426,7 +3426,7 @@ typedef struct particle {
} star;

uint16_t mode_starburst(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned maxData = FAIR_DATA_PER_SEG; //ESP8266: 256 ESP32: 640
unsigned segs = strip.getActiveSegmentsNum();
if (segs <= (strip.getMaxSegments() /2)) maxData *= 2; //ESP8266: 512 if <= 8 segs ESP32: 1280 if <= 16 segs
Expand Down Expand Up @@ -3545,7 +3545,7 @@ static const char _data_FX_MODE_STARBURST[] PROGMEM = "Fireworks Starburst@Chanc
*/
uint16_t mode_exploding_fireworks(void)
{
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const int cols = SEGMENT.is2D() ? SEG_W : 1;
const int rows = SEGMENT.is2D() ? SEG_H : SEGLEN;

Expand Down Expand Up @@ -3683,7 +3683,7 @@ static const char _data_FX_MODE_EXPLODING_FIREWORKS[] PROGMEM = "Fireworks 1D@Gr
*/
uint16_t mode_drip(void)
{
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
//allocate segment data
unsigned strips = SEGMENT.nrOfVStrips();
const int maxNumDrops = 4;
Expand Down Expand Up @@ -3779,7 +3779,7 @@ typedef struct Tetris {
} tetris;

uint16_t mode_tetrix(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned strips = SEGMENT.nrOfVStrips(); // allow running on virtual strips (columns in 2D segment)
unsigned dataSize = sizeof(tetris);
if (!SEGENV.allocateData(dataSize * strips)) return mode_static(); //allocation failed
Expand Down Expand Up @@ -4086,7 +4086,7 @@ static const char _data_FX_MODE_PACIFICA[] PROGMEM = "Pacifica@!,Angle;;!;;pal=5
* Mode simulates a gradual sunrise
*/
uint16_t mode_sunrise() {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
//speed 0 - static sun
//speed 1 - 60: sunrise time in minutes
//speed 60 - 120 : sunset time in minutes - 60;
Expand Down Expand Up @@ -4293,7 +4293,7 @@ static const char _data_FX_MODE_FLOW[] PROGMEM = "Flow@!,Zones;;!;;m12=1"; //ver
*/
uint16_t mode_chunchun(void)
{
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
SEGMENT.fade_out(254); // add a bit of trail
unsigned counter = strip.now * (6 + (SEGMENT.speed >> 4));
unsigned numBirds = 2 + (SEGLEN >> 3); // 2 + 1/8 of a segment
Expand Down Expand Up @@ -4344,7 +4344,7 @@ typedef struct Spotlight {
*/
uint16_t mode_dancing_shadows(void)
{
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned numSpotlights = map(SEGMENT.intensity, 0, 255, 2, SPOT_MAX_COUNT); // 49 on 32 segment ESP32, 17 on 16 segment ESP8266
bool initialize = SEGENV.aux0 != numSpotlights;
SEGENV.aux0 = numSpotlights;
Expand Down Expand Up @@ -4806,7 +4806,7 @@ static const char _data_FX_MODE_AURORA[] PROGMEM = "Aurora@!,!;1,2,3;!;;sx=24,pa
// 16 bit perlinmove. Use Perlin Noise instead of sinewaves for movement. By Andrew Tuline.
// Controls are speed, # of pixels, faderate.
uint16_t mode_perlinmove(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
SEGMENT.fade_out(255-SEGMENT.custom1);
for (int i = 0; i < SEGMENT.intensity/16 + 1; i++) {
unsigned locn = inoise16(strip.now*128/(260-SEGMENT.speed)+i*15000, strip.now*128/(260-SEGMENT.speed)); // Get a new pixel location from moving noise.
Expand Down Expand Up @@ -4842,7 +4842,7 @@ static const char _data_FX_MODE_WAVESINS[] PROGMEM = "Wavesins@!,Brightness vari
//////////////////////////////
// By: ldirko https://editor.soulmatelights.com/gallery/392-flow-led-stripe , modifed by: Andrew Tuline
uint16_t mode_FlowStripe(void) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
const int hl = SEGLEN * 10 / 13;
uint8_t hue = strip.now / (SEGMENT.speed+1);
uint32_t t = strip.now / (SEGMENT.intensity/8+1);
Expand Down Expand Up @@ -6596,7 +6596,7 @@ static const char _data_FX_MODE_MATRIPIX[] PROGMEM = "Matripix@!,Brightness;!,!;
// * MIDNOISE //
//////////////////////
uint16_t mode_midnoise(void) { // Midnoise. By Andrew Tuline.
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
// Changing xdist to SEGENV.aux0 and ydist to SEGENV.aux1.

um_data_t *um_data = getAudioData();
Expand Down Expand Up @@ -6687,7 +6687,7 @@ static const char _data_FX_MODE_NOISEMETER[] PROGMEM = "Noisemeter@Fade rate,Wid
// * PIXELWAVE //
//////////////////////
uint16_t mode_pixelwave(void) { // Pixelwave. By Andrew Tuline.
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
// even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment

if (SEGENV.call == 0) {
Expand Down Expand Up @@ -6755,7 +6755,7 @@ static const char _data_FX_MODE_PLASMOID[] PROGMEM = "Plasmoid@Phase,# of pixels
//////////////////////
// Puddles/Puddlepeak By Andrew Tuline. Merged by @dedehai
uint16_t mode_puddles_base(bool peakdetect) {
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
unsigned size = 0;
uint8_t fadeVal = map(SEGMENT.speed, 0, 255, 224, 254);
unsigned pos = hw_random16(SEGLEN); // Set a random starting position.
Expand Down Expand Up @@ -6805,7 +6805,7 @@ static const char _data_FX_MODE_PUDDLES[] PROGMEM = "Puddles@Fade rate,Puddle si
// * PIXELS //
//////////////////////
uint16_t mode_pixels(void) { // Pixels. By Andrew Tuline.
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();

if (!SEGENV.allocateData(32*sizeof(uint8_t))) return mode_static(); //allocation failed
uint8_t *myVals = reinterpret_cast<uint8_t*>(SEGENV.data); // Used to store a pile of samples because WLED frame rate and WLED sample rate are not synchronized. Frame rate is too low.
Expand Down Expand Up @@ -6833,7 +6833,7 @@ static const char _data_FX_MODE_PIXELS[] PROGMEM = "Pixels@Fade rate,# of pixels
// ** Blurz //
//////////////////////
uint16_t mode_blurz(void) { // Blurz. By Andrew Tuline.
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
// even with 1D effect we have to take logic for 2D segments for allocation as fill_solid() fills whole segment

um_data_t *um_data = getAudioData();
Expand Down Expand Up @@ -6897,7 +6897,7 @@ static const char _data_FX_MODE_DJLIGHT[] PROGMEM = "DJ Light@Speed;;;01f;m12=2,
// ** Freqmap //
////////////////////
uint16_t mode_freqmap(void) { // Map FFT_MajorPeak to SEGLEN. Would be better if a higher framerate.
if (SEGLEN == 1) return mode_static();
if (SEGLEN <= 1) return mode_static();
// Start frequency = 60 Hz and log10(60) = 1.78
// End frequency = MAX_FREQUENCY in Hz and lo10(MAX_FREQUENCY) = MAX_FREQ_LOG10

Expand Down
Loading

0 comments on commit aa2c4f8

Please sign in to comment.