Guest User

metaballs with lookup table

a guest
Jul 22nd, 2020
83
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.             };
  16. #define XY_MATRIX         (SERPENTINE | ROWMAJOR /*| FLIPMINOR*/)
  17. #define kMatrixWidth      NUM_ROWS
  18. #define kMatrixHeight     NUM_COLS
  19.  
  20.  
  21. // LEDs pin
  22. #define DATA_PIN 2
  23.  
  24. // LED brightness
  25. #define BRIGHTNESS 64
  26.  
  27.  
  28. // Define the array of leds
  29. CRGB leds[NUM_LEDS + 1];
  30.  
  31. DEFINE_GRADIENT_PALETTE( lava) {
  32.   0,      255,  30,  0,
  33.   32,     0,    0,   0,
  34.   128,    0,    0,   0,
  35.   150,    180,  180, 0,
  36.   255,    255,  50,  0
  37. };
  38.  
  39. CRGBPalette16 myPal = lava;
  40. byte sqrt_LUT[NUM_ROWS][NUM_COLS];
  41.  
  42.  
  43. void setup() {
  44.   Serial.begin(250000);
  45.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  46.   FastLED.setBrightness(BRIGHTNESS);
  47.   for (uint8_t i = 0; i < NUM_COLS; i++)
  48.     for (uint8_t j = 0; j < NUM_ROWS; j++)
  49.       sqrt_LUT[i][j] = dist(0, 0, i, j);
  50. }
  51.  
  52.  
  53. void loop() {
  54.  
  55.   uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
  56.   uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
  57.   uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
  58.   uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
  59.   uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
  60.   uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
  61.   uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
  62.   uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
  63.   uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
  64.   uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
  65.  
  66.   for (uint8_t i = 0; i < NUM_COLS; i++)    {
  67.     for (uint8_t j = 0; j < NUM_ROWS; j++) {
  68.  
  69.       uint16_t sum =   distLUT(i, j, bx1, by1);
  70.       sum = qadd8(sum, distLUT(i, j, bx2, by2));
  71.       sum = qadd8(sum, distLUT(i, j, bx3, by3));
  72.       sum = qadd8(sum, distLUT(i, j, bx4, by4));
  73.       sum = qadd8(sum, distLUT(i, j, bx5, by5));
  74.  
  75.       byte col =  constrain(sum, 128, 255);
  76.       leds[XY (i, j)] =  ColorFromPalette(myPal, col, BRIGHTNESS);
  77.  
  78.     }
  79.   }
  80.  
  81.   FastLED.show();
  82.   static int frame = 0;
  83.   if (frame++ % 32 == 0)
  84.     Serial.println(FastLED.getFPS());
  85. } //loop
  86.  
  87.  
  88. byte distLUT (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  89.   byte a = abs(y2 - y1);
  90.   byte b = abs(x2 - x1);
  91.   return sqrt_LUT[a][b];
  92. }
  93.  
  94. byte dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  95.   byte a = y2 - y1;
  96.   byte b = x2 - x1;
  97.   a *= a;
  98.   b *= b;
  99.   byte dist = 220;
  100.   if (a + b > 0)
  101.     dist = 220.0 / sqrt16(a + b);
  102.   return dist;
  103. }
  104.  
  105. uint16_t XY(uint8_t x, uint8_t y) {
  106.   uint8_t major, minor, sz_major, sz_minor;
  107.   if (x >= kMatrixWidth || y >= kMatrixHeight)
  108.     return NUM_LEDS;
  109.   if (XY_MATRIX & ROWMAJOR)
  110.     major = x, minor = y, sz_major = kMatrixWidth,  sz_minor = kMatrixHeight;
  111.   else
  112.     major = y, minor = x, sz_major = kMatrixHeight, sz_minor = kMatrixWidth;
  113.   if ((XY_MATRIX & FLIPMAJOR) ^ (minor & 1 && (XY_MATRIX & SERPENTINE)))
  114.     major = sz_major - 1 - major;
  115.   if (XY_MATRIX & FLIPMINOR)
  116.     minor = sz_minor - 1 - minor;
  117.   return (uint16_t) minor * sz_major + major;
  118. }
Add Comment
Please, Sign In to add comment