Advertisement
kriegsman

XY Matrix sample (draft)

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