PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PlasmaAnimation.ino 4.2KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //PlazINT - Fast Plasma Generator using Integer Math Only
  2. //Edmund "Skorn" Horn
  3. //March 4,2013
  4. //Version 1.0 adapted for OctoWS2811Lib (tested, working...)
  5. #include <OctoWS2811.h>
  6. //OctoWS2811 Defn. Stuff
  7. #define COLS_LEDs 60 // all of the following params need to be adjusted for screen size
  8. #define ROWS_LEDs 16 // LED_LAYOUT assumed 0 if ROWS_LEDs > 8
  9. #define LEDS_PER_STRIP (COLS_LEDs * (ROWS_LEDs / 8))
  10. DMAMEM int displayMemory[LEDS_PER_STRIP*6];
  11. int drawingMemory[LEDS_PER_STRIP*6];
  12. const int config = WS2811_GRB | WS2811_800kHz;
  13. OctoWS2811 leds(LEDS_PER_STRIP, displayMemory, drawingMemory, config);
  14. //Byte val 2PI Cosine Wave, offset by 1 PI
  15. //supports fast trig calcs and smooth LED fading/pulsing.
  16. uint8_t const cos_wave[256] PROGMEM =
  17. {0,0,0,0,1,1,1,2,2,3,4,5,6,6,8,9,10,11,12,14,15,17,18,20,22,23,25,27,29,31,33,35,38,40,42,
  18. 45,47,49,52,54,57,60,62,65,68,71,73,76,79,82,85,88,91,94,97,100,103,106,109,113,116,119,
  19. 122,125,128,131,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,
  20. 189,191,194,197,199,202,204,207,209,212,214,216,218,221,223,225,227,229,231,232,234,236,
  21. 238,239,241,242,243,245,246,247,248,249,250,251,252,252,253,253,254,254,255,255,255,255,
  22. 255,255,255,255,254,254,253,253,252,252,251,250,249,248,247,246,245,243,242,241,239,238,
  23. 236,234,232,231,229,227,225,223,221,218,216,214,212,209,207,204,202,199,197,194,191,189,
  24. 186,183,180,177,174,171,168,165,162,159,156,153,150,147,144,141,138,135,131,128,125,122,
  25. 119,116,113,109,106,103,100,97,94,91,88,85,82,79,76,73,71,68,65,62,60,57,54,52,49,47,45,
  26. 42,40,38,35,33,31,29,27,25,23,22,20,18,17,15,14,12,11,10,9,8,6,6,5,4,3,2,2,1,1,1,0,0,0,0
  27. };
  28. //Gamma Correction Curve
  29. uint8_t const exp_gamma[256] PROGMEM =
  30. {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,
  31. 4,4,4,4,4,5,5,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,10,10,10,11,11,12,12,12,13,13,14,14,14,15,15,
  32. 16,16,17,17,18,18,19,19,20,20,21,21,22,23,23,24,24,25,26,26,27,28,28,29,30,30,31,32,32,33,
  33. 34,35,35,36,37,38,39,39,40,41,42,43,44,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
  34. 61,62,63,64,65,66,67,68,70,71,72,73,74,75,77,78,79,80,82,83,84,85,87,89,91,92,93,95,96,98,
  35. 99,100,101,102,105,106,108,109,111,112,114,115,117,118,120,121,123,125,126,128,130,131,133,
  36. 135,136,138,140,142,143,145,147,149,151,152,154,156,158,160,162,164,165,167,169,171,173,175,
  37. 177,179,181,183,185,187,190,192,194,196,198,200,202,204,207,209,211,213,216,218,220,222,225,
  38. 227,229,232,234,236,239,241,244,246,249,251,253,254,255
  39. };
  40. void setup()
  41. {
  42. pinMode(13, OUTPUT);
  43. leds.begin();
  44. leds.show();
  45. }
  46. void loop()
  47. {
  48. unsigned long frameCount=25500; // arbitrary seed to calculate the three time displacement variables t,t2,t3
  49. while(1) {
  50. frameCount++ ;
  51. uint16_t t = fastCosineCalc((42 * frameCount)/100); //time displacement - fiddle with these til it looks good...
  52. uint16_t t2 = fastCosineCalc((35 * frameCount)/100);
  53. uint16_t t3 = fastCosineCalc((38 * frameCount)/100);
  54. for (uint8_t y = 0; y < ROWS_LEDs; y++) {
  55. int left2Right, pixelIndex;
  56. if (((y % (ROWS_LEDs/8)) & 1) == 0) {
  57. left2Right = 1;
  58. pixelIndex = y * COLS_LEDs;
  59. } else {
  60. left2Right = -1;
  61. pixelIndex = (y + 1) * COLS_LEDs - 1;
  62. }
  63. for (uint8_t x = 0; x < COLS_LEDs ; x++) {
  64. //Calculate 3 seperate plasma waves, one for each color channel
  65. uint8_t r = fastCosineCalc(((x << 3) + (t >> 1) + fastCosineCalc((t2 + (y << 3)))));
  66. uint8_t g = fastCosineCalc(((y << 3) + t + fastCosineCalc(((t3 >> 2) + (x << 3)))));
  67. uint8_t b = fastCosineCalc(((y << 3) + t2 + fastCosineCalc((t + x + (g >> 2)))));
  68. //uncomment the following to enable gamma correction
  69. //r=pgm_read_byte_near(exp_gamma+r);
  70. //g=pgm_read_byte_near(exp_gamma+g);
  71. //b=pgm_read_byte_near(exp_gamma+b);
  72. leds.setPixel(pixelIndex, ((r << 16) | (g << 8) | b));
  73. pixelIndex += left2Right;
  74. }
  75. }
  76. digitalWrite(13, HIGH);
  77. leds.show(); // not sure if this function is needed to update each frame
  78. digitalWrite(13, LOW);
  79. }
  80. }
  81. inline uint8_t fastCosineCalc( uint16_t preWrapVal)
  82. {
  83. uint8_t wrapVal = (preWrapVal % 255);
  84. if (wrapVal<0) wrapVal=255+wrapVal;
  85. return (pgm_read_byte_near(cos_wave+wrapVal));
  86. }