Optimize non-blocking examples?

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
things
 
Posts: 21
Joined: Fri Nov 08, 2013 7:50 am

Optimize non-blocking examples?

Post by things »

I'm using some neopixels in a project that also uses a button. As such I needed a few pattern functions that were also non blocking. I've converted a few of the ones included in the strandtest example, however I don't feel they're running as quickly/efficiently as they could be. Any tips to perhaps speed these up a bit?

Code: Select all

uint16_t k, j = 0;
boolean iterated = false;
byte colwipe = 1;
uint32_t col;

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint16_t wait) {
  if(millis() - previousMillis > 40) {
    previousMillis = millis();  
    if (k < strip.numPixels()){
        strip.setPixelColor(k, c);
        strip.show();
        //delay(wait);
        k++;
    } else {
      k = 0;
      colwipe++;
     if (colwipe > 3){
        colwipe = 1;
      }
    }
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  if (j < 256) { 
    if (iterated) {
      j += 4;
      iterated = false;
    }
    if (k < strip.numPixels()) {
      strip.setPixelColor(k, hsv2rgb(((k * 256 / strip.numPixels()) + j), 255, 255));
      k++;
    } else {
      k = 0;
      iterated = true;
    }
    strip.show();
    if (iterated == true){
      delay(wait);
      //iterated = false;
    }
  } else {
    j = 0;
  }
}

void fade(uint8_t wait) { // Whole-strip fade
  if (j < 255) {
    if (k < strip.numPixels()){
      strip.setPixelColor(k, col);
      k++;
    } else {
      k = 0;
      col = hsv2rgb(j, 255, 255);
      j += 4;
    }
    strip.show();
    delay(wait);
  } else {
    j = 0;
  }
}

uint32_t hsv2rgb(uint8_t hi, uint8_t s, uint8_t v) {
  uint8_t n, r, g, b;
  int16_t h;
  h = hi * 6;
  // Hue circle = 0 to 1530 (NOT 1536!)
  h %= 1530;           // -1529 to +1529
  if(h < 0) h += 1530; //     0 to +1529
  n  = h % 255;        // Angle within sextant; 0 to 254 (NOT 255!)
  switch(h / 255) {    // Sextant number; 0 to 5
   case 0 : r = 255    ; g =   n    ; b =   0    ; break; // R to Y
   case 1 : r = 254 - n; g = 255    ; b =   0    ; break; // Y to G
   case 2 : r =   0    ; g = 255    ; b =   n    ; break; // G to C
   case 3 : r =   0    ; g = 254 - n; b = 255    ; break; // C to B
   case 4 : r =   n    ; g =   0    ; b = 255    ; break; // B to M
   default: r = 255    ; g =   0    ; b = 254 - n; break; // M to R
  }
 
  uint32_t v1 =   1 + v; // 1 to 256; allows >>8 instead of /255
  uint16_t s1 =   1 + s; // 1 to 256; same reason
  uint8_t  s2 = 255 - s; // 255 to 0
  return ((((((r * s1) >> 8) + s2) * v1) & 0xff00) << 8) | // MAF!
          (((((g * s1) >> 8) + s2) * v1) & 0xff00)       |
         ( ((((b * s1) >> 8) + s2) * v1)           >> 8);
}
Or if you know of any other pattern code that's also non blocking, I'd love to see it :)

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Optimize non-blocking examples?

Post by adafruit_support_bill »


things
 
Posts: 21
Joined: Fri Nov 08, 2013 7:50 am

Re: Optimize non-blocking examples?

Post by things »

Wow, that would have saved me so much time when I initially modified these. Was sometime last year but only just putting them to practical use now, but that method looks a lot cleaner. Thanks :D

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”