I’m going through some of my dynamic patterns looking for things that could (or should) be rewritten. Changing a lot of my variables from simple INT to (u)int8_t (or 16 or 32) as that saves a few bytes every time (which over time adds up!) But, mainly I’m doing it both as an optimization exercise but also to write code that a) someone else would easily understand, b) is easily maintained, and c) is actually proper.
So I’m looking a the first image and it’s code. This is generated like this:
void psychedelicPower() {
static uint8_t cntr = 0;
static uint8_t hue = 0;
static int8_t increment = 1;
fill_solid(&(leds[32 + cntr]), 3, CRGB::DarkBlue);
fill_solid(&(leds[27 + cntr]), 4, CRGB::Maroon);
fill_solid(&(leds[23 + cntr]), 4, CRGB::Black);
fill_solid(&(leds[23 - cntr]), 2 * cntr, CHSV(hue++, 255, 255));
fill_solid(&(leds[19 - cntr]), 4, CRGB::Black);
fill_solid(&(leds[0]), 19 - cntr, CRGB::DarkBlue);
LEDS.show();
fill_solid(&(leds[0]), NUM_LEDS, CRGB::Black); // reset string
cntr = cntr + increment;
if (cntr > 12) increment = -increment;
if (cntr < 1) increment = -increment;
}
All I’m doing is filling the string with specific colors at specific spots and using a counter to track where that specific color needs to be. I have to believe there’s a better way to do this.
Even knowing what’s going on, every time I look at it, it takes me a bit to remember where each pixel is, where I need to start the next color, etc., etc.
The second image is a design that generates diamond shapes with an inner and outer outline (of opposite hue). That calculation is done by first deciding where along the string the center of the diamond is, then subtracting whatever amount of pixels to get to the starting point of the diamond’s edge, and fill the string n-pixels, basically fill from (center-of-diamond - n) to ((center-of-diamond - n) + 2n). The inner and outer outlines just follow along the diamond edges.
void diamonds() {
static int8_t cntr = 0;
static uint8_t hue = 0;
static int8_t increment = 1;
fill_solid(&(leds[23 + cntr]), 6, CHSV(hue + 127, 255, 255));
fill_solid(&(leds[23 - cntr]), 2 * cntr, CHSV(hue, 255, 255));
fill_solid(&(leds[17 - cntr]), 6, CHSV(hue + 127, 255, 255));
LEDS.show();
fill_solid(&(leds[0]), NUM_LEDS, CRGB::Black);
cntr += increment;
if (cntr > 12) increment = -increment;
if (cntr < 1) {
hue += 15;
increment = -increment;
}
}
So, is there a better way to do this?