niedzwiedzw

Untitled

Feb 25th, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Portions of this code are adapted from Aurora: https://github.com/pixelmatix/aurora
  3.  * Copyright (c) 2014 Jason Coon
  4.  *
  5.  * Portions of this code are adapted from LedEffects Plasma by Robert Atkins: https://bitbucket.org/ratkins/ledeffects/src/26ed3c51912af6fac5f1304629c7b4ab7ac8ca4b/Plasma.cpp?at=default
  6.  * Copyright (c) 2013 Robert Atkins
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9.  * this software and associated documentation files (the "Software"), to deal in
  10.  * the Software without restriction, including without limitation the rights to
  11.  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12.  * the Software, and to permit persons to whom the Software is furnished to do so,
  13.  * subject to the following conditions:
  14.  *
  15.  * The above copyright notice and this permission notice shall be included in all
  16.  * copies or substantial portions of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20.  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21.  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22.  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26.  
  27. // HUB75E pinout
  28. // R1 | G1
  29. // B1 | GND
  30. // R2 | G2
  31. // B2 | E
  32. //  A | B
  33. //  C | D
  34. // CLK| LAT
  35. // OE | GND
  36.  
  37. /*  Default library pin configuration for the reference
  38.   you can redefine only ones you need later on object creation
  39.  
  40. #define R1 25
  41. #define G1 26
  42. #define BL1 27
  43. #define R2 14
  44. #define G2 12
  45. #define BL2 13
  46. #define CH_A 23
  47. #define CH_B 19
  48. #define CH_C 5
  49. #define CH_D 17
  50. #define CH_E -1 // assign to any available pin if using two panels or 64x64 panels with 1/32 scan
  51. #define CLK 16
  52. #define LAT 4
  53. #define OE 15
  54.  
  55. */
  56.  
  57.  
  58. #include "ESP32-HUB75-MatrixPanel-I2S-DMA.h"
  59. #include <FastLED.h>
  60.  
  61. // Configure for your panel(s) as appropriate!
  62. #define PANEL_WIDTH 64
  63. #define PANEL_HEIGHT 64     // Panel height of 64 will required PIN_E to be defined.
  64. #define PANELS_NUMBER 1     // Number of chained panels, if just a single panel, obviously set to 1
  65. #define PIN_E 32
  66.  
  67. #define PANE_WIDTH PANEL_WIDTH * PANELS_NUMBER
  68. #define PANE_HEIGHT PANEL_HEIGHT
  69.  
  70.  
  71. // placeholder for the matrix object
  72. MatrixPanel_I2S_DMA *dma_display = nullptr;
  73.  
  74.  
  75. uint16_t time_counter = 0, cycles = 0, fps = 0;
  76. unsigned long fps_timer;
  77.  
  78. CRGB currentColor;
  79. CRGBPalette16 palettes[] = {HeatColors_p, LavaColors_p, RainbowColors_p, RainbowStripeColors_p, CloudColors_p};
  80. CRGBPalette16 currentPalette = palettes[0];
  81.  
  82.  
  83. CRGB ColorFromCurrentPalette(uint8_t index = 0, uint8_t brightness = 255, TBlendType blendType = LINEARBLEND) {
  84.   return ColorFromPalette(currentPalette, index, brightness, blendType);
  85. }
  86.  
  87. void setup() {
  88.  
  89.   Serial.begin(115200);
  90.  
  91.   Serial.println(F("*****************************************************"));
  92.   Serial.println(F("*        ESP32-HUB75-MatrixPanel-I2S-DMA DEMO       *"));
  93.   Serial.println(F("*****************************************************"));
  94.  
  95.   /*
  96.     The configuration for MatrixPanel_I2S_DMA object is held in HUB75_I2S_CFG structure,
  97.     pls refer to the lib header file for full details.
  98.     All options has it's predefined default values. So we can create a new structure and redefine only the options we need
  99.  
  100.     // those are the defaults
  101.     mxconfig.mx_width = 64;                   // physical width of a single matrix panel module (in pixels, usually it is always 64 ;) )
  102.     mxconfig.mx_height = 32;                  // physical height of a single matrix panel module (in pixels, usually almost always it is either 32 or 64)
  103.     mxconfig.chain_length = 1;                // number of chained panels regardless of the topology, default 1 - a single matrix module
  104.     mxconfig.gpio.r1 = R1;                    // pin mappings
  105.     mxconfig.gpio.g1 = G1;
  106.     mxconfig.gpio.b1 = B1;                    // etc
  107.     mxconfig.driver = HUB75_I2S_CFG::SHIFT;   // shift reg driver, default is plain shift register
  108.     mxconfig.double_buff = false;             // use double buffer (twice amount of RAM required)
  109.     mxconfig.i2sspeed = HUB75_I2S_CFG::HZ_10M;// I2S clock speed, better leave as-is unless you want to experiment
  110.   */
  111.  
  112.   /*
  113.     For example we have two 64x64 panels chained, so we need to customize our setup like this
  114.  
  115.   */
  116.   HUB75_I2S_CFG mxconfig;
  117.   mxconfig.mx_height = PANEL_HEIGHT;      // we have 64 pix heigh panels
  118.   mxconfig.chain_length = PANELS_NUMBER;  // we have 2 panels chained
  119.   mxconfig.gpio.e = PIN_E;                // we MUST assign pin e to some free pin on a board to drive 64 pix height panels with 1/32 scan
  120.   //mxconfig.driver = HUB75_I2S_CFG::FM6126A;     // in case that we use panels based on FM6126A chip, we can change that
  121.  
  122.   /*
  123.     //Another way of creating config structure
  124.     //Custom pin mapping for all pins
  125.     HUB75_I2S_CFG::i2s_pins _pins={R1, G1, BL1, R2, G2, BL2, CH_A, CH_B, CH_C, CH_D, CH_E, LAT, OE, CLK};
  126.     HUB75_I2S_CFG mxconfig(
  127.                             64,   // width
  128.                             64,   // height
  129.                              4,   // chain length
  130.                          _pins,   // pin mapping
  131.       HUB75_I2S_CFG::FM6126A      // driver chip
  132.     );
  133.  
  134.   */
  135.  
  136.  
  137.   // OK, now we can create our matrix object
  138.   dma_display = new MatrixPanel_I2S_DMA(mxconfig);
  139.  
  140.   // let's adjust default brightness to about 75%
  141.   dma_display->setBrightness8(192);    // range is 0-255, 0 - 0%, 255 - 100%
  142.  
  143.   // Allocate memory and start DMA display
  144.   if( not dma_display->begin() )
  145.       Serial.println("****** !KABOOM! I2S memory allocation failed ***********");
  146.  
  147.   // well, hope we are OK, let's draw some colors first :)
  148.   Serial.println("Fill screen: RED");
  149.   dma_display->fillScreenRGB888(255, 0, 0);
  150.   delay(1000);
  151.  
  152.   Serial.println("Fill screen: GREEN");
  153.   dma_display->fillScreenRGB888(0, 255, 0);
  154.   delay(1000);
  155.  
  156.   Serial.println("Fill screen: BLUE");
  157.   dma_display->fillScreenRGB888(0, 0, 255);
  158.   delay(1000);
  159.  
  160.   Serial.println("Fill screen: Neutral White");
  161.   dma_display->fillScreenRGB888(128, 128, 128);
  162.   delay(1000);
  163.  
  164.   Serial.println("Fill screen: black");
  165.   dma_display->fillScreenRGB888(0, 0, 0);
  166.   delay(1000);
  167.  
  168.  
  169.   // Set current FastLED palette
  170.   currentPalette = RainbowColors_p;
  171.   Serial.println("Starting plasma effect...");
  172.   fps_timer = millis();
  173. }
  174.  
  175. void loop() {
  176.  
  177.     for (int x = 0; x < PANE_WIDTH; x++) {
  178.             for (int y = 0; y <  PANE_HEIGHT; y++) {
  179.                 int16_t v = 0;
  180.                 uint8_t wibble = sin8(time_counter);
  181.                 v += sin16(x * wibble * 3 + time_counter);
  182.                 v += cos16(y * (128 - wibble)  + time_counter);
  183.                 v += sin16(y * x * cos8(-time_counter) / 8);
  184.  
  185.                 currentColor = ColorFromPalette(currentPalette, (v >> 8) + 127); //, brightness, currentBlendType);
  186.                 dma_display->drawPixelRGB888(x, y, currentColor.r, currentColor.g, currentColor.b);
  187.             }
  188.     }
  189.  
  190.     ++time_counter;
  191.     ++cycles;
  192.     ++fps;
  193.  
  194.     if (cycles >= 1024) {
  195.         time_counter = 0;
  196.         cycles = 0;
  197.         currentPalette = palettes[random(0,sizeof(palettes)/sizeof(palettes[0]))];
  198.     }
  199.  
  200.     // print FPS rate every 5 seconds
  201.     // Note: this is NOT a matrix refresh rate, it's the number of data frames being drawn to the DMA buffer per second
  202.     if (fps_timer + 5000 < millis()){
  203.       Serial.printf_P(PSTR("Effect fps: %d\n"), fps/5);
  204.       fps_timer = millis();
  205.       fps = 0;
  206.     }
  207. } // end loop
  208.  
Add Comment
Please, Sign In to add comment