|
| 1 | +// NeoPixelCylon |
| 2 | +// This example will move a Cylong Red Eye back and forth across the |
| 3 | +// the full collection of pixels on the strip. |
| 4 | +// |
| 5 | +// This will demonstrate the use of the NeoEase animation ease methods; that provide |
| 6 | +// simulated acceleration to the animations. |
| 7 | +// |
| 8 | +// |
| 9 | + |
| 10 | +#include <NeoPixelBus.h> |
| 11 | +#include <NeoPixelAnimator.h> |
| 12 | + |
| 13 | +const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip |
| 14 | +const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266 |
| 15 | +const RgbColor CylonEyeColor(HtmlColor(0x7f0000)); |
| 16 | + |
| 17 | +NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin); |
| 18 | + |
| 19 | +NeoPixelAnimator animations(2); // only ever need 2 animations |
| 20 | + |
| 21 | +uint16_t lastPixel = 0; // track the eye position |
| 22 | +int8_t moveDir = 1; // track the direction of movement |
| 23 | + |
| 24 | +// uncomment one of the lines below to see the effects of |
| 25 | +// changing the ease function on the movement animation |
| 26 | +AnimEaseFunction moveEase = |
| 27 | +// NeoEase::Linear; |
| 28 | +// NeoEase::QuadraticInOut; |
| 29 | +// NeoEase::CubicInOut; |
| 30 | + NeoEase::QuarticInOut; |
| 31 | +// NeoEase::QuinticInOut; |
| 32 | +// NeoEase::SinusoidalInOut; |
| 33 | +// NeoEase::ExponentialInOut; |
| 34 | +// NeoEase::CircularInOut; |
| 35 | + |
| 36 | +void FadeAll(uint8_t darkenBy) |
| 37 | +{ |
| 38 | + RgbColor color; |
| 39 | + for (uint16_t indexPixel = 0; indexPixel < strip.PixelCount(); indexPixel++) |
| 40 | + { |
| 41 | + color = strip.GetPixelColor(indexPixel); |
| 42 | + color.Darken(darkenBy); |
| 43 | + strip.SetPixelColor(indexPixel, color); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void FadeAnimUpdate(const AnimationParam& param) |
| 48 | +{ |
| 49 | + if (param.state == AnimationState_Completed) |
| 50 | + { |
| 51 | + FadeAll(10); |
| 52 | + animations.RestartAnimation(param.index); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void MoveAnimUpdate(const AnimationParam& param) |
| 57 | +{ |
| 58 | + // apply the movement animation curve |
| 59 | + float progress = moveEase(param.progress); |
| 60 | + |
| 61 | + // use the curved progress to calculate the pixel to effect |
| 62 | + uint16_t nextPixel; |
| 63 | + if (moveDir > 0) |
| 64 | + { |
| 65 | + nextPixel = progress * PixelCount; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + nextPixel = (1.0f - progress) * PixelCount; |
| 70 | + } |
| 71 | + |
| 72 | + // if progress moves fast enough, we may move more than |
| 73 | + // one pixel, so we update all between the calculated and |
| 74 | + // the last |
| 75 | + if (lastPixel != nextPixel) |
| 76 | + { |
| 77 | + for (uint16_t i = lastPixel + moveDir; i != nextPixel; i += moveDir) |
| 78 | + { |
| 79 | + strip.SetPixelColor(i, CylonEyeColor); |
| 80 | + } |
| 81 | + } |
| 82 | + strip.SetPixelColor(nextPixel, CylonEyeColor); |
| 83 | + |
| 84 | + lastPixel = nextPixel; |
| 85 | + |
| 86 | + if (param.state == AnimationState_Completed) |
| 87 | + { |
| 88 | + // reverse direction of movement |
| 89 | + moveDir *= -1; |
| 90 | + |
| 91 | + // done, time to restart this position tracking animation/timer |
| 92 | + animations.RestartAnimation(param.index); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void SetupAnimations() |
| 97 | +{ |
| 98 | + // fade all pixels providing a tail that is longer the faster |
| 99 | + // the pixel moves. |
| 100 | + animations.StartAnimation(0, 5, FadeAnimUpdate); |
| 101 | + |
| 102 | + // take several seconds to move eye fron one side to the other |
| 103 | + animations.StartAnimation(1, 2000, MoveAnimUpdate); |
| 104 | +} |
| 105 | + |
| 106 | +void setup() |
| 107 | +{ |
| 108 | + strip.Begin(); |
| 109 | + strip.Show(); |
| 110 | + |
| 111 | + SetupAnimations(); |
| 112 | +} |
| 113 | + |
| 114 | +void loop() |
| 115 | +{ |
| 116 | + // this is all that is needed to keep it running |
| 117 | + // and avoiding using delay() is always a good thing for |
| 118 | + // any timing related routines |
| 119 | + animations.UpdateAnimations(); |
| 120 | + strip.Show(); |
| 121 | +} |
0 commit comments