Advertisement
Guest User

esp32 neopixelbus multipin output not working

a guest
Aug 29th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <NeoPixelBus.h>
  3. #include <Artnet.h>
  4. #include <WiFi.h>
  5. #include <SPI.h>
  6. #include <ETH.h>
  7.  
  8. // Neopixel settings
  9. const int numLeds1 = 144;                // number of leds on pin 2
  10. const int numLeds2 = 144;                // number of leds on pin 3
  11. const int channelsPerLed = 4;            // 4 channels fixed for now
  12. const int PixelPin1 = 4;                 // make sure to set this to the correct pin
  13. const int PixelPin2 = 2;                 // make sure to set this to the correct pin
  14.  
  15.  
  16.  
  17. // Calculate the amount of universes needed per pin
  18. int totalPixelCount = numLeds1 + numLeds2;
  19.  
  20. const int numberOfChannels1 = numLeds1 * channelsPerLed; // Total number of channels you want to receive (1 led = 4 channels)
  21. const int numberOfUniverses1 = (numberOfChannels1 + 512 - 1) / 512;
  22.  
  23. const int numberOfChannels2 = numLeds2 * channelsPerLed; // Total number of channels you want to receive (1 led = 4 channels)
  24. const int numberOfUniverses2 = (numberOfChannels2 + 512 - 1) / 512;
  25. int totalUniverses = numberOfUniverses1 + numberOfUniverses2;
  26.  
  27. // Setup the Neopixelbus pins
  28. typedef NeoPixelBus<NeoGrbwFeature, NeoEsp32RmtNSk6812Method> MyNeoPixelBus;
  29. MyNeoPixelBus strips[] = {
  30.   MyNeoPixelBus(numLeds1, PixelPin1, NeoBusChannel_0),
  31.   MyNeoPixelBus(numLeds2, PixelPin2, NeoBusChannel_1)
  32. };
  33.  
  34. // NeoPixelBus<NeoGrbwFeature, NeoEsp32Rmt0Sk6812Method> leds1(numLeds1, PixelPin1);
  35. // NeoPixelBus<NeoGrbwFeature, NeoEsp32Rmt1Sk6812Method> leds2(numLeds2, PixelPin2);
  36. RgbwColor red(255, 0, 0, 0);
  37. RgbwColor green(0, 255, 0, 0);
  38. RgbwColor blue(0, 0, 255, 0);
  39. RgbwColor white(0, 0, 0, 255);
  40. RgbwColor black(0, 0, 0, 0);
  41.  
  42.  
  43. // Ethernet stuff
  44. char hostname[] = "controller";
  45. WiFiClient ethclient;
  46. static bool eth_connected = false;
  47. void WiFiEvent(WiFiEvent_t event)
  48. {
  49.   switch (event) {
  50.     case SYSTEM_EVENT_ETH_START:
  51.       Serial.println("ETH Started");
  52.       ETH.setHostname(hostname);
  53.       break;
  54.     case SYSTEM_EVENT_ETH_CONNECTED:
  55.       Serial.println("ETH Connected");
  56.       break;
  57.     case SYSTEM_EVENT_ETH_GOT_IP:
  58.       Serial.print("ETH MAC: ");
  59.       Serial.print(ETH.macAddress());
  60.       Serial.print(", IPv4: ");
  61.       Serial.print(ETH.localIP());
  62.       if (ETH.fullDuplex()) {
  63.         Serial.print(", FULL_DUPLEX");
  64.       }
  65.       Serial.print(", ");
  66.       Serial.print(ETH.linkSpeed());
  67.       Serial.println("Mbps");
  68.       eth_connected = true;
  69.       break;
  70.     case SYSTEM_EVENT_ETH_DISCONNECTED:
  71.       Serial.println("ETH Disconnected");
  72.       eth_connected = false;
  73.       break;
  74.     case SYSTEM_EVENT_ETH_STOP:
  75.       Serial.println("ETH Stopped");
  76.       eth_connected = false;
  77.       break;
  78.     default:
  79.       break;
  80.   }
  81. }
  82.  
  83.  
  84.  
  85. // Artnet settings
  86. Artnet artnet;
  87. const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0.
  88. int previousDataLength = 0;
  89.  
  90.  
  91.  
  92. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data, IPAddress remoteIP)
  93. {
  94.   // read universe and put into the right part of the display buffer
  95.   if (universe < totalUniverses){
  96.       for (int i = 0; i < length / channelsPerLed; i++)
  97.       {
  98.         int led = i + (universe - startUniverse) * (previousDataLength / channelsPerLed);
  99.         RgbwColor color(data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2], data[i * channelsPerLed + 3]);
  100.         if (led < numLeds1) {
  101.             Serial.println(PixelPin1);
  102.             // Serial.print("pin1 led: ");
  103.             // Serial.println(led);
  104.             // Serial.printf("Led: %i channel1: %i channel2 %i channel3 %i channel4 %i\n", led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2], data[i * channelsPerLed + 3] );
  105.             strips[0].SetPixelColor(led, color);
  106.         }
  107.         else if (led >= numLeds1 && led < (numLeds1 + numLeds2)) {
  108.             Serial.println(PixelPin2);
  109.             // Serial.printf("Led: %i channel1: %i channel2 %i channel3 %i channel4 %i\n", led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2], data[i * channelsPerLed + 3] );
  110.             strips[1].SetPixelColor(led, color);
  111.         }
  112.       }
  113.   }
  114.   else if (universe > totalUniverses) {
  115.     Serial.print("This universe is not configured: ");
  116.     Serial.println(universe);
  117.   }
  118.   strips[0].Show();
  119.   strips[1].Show();      
  120.   previousDataLength = length;
  121. }
  122.  
  123. void setup()
  124. {
  125.   Serial.begin(115200);
  126.   WiFi.onEvent(WiFiEvent);
  127.   ETH.begin();
  128.  
  129.   artnet.begin();
  130.   strips[0].Begin();
  131.   strips[1].Begin();
  132.   strips[0].ClearTo(black);
  133.   strips[1].ClearTo(black);
  134.  
  135.  
  136.   // this will be called for each packet received
  137.   artnet.setArtDmxCallback(onDmxFrame);
  138.  
  139. }
  140.  
  141.  
  142. void loop()
  143. {
  144.     artnet.read();
  145.  
  146.     // Debugging channels and universes
  147.     // Serial.print("Pin 1 channel count: ");
  148.     // Serial.print(numberOfChannels1);
  149.     // Serial.print(" - Pin 1 universes count:");
  150.     // Serial.print(numberOfUniverses1);    
  151.     // Serial.print(" --- Pin 2 channel count: ");
  152.     // Serial.print(numberOfChannels2);
  153.     // Serial.print(" - Pin 2 universes count:");
  154.     // Serial.println(numberOfUniverses2);
  155.  
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement