acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

user_xmas.cpp (2512B)

      1 #if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
      2 
      3 // Christmas demo for eye + NeoPixels. Randomly sets pixels in holiday-themed colors.
      4 
      5 #include <Adafruit_NeoPixel.h>
      6 
      7 // Pin 8 is the built-in NeoPixels on Circuit Playground Express & Bluetooth.
      8 // With a TFT Gizmo attached, you can use A1 or A2 to easily connect a strand.
      9 #define LED_PIN          8
     10 #define LED_COUNT        10
     11 #define LED_BRIGHTNESS   50 // about 1/5 brightness (max = 255)
     12 #define TWINKLE_INTERVAL 333 // Every 333 ms (1/3 second), change a pixel
     13 #define LIT_PIXELS       (LED_COUNT / 3) // Must be LESS than LED_COUNT/2
     14 
     15 Adafruit_NeoPixel pixels(LED_COUNT, LED_PIN);
     16 
     17 
     18 uint32_t timeOfLastTwinkle = 0;  // Used for timing pixel changes
     19 uint8_t litPixel[LIT_PIXELS];    // Indices of which pixels are lit
     20 uint8_t pixelIndex = LIT_PIXELS; // Index of currently-changing litPixel
     21 
     22 uint32_t colors[] = { 0xFF0000, 0x00FF00, 0xFFFFFF }; // Red, green, white
     23 #define NUM_COLORS (sizeof colors / sizeof colors[0])
     24 
     25 void user_setup(void) {
     26   pixels.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
     27   pixels.show();            // Turn OFF all pixels ASAP
     28   pixels.setBrightness(LED_BRIGHTNESS);
     29   memset(litPixel, 255, sizeof litPixel); // Fill with out-of-range nonsense
     30 }
     31 
     32 void user_loop(void) {
     33   uint32_t t = millis();
     34 
     35   if((t - timeOfLastTwinkle) >= TWINKLE_INTERVAL) { // Time to update pixels?
     36     timeOfLastTwinkle = t;
     37     if(++pixelIndex >= LIT_PIXELS) pixelIndex = 0;
     38 
     39     // Pick a NEW pixel that's not currently lit and not adjacent to a lit one.
     40     // This just brute-force randomly tries pixels until a valid one is found,
     41     // no mathematical cleverness. Should only take a few iterations and won't
     42     // significantly slow down the eyes.
     43     int newPixel, pixelAfter, pixelBefore;
     44     do {
     45       newPixel    = random(LED_COUNT);
     46       pixelAfter  = (newPixel + 1) % LED_COUNT;
     47       pixelBefore = (newPixel - 1);
     48       if(pixelBefore < 0) pixelBefore = LED_COUNT - 1;
     49     } while(pixels.getPixelColor(newPixel)   ||
     50             pixels.getPixelColor(pixelAfter) ||
     51             pixels.getPixelColor(pixelBefore));
     52 
     53     // Turn OFF litPixel[pixelIndex]
     54     pixels.setPixelColor(litPixel[pixelIndex], 0);
     55     // 'newPixel' is the winner. Save in the litPixel[] array for later...
     56     litPixel[pixelIndex] = newPixel;
     57     // Turn ON newPixel with a random color from the colors[] list.
     58     pixels.setPixelColor(newPixel, colors[random(NUM_COLORS)]);
     59 
     60     pixels.show();
     61   }
     62 }
     63 
     64 #endif // 0