|
| 1 | +/***************************************************** |
| 2 | +
|
| 3 | +quickPatterns demo reel - patterns to demonstrate some of the options available in the quickPatterns library |
| 4 | +https://github.com/brimshot/quickPatterns |
| 5 | +
|
| 6 | +MIT License |
| 7 | +
|
| 8 | +Copyright (c) 2021 Chris Brim |
| 9 | +
|
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +of this software and associated documentation files (the "Software"), to deal |
| 12 | +in the Software without restriction, including without limitation the rights |
| 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +copies of the Software, and to permit persons to whom the Software is |
| 15 | +furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included in all |
| 18 | +copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | +SOFTWARE. |
| 27 | +*****************************************************/ |
| 28 | + |
| 29 | +#ifdef CORE_TEENSY |
| 30 | +#define FASTLED_ALLOW_INTERRUPTS 0 |
| 31 | +#endif |
| 32 | + |
| 33 | +#include <quickPatterns.h> |
| 34 | + |
| 35 | +////////////////////////////////// |
| 36 | +// HARDWARE CONSTANTS |
| 37 | +////////////////////////////////// |
| 38 | + |
| 39 | +#define CHIPSET WS2812B |
| 40 | +#define DATA_PIN 2 |
| 41 | +#define NUM_LEDS 150 |
| 42 | +#define BRIGHTNESS 64 |
| 43 | +#define COLOR_ORDER GRB //GRB for WS2812, RGB for WS2811 |
| 44 | + |
| 45 | + |
| 46 | +////////////////////////////////// |
| 47 | +// CUSTOM PATTERNS FOR THIS SKETCH |
| 48 | +// |
| 49 | +// For information on writing custom patterns for use with quickPatterns see: https://github.com/brimshot/quickPatterns |
| 50 | +// |
| 51 | +////////////////////////////////// |
| 52 | + |
| 53 | +// A juggle effect that slowly increases speed over time |
| 54 | +class increasingJuggle : public qpPattern { |
| 55 | + |
| 56 | + private: |
| 57 | + const int MAX_SPEED = 2; |
| 58 | + const int FRAMES_TO_SHOW_AT_MAX_SPEED = 200; |
| 59 | + |
| 60 | + int _startingSpeed; |
| 61 | + int speed; |
| 62 | + int velocity = 0; |
| 63 | + int framesRenderedAtMaxSpeed = 0; |
| 64 | + |
| 65 | + void reset() |
| 66 | + { |
| 67 | + speed = _startingSpeed; |
| 68 | + velocity = framesRenderedAtMaxSpeed = 0; |
| 69 | + } |
| 70 | + |
| 71 | + public: |
| 72 | + |
| 73 | + increasingJuggle(int startingSpeed) |
| 74 | + { |
| 75 | + _startingSpeed = speed = startingSpeed; |
| 76 | + } |
| 77 | + |
| 78 | + void draw() { |
| 79 | + |
| 80 | + // Speed limiter |
| 81 | + if(this->frames % speed) |
| 82 | + return; |
| 83 | + |
| 84 | + // Do juggle |
| 85 | + for( int i = 0; i < 8; i++) |
| 86 | + _targetLeds[beatsin16(i+7, 0, _numLeds - 1)] = _getColor(); |
| 87 | + |
| 88 | + // Increase velocity on each render |
| 89 | + velocity += 1; |
| 90 | + if(velocity%10 == 0) |
| 91 | + speed = max(--speed, MAX_SPEED); |
| 92 | + |
| 93 | + // Stay at max speed for 200 frames then start over |
| 94 | + if(speed == MAX_SPEED) { |
| 95 | + if(++framesRenderedAtMaxSpeed == 200) { |
| 96 | + reset(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +}; |
| 103 | + |
| 104 | + |
| 105 | +// Sends segments of lights down the strip one by one and stacks them at the end |
| 106 | +class segmentStacker : public qpPattern { |
| 107 | + |
| 108 | + private: |
| 109 | + const int FRAMES_TO_SHOW_WITH_FULL_STACK = 200; |
| 110 | + |
| 111 | + int stackSize = 0; |
| 112 | + int segmentSize = 0; |
| 113 | + int segmentPos = 0; |
| 114 | + int framesWithFullStack = 0; |
| 115 | + |
| 116 | + void reset() { segmentPos = stackSize = framesWithFullStack = 0; } |
| 117 | + |
| 118 | + public: |
| 119 | + |
| 120 | + segmentStacker(int segmentSize) : segmentSize(segmentSize) {} |
| 121 | + |
| 122 | + void draw() { |
| 123 | + |
| 124 | + // -. Quick note: we do NOT clear the leds here at the beginning of each loop since we are expecting other patterns on the same layer to do it for us |
| 125 | + |
| 126 | + // Draw stack |
| 127 | + for(int i = (_numLeds-1); i > ((_numLeds-1) - stackSize); i--) { |
| 128 | + _targetLeds[i] = _getColor(); |
| 129 | + } |
| 130 | + |
| 131 | + // Pause or reset at end |
| 132 | + if(stackSize >= (_numLeds-1)) { |
| 133 | + if(framesWithFullStack < FRAMES_TO_SHOW_WITH_FULL_STACK) { |
| 134 | + framesWithFullStack++; |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + reset(); |
| 139 | + _countCycle(); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + // Draw segment |
| 144 | + for(int i = segmentPos; i < (segmentPos + segmentSize); i++) { |
| 145 | + _targetLeds[i] = _getColor(); |
| 146 | + } |
| 147 | + |
| 148 | + // Advance segment pos for next frame |
| 149 | + segmentPos++; |
| 150 | + |
| 151 | + // If we bumped into our stack, add it |
| 152 | + if((segmentPos + segmentSize) >= ((_numLeds-1) - stackSize)) { |
| 153 | + stackSize += segmentSize; |
| 154 | + segmentPos = 0; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | +}; |
| 160 | + |
| 161 | + |
| 162 | +////////////////////////////////// |
| 163 | +// MAIN SKETCH BEGINS HERE |
| 164 | +////////////////////////////////// |
| 165 | + |
| 166 | +// Declare array of CRGB objects for use with FastLED |
| 167 | +CRGB leds[NUM_LEDS]; |
| 168 | + |
| 169 | +// declare quickPatterns controller and pass in main led array |
| 170 | +quickPatterns quickPatterns(leds, NUM_LEDS); |
| 171 | + |
| 172 | +void setup() { |
| 173 | + |
| 174 | + delay(3000); // Recovery delay |
| 175 | + |
| 176 | + random16_add_entropy(analogRead(0)); |
| 177 | + |
| 178 | + // ~ Configure FastLED |
| 179 | + |
| 180 | + FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS) |
| 181 | + .setCorrection(TypicalLEDStrip) |
| 182 | + .setDither(BRIGHTNESS < 255); |
| 183 | + |
| 184 | + FastLED.setBrightness(BRIGHTNESS); |
| 185 | + FastLED.setMaxPowerInVoltsAndMilliamps(5, 450); |
| 186 | + |
| 187 | + FastLED.clear(); |
| 188 | + |
| 189 | + |
| 190 | + /////////// quickPatterns setup |
| 191 | + |
| 192 | + // ----- Scene 1: Rainbow base layers with sparkling highlights that increase in speed as they shine |
| 193 | + |
| 194 | + quickPatterns.newScene().addPattern(new qpPaletteGradient(2)) |
| 195 | + .usePalette(RainbowColors_p) |
| 196 | + .drawEveryNTicks(2); |
| 197 | + |
| 198 | + // Decoration layer: juggles that increase in speed then reset after a short time |
| 199 | + quickPatterns.sameScene().addPattern(new increasingJuggle(12)) |
| 200 | + .singleColor(CRGB(128, 128, 128)); |
| 201 | + quickPatterns.sameLayer().setLayerBrush(ADD).continuallyFadeLayerBy(20); |
| 202 | + |
| 203 | + // ----- End Scene 1 |
| 204 | + |
| 205 | + // ----- Scene 2: Twinkling rainbow that is slowly revealed by stacking a visible window segment by segment |
| 206 | + |
| 207 | + quickPatterns.newScene().addPattern(new qpPaletteTwinkle(12)) |
| 208 | + .usePalette(RainbowColors_p); |
| 209 | + |
| 210 | + quickPatterns.sameScene().addPattern(new qpFill()) |
| 211 | + .singleColor(CRGB::Black); |
| 212 | + |
| 213 | + quickPatterns.sameLayer().addPattern(new segmentStacker(10)) |
| 214 | + .singleColor(CRGB::White); |
| 215 | + quickPatterns.sameLayer().setLayerBrush(MASK); |
| 216 | + |
| 217 | + // ----- End Scene 2 |
| 218 | + |
| 219 | + |
| 220 | + // ----- Scene 3: three overlapping layers at different speeds |
| 221 | + |
| 222 | + quickPatterns.newScene().addPattern(new qpBouncingBars(9)) // blue bars, 9 pixels width |
| 223 | + .singleColor(CRGB::Blue); |
| 224 | + |
| 225 | + quickPatterns.sameScene().addPattern(new qpBouncingBars(5)) // short red bars, 5 pixels |
| 226 | + .singleColor(CRGB::Red); |
| 227 | + quickPatterns.sameLayer().setLayerBrush(ADD); //when passing over underlying pixels, add their light together |
| 228 | + |
| 229 | + quickPatterns.sameScene().addPattern(new qpBouncingBars(5)) // slow yellow bars, 5 pixels |
| 230 | + .singleColor(CRGB::Yellow) |
| 231 | + .drawEveryNTicks(3); //move at a slower speed |
| 232 | + quickPatterns.sameLayer().setLayerBrush(COMBINE); |
| 233 | + |
| 234 | + // ----- End Scene 3 |
| 235 | + |
| 236 | + |
| 237 | + // ----- Scene 4: Linked activations and deactivations |
| 238 | + |
| 239 | + qpPattern &Sinelon = quickPatterns.newScene().addPattern(new qpSinelon(16)) |
| 240 | + .chooseColorFromPalette(RainbowColors_p, SEQUENTIAL) |
| 241 | + .changeColorEveryNTicks(2); |
| 242 | + quickPatterns.sameLayer().continuallyFadeLayerBy(20); |
| 243 | + |
| 244 | + qpPattern &WhiteLightning = quickPatterns.sameScene().addPattern(new qpLightning(12)) |
| 245 | + .singleColor(CRGB::White) |
| 246 | + .activateWhenPatternPHasCompletedNCycles(Sinelon, 2) |
| 247 | + .stayActiveForNCycles(2); |
| 248 | + |
| 249 | + quickPatterns.sameScene().addPattern(new qpLightning(20)) |
| 250 | + .singleColor(CRGB(200, 0, 180)) |
| 251 | + .activateWhenPatternPHasDeactivatedNTimes(WhiteLightning, 3) //After pattern p has activated 3 times... possible? |
| 252 | + .stayActiveForNCycles(4); |
| 253 | + |
| 254 | + // ----- End Scene 4 |
| 255 | + |
| 256 | + |
| 257 | +} |
| 258 | + |
| 259 | +void loop() |
| 260 | +{ |
| 261 | + quickPatterns.show(); |
| 262 | + |
| 263 | + EVERY_N_MINUTES(1) { |
| 264 | + quickPatterns.nextScene(); |
| 265 | + } |
| 266 | +} |
0 commit comments