d333gs

X,Y

Nov 24th, 2020
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4.  
  5. #define COLOR_ORDER GRB
  6. #define CHIPSET WS2812
  7. #define NUM_LEDS 512
  8. #define BRIGHTNESS 64
  9.  
  10. // Helper functions for an two-dimensional XY matrix of pixels.
  11. // Simple 2-D demo code is included as well.
  12. //
  13. // XY(x,y) takes x and y coordinates and returns an LED index number,
  14. // for use like this: leds[ XY(x,y) ] == CRGB::Red;
  15. // No error checking is performed on the ranges of x and y.
  16. //
  17. // XYsafe(x,y) takes x and y coordinates and returns an LED index number,
  18. // for use like this: leds[ XY(x,y) ] == CRGB::Red;
  19. // Error checking IS performed on the ranges of x and y, and an
  20. // index of "-1" is returned. Special instructions below
  21. // explain how to use this without having to do your own error
  22. // checking every time you use this function.
  23. // This is a slightly more advanced technique, and
  24. // it REQUIRES SPECIAL ADDITIONAL setup, described below.
  25.  
  26.  
  27. // Params for width and height
  28. const uint8_t kMatrixWidth = 16;
  29. const uint8_t kMatrixHeight = 16;
  30.  
  31. // Param for different pixel layouts
  32. const bool kMatrixSerpentineLayout = true;
  33. const bool kMatrixVertical = false;
  34. // Set 'kMatrixSerpentineLayout' to false if your pixels are
  35. // laid out all running the same way, like this:
  36. //
  37. // 0 > 1 > 2 > 3 > 4
  38. // |
  39. // .----<----<----<----'
  40. // |
  41. // 5 > 6 > 7 > 8 > 9
  42. // |
  43. // .----<----<----<----'
  44. // |
  45. // 10 > 11 > 12 > 13 > 14
  46. // |
  47. // .----<----<----<----'
  48. // |
  49. // 15 > 16 > 17 > 18 > 19
  50. //
  51. // Set 'kMatrixSerpentineLayout' to true if your pixels are
  52. // laid out back-and-forth, like this:
  53. //
  54. // 0 > 1 > 2 > 3 > 4
  55. // |
  56. // |
  57. // 9 < 8 < 7 < 6 < 5
  58. // |
  59. // |
  60. // 10 > 11 > 12 > 13 > 14
  61. // |
  62. // |
  63. // 19 < 18 < 17 < 16 < 15
  64. //
  65. // Bonus vocabulary word: anything that goes one way
  66. // in one row, and then backwards in the next row, and so on
  67. // is call "boustrophedon", meaning "as the ox plows."
  68.  
  69.  
  70. // This function will return the right 'led index number' for
  71. // a given set of X and Y coordinates on your matrix.
  72. // IT DOES NOT CHECK THE COORDINATE BOUNDARIES.
  73. // That's up to you. Don't pass it bogus values.
  74. //
  75. // Use the "XY" function like this:
  76. //
  77. // for( uint8_t x = 0; x < kMatrixWidth; x++) {
  78. // for( uint8_t y = 0; y < kMatrixHeight; y++) {
  79. //
  80. // // Here's the x, y to 'led index' in action:
  81. // leds[ XY( x, y) ] = CHSV( random8(), 255, 255);
  82. //
  83. // }
  84. // }
  85. //
  86. //
  87. uint16_t XY( uint8_t x, uint8_t y)
  88. {
  89. uint16_t i;
  90.  
  91. if( kMatrixSerpentineLayout == false) {
  92. if (kMatrixVertical == false) {
  93. i = (y * kMatrixWidth) + x;
  94. } else {
  95. i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
  96. }
  97. }
  98.  
  99. if( kMatrixSerpentineLayout == true) {
  100. if (kMatrixVertical == false) {
  101. if( y & 0x01) {
  102. // Odd rows run backwards
  103. uint8_t reverseX = (kMatrixWidth - 1) - x;
  104. i = (y * kMatrixWidth) + reverseX;
  105. } else {
  106. // Even rows run forwards
  107. i = (y * kMatrixWidth) + x;
  108. }
  109. } else { // vertical positioning
  110. if ( x & 0x01) {
  111. i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
  112. } else {
  113. i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
  114. }
  115. }
  116. }
  117.  
  118. return i;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment