Advertisement
Guest User

Untitled

a guest
Jul 21st, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Metaballs
  2. //16x16 rgb led matrix demo
  3. //Yaroslaw Turbin 20.07.2020
  4. //https://vk.com/ldirko
  5. //https://www.reddit.com/user/ldirko/
  6. #include "FastLED.h"
  7.  
  8. // Matrix size
  9. #define NUM_ROWS 16
  10. #define NUM_COLS 16
  11. #define NUM_LEDS (NUM_ROWS * NUM_COLS)
  12.  
  13. enum XY_cfg {LINEAR = 0, SERPENTINE = 1, COLUMNMAJOR = 0,
  14.              ROWMAJOR = 2, FLIPMAJOR = 4, FLIPMINOR = 8};
  15. #define XY_MATRIX         (SERPENTINE | ROWMAJOR /*| FLIPMINOR*/)
  16. #define kMatrixWidth      NUM_ROWS
  17. #define kMatrixHeight     NUM_COLS
  18.  
  19.  
  20. // LEDs pin
  21. #define DATA_PIN 2
  22.  
  23. // LED brightness
  24. #define BRIGHTNESS 64
  25.  
  26.  
  27. // Define the array of leds
  28. CRGB leds[NUM_LEDS + 1];
  29.  
  30. DEFINE_GRADIENT_PALETTE( lava) {
  31.   0,      255,  30,  0,
  32.   32,     0,    0,   0,
  33.   128,    0,    0,   0,
  34.   150,    180,  180, 0,
  35.   255,    255,  50,  0
  36. };
  37.  
  38. CRGBPalette16 myPal = lava;
  39.  
  40.  
  41. void setup() {
  42.   Serial.begin(250000);
  43.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  44.   FastLED.setBrightness(BRIGHTNESS);
  45. }
  46.  
  47.  
  48. void loop() {
  49.  
  50.   uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
  51.   uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
  52.   uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
  53.   uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
  54.   uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
  55.   uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
  56.   uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
  57.   uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
  58.   uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
  59.   uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
  60.  
  61.   for (uint8_t i = 0; i < NUM_COLS; i++)    {
  62.     for (uint8_t j = 0; j < NUM_ROWS; j++) {
  63.  
  64.       float sum =  2 * dist(i, j, bx1, by1);
  65.       sum += 2 * dist(i, j, bx2, by2);
  66.       sum += 2 * dist(i, j, bx3, by3);
  67.       sum += 2 * dist(i, j, bx4, by4);
  68.       sum += 2 * dist(i, j, bx5, by5);
  69.  
  70.       byte col =  constrain(120 * sum, 128, 255);
  71.       leds[XY (i, j)] =  ColorFromPalette(myPal, col, BRIGHTNESS);
  72.  
  73.     }
  74.   }
  75.  
  76.   FastLED.show();
  77.   static int frame = 0;
  78.   if (frame++ % 32 == 0)
  79.     Serial.println(FastLED.getFPS());
  80. } //loop
  81.  
  82.  
  83.  
  84. uint16_t XY(uint8_t x, uint8_t y) {
  85.   uint8_t major, minor, sz_major, sz_minor;
  86.   if (x >= kMatrixWidth || y >= kMatrixHeight)
  87.     return NUM_LEDS;
  88.   if (XY_MATRIX & ROWMAJOR)
  89.     major = x, minor = y, sz_major = kMatrixWidth,  sz_minor = kMatrixHeight;
  90.   else
  91.     major = y, minor = x, sz_major = kMatrixHeight, sz_minor = kMatrixWidth;
  92.   if ((XY_MATRIX & FLIPMAJOR) ^ (minor & 1 && (XY_MATRIX & SERPENTINE)))
  93.     major = sz_major - 1 - major;
  94.   if (XY_MATRIX & FLIPMINOR)
  95.     minor = sz_minor - 1 - minor;
  96.   return (uint16_t) minor * sz_major + major;
  97. }
  98.  
  99.  
  100. float dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  101. //  float dist = 1 / sqrt(sq(y2 - y1) + sq(x2 - x1));
  102.   float dist = Q_rsqrt(sq(y2 - y1) + sq(x2 - x1));
  103.   return dist;
  104. }
  105.  
  106.  
  107. float Q_rsqrt( float number )
  108. {
  109.   long i;
  110.   float x2, y;
  111.   const float threehalfs = 1.5F;
  112.  
  113.   x2 = number * 0.5F;
  114.   y  = number;
  115.   i  = * ( long * ) &y;                       // evil floating point bit level hacking
  116.   i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
  117.   y  = * ( float * ) &i;
  118.   y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
  119. //  y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
  120.  
  121.   return y;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement