Advertisement
Guest User

particle photon led test

a guest
Dec 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1.  
  2. // This #include statement was automatically added by the Particle IDE.
  3. #include <neopixel.h>
  4. #include <math.h>
  5.  
  6. void spin(int R, int G, int B);
  7.  
  8. // IMPORTANT: Set pixel COUNT, PIN and TYPE
  9. #define PIXEL_COUNT 7
  10. #define PIXEL_TYPE WS2812
  11.  
  12. #define RED 155,10,10
  13. #define GREEN 10,155,10
  14. #define BLUE 5,5,155
  15.  
  16. const float STEPS = 1.0f / PIXEL_COUNT;
  17.  
  18. float fRand()
  19. {
  20.     const int max = 0xFF;
  21.     float val = ((float)random(max)) / max;
  22.     return val;
  23. }
  24.  
  25. class Color {
  26. private:
  27.     const float gamma = 1.0f / 2.2f;
  28. public:
  29.     Color()
  30.     {
  31.     }
  32.    
  33.     Color(float r, float g, float b)
  34.     :r(r),g(g),b(b)
  35.     {
  36.     }
  37.    
  38.     float r;
  39.     float g;
  40.     float b;
  41.    
  42.     int red(bool gammacorrect = false) const
  43.     {
  44.         float value = pow(r, gammacorrect ? gamma : 1.0f);
  45.         return (int)(value * 0xFF) % 0xFF;
  46.     }
  47.     int green(bool gammacorrect = false) const
  48.     {
  49.         float value = pow(g, gammacorrect ? gamma : 1.0f);
  50.         return (int)(value * 0xFF) % 0xFF;
  51.     }
  52.     int blue(bool gammacorrect = false) const
  53.     {
  54.         float value = pow(b, gammacorrect ? gamma : 1.0f);
  55.         return (int)(value * 0xFF) % 0xFF;
  56.     }
  57.    
  58.     void set(float r, float g, float b)
  59.     {
  60.         this->r = r;
  61.         this->g = g;
  62.         this->b = b;
  63.     }
  64.    
  65.     void add(const Color& other, float amount = 1.0f)
  66.     {
  67.         this->r += other.r * amount;
  68.         this->g += other.g * amount;
  69.         this->b += other.b * amount;
  70.     }
  71.    
  72.     void randomize()
  73.     {
  74.         delay(1);
  75.         r = fRand();
  76.         g = fRand();
  77.         b = fRand();
  78.     }
  79.    
  80.     Color& operator=(const Color& other)
  81.     {
  82.         r = other.r;
  83.         g = other.g;
  84.         b = other.b;
  85.         return *this;
  86.     }
  87. };
  88.  
  89. class Block {
  90. public:
  91.     Adafruit_NeoPixel strip;
  92.     int at = 0;
  93.     int position = 0;
  94.     Color color;
  95.     Color pixels[PIXEL_COUNT];
  96.    
  97.     Block(int port)
  98.     : strip(PIXEL_COUNT, port, PIXEL_TYPE)
  99.     {
  100.     }
  101.    
  102.     void init()
  103.     {
  104.         //color.randomize();
  105.         strip.begin();
  106.         for(int i=0; i < PIXEL_COUNT; i++) {
  107.             pixels[i] = color;
  108.         }
  109.     }
  110.    
  111.     void update()
  112.     {
  113.         static const int livePixelCount = PIXEL_COUNT -1;
  114.         at++;
  115.         const float fade = 0.05f;
  116.        
  117.         for(int i=1; i < PIXEL_COUNT; i++) {
  118.             Color& pixel = pixels[i];
  119.            
  120.             pixel.r -= pixel.r * 0.25f;
  121.             pixel.g -= pixel.g * 0.25f;
  122.             pixel.b -= pixel.b * 0.25f;
  123.             /*
  124.             pixel.r -= fade;
  125.             pixel.g -= fade;
  126.             pixel.b -= fade;
  127.             if(pixel.r < 0.0f ) pixel.r = 0.0f;
  128.             if(pixel.g < 0.0f ) pixel.g = 0.0f;
  129.             if(pixel.b < 0.0f ) pixel.b = 0.0f;
  130.             */
  131.         }
  132.        
  133.         color.r -= color.r * 0.01f;
  134.         color.g -= color.g * 0.01f;
  135.         color.b -= color.b * 0.01f;
  136.        
  137.         if((at % 600) == 0) {
  138.             color.randomize();
  139.         }
  140.        
  141.         pixels[0] = color;
  142.         if(at % 4 == 0) {
  143.             ++position;
  144.             pixels[(position % livePixelCount) + 1].add(color);
  145.         }
  146.     }
  147.    
  148.     void draw()
  149.     {
  150.         int r,g,b;
  151.         for(int i=0; i < PIXEL_COUNT; i++) {
  152.             const Color& pixel = pixels[i];
  153.            
  154.             strip.setColor(i, pixel.red(true) , pixel.green(true), pixel.blue(true));
  155.         }
  156.         strip.show();
  157.        
  158.         //strip.setBrightness(j);
  159.     }
  160. };
  161.  
  162. Block strip = Block(D4);
  163. Block strip2 = Block(D5);
  164. Block strip3 = Block(D6);
  165.  
  166. void setup() {
  167.     strip.color.set(1.0f, 0.0f, 0.0f);
  168.     strip2.color.set(0.0f, 1.0f, 0.0f);
  169.     strip3.color.set(0.0f, 0.0f, 1.0f);
  170.    
  171.     strip.init();
  172.     strip2.init();
  173.     strip3.init();
  174.    
  175.     uint32_t seed = millis();
  176.     randomSeed(seed);
  177. }
  178.  
  179. void loop() {
  180.     strip.update();
  181.     strip2.update();
  182.     strip3.update();
  183.    
  184.     strip.draw();
  185.     strip2.draw();
  186.     strip3.draw();
  187.    
  188.     delay(4);
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement