ldirko

MB speedup test

Jul 22nd, 2020 (edited)
222
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.  
  7. //some distance aproximate for get 30+ fps )))
  8.  
  9. #include "FastLED.h"
  10.  
  11. // Matrix size
  12. #define NUM_ROWS 16
  13. #define NUM_COLS 16
  14. #define NUM_LEDS (NUM_ROWS * NUM_COLS)
  15.  
  16.  
  17.  
  18. // LEDs pin
  19. #define DATA_PIN 3
  20.  
  21. // LED brightness
  22. #define BRIGHTNESS 255
  23.  
  24.  
  25. // Define the array of leds
  26. CRGB leds[NUM_LEDS];
  27.  
  28. DEFINE_GRADIENT_PALETTE( lava) {
  29.   0,      255,  30,  0,
  30.  32,     0,    0,   0,
  31.  128,    0,    0,   0,
  32.  140,    180,  180, 0,
  33.  255,    255,  50,  0    
  34.  
  35. };
  36.  
  37. CRGBPalette16 myPal = lava;
  38.  
  39.  
  40. void setup() {
  41.   Serial.begin(250000);
  42.    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  43.   FastLED.setBrightness(BRIGHTNESS);
  44. }
  45.  
  46.  
  47. void loop() {
  48.  
  49.   uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
  50.   uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
  51.   uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
  52.   uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
  53.   uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
  54.   uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
  55.   uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
  56.   uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
  57.   uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
  58.   uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
  59.  
  60.   for (uint8_t i = 0; i < NUM_COLS; i++)    {
  61.     for (uint8_t j = 0; j < NUM_ROWS; j++) {
  62.  
  63.       uint16_t  sum =  dist(i, j, bx1, by1);
  64.       sum = qadd8(sum, dist(i, j, bx2, by2));
  65.       sum = qadd8(sum, dist(i, j, bx3, by3));
  66.       sum = qadd8(sum, dist(i, j, bx4, by4));
  67.       sum = qadd8(sum, dist(i, j, bx5, by5));
  68.  
  69.      // byte col =  constrain(120 * sum, 128, 255);
  70.          
  71.  
  72.       leds[XY (i, j)] =  ColorFromPalette(myPal, constrain(sum, 128, 255), BRIGHTNESS);
  73. //Serial.println(col);
  74.     }
  75.   }
  76.  
  77.   FastLED.show();
  78.   static int frame = 0;
  79.   if (frame++ % 32 == 0)
  80.     Serial.println(FastLED.getFPS());
  81. } //loop
  82.  
  83.  
  84.  
  85. uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}        //simple function to find led number in led matrix,
  86.  
  87.  
  88. byte dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {    // https://gamedev.ru/tip/?id=41
  89. byte dx= abs8(y2-y1);
  90. byte dy = abs8(x2-x1);
  91.    if ( dx < dy )  return 200/dy;  
  92.   else    return 200/dx;
  93. }
Add Comment
Please, Sign In to add comment