Advertisement
Guest User

Untitled

a guest
Dec 30th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1.  
  2. #include <FastLED.h>
  3.  
  4. #define LED_PIN  5
  5. #define COLOR_ORDER GRB
  6. #define CHIPSET     WS2811
  7.  
  8. #define BRIGHTNESS 32
  9.  
  10. const uint8_t kMatrixWidth  = 16;
  11. const uint8_t kMatrixHeight = 16;
  12. const bool    kMatrixSerpentineLayout = true;
  13.  
  14. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  15. CRGB leds[NUM_LEDS];
  16.  
  17. void setup() {
  18.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  19.   FastLED.setBrightness( BRIGHTNESS );
  20. }
  21.  
  22. void loop()
  23. {
  24.     uint32_t ms = millis();
  25.     int32_t yHueDelta32 = ((int32_t)cos16( ms * 27 ) * (350 / kMatrixWidth));
  26.     int32_t xHueDelta32 = ((int32_t)cos16( ms * 39 ) * (310 / kMatrixHeight));
  27.     DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768);
  28.     FastLED.show();
  29. }
  30.  
  31. void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8)
  32. {
  33.   byte lineStartHue = startHue8;
  34.   for( byte y = 0; y < kMatrixHeight; y++) {
  35.     lineStartHue += yHueDelta8;
  36.     byte pixelHue = lineStartHue;      
  37.     for( byte x = 0; x < kMatrixWidth; x++) {
  38.       pixelHue += xHueDelta8;
  39.       leds[ XY(x, y)]  = CHSV( pixelHue, 255, 255);
  40.     }
  41.   }
  42. }
  43.  
  44. // Helper function that translates from x, y into an index into the LED array
  45. // Handles both 'row order' and 'serpentine' pixel layouts.
  46. uint16_t XY( uint8_t x, uint8_t y)
  47. {
  48.   uint16_t i;
  49.  
  50.   if( kMatrixSerpentineLayout == false) {
  51.     i = (y * kMatrixWidth) + x;
  52.   } else {
  53.     if( y & 0x01) {
  54.       // Odd rows run backwards
  55.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  56.       i = (y * kMatrixWidth) + reverseX;
  57.     } else {
  58.       // Even rows run forwards
  59.       i = (y * kMatrixWidth) + x;
  60.     }
  61.   }
  62.  
  63.   return i;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement