ArtnetNeoPixel.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. This example will receive multiple universes via Artnet and control a strip of ws2811 leds via
  3. Adafruit's NeoPixel library: https://github.com/adafruit/Adafruit_NeoPixel
  4. This example may be copied under the terms of the MIT license, see the LICENSE file for details
  5. */
  6. #include <Artnet.h>
  7. #include <Ethernet.h>
  8. #include <EthernetUdp.h>
  9. #include <SPI.h>
  10. #include <Adafruit_NeoPixel.h>
  11. // Neopixel settings
  12. const int numLeds = 240; // change for your setup
  13. const int channelsPerLed = 3;
  14. const int numberOfChannels = numLeds * channelsPerLed; // Total number of channels you want to receive (1 led = 3 channels)
  15. const byte dataPin = 2;
  16. Adafruit_NeoPixel leds = Adafruit_NeoPixel(numLeds, dataPin, NEO_GRB + NEO_KHZ800);
  17. // Artnet settings
  18. Artnet artnet;
  19. const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0.
  20. // Check if we got all universes
  21. const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
  22. bool universesReceived[maxUniverses];
  23. bool sendFrame = 1;
  24. int previousDataLength = 0;
  25. // Change ip and mac address for your setup
  26. byte ip[] = {10, 0, 1, 199};
  27. byte mac[] = {0x04, 0xE9, 0xE5, 0x00, 0x69, 0xEC};
  28. byte broadcast[] = {10, 0, 1, 255};
  29. void setup()
  30. {
  31. //Serial.begin(115200);
  32. artnet.begin(mac, ip);
  33. leds.begin();
  34. artnet.setBroadcast(broadcast);
  35. initTest();
  36. // this will be called for each packet received
  37. artnet.setArtDmxCallback(onDmxFrame);
  38. }
  39. void loop()
  40. {
  41. // we call the read function inside the loop
  42. artnet.read();
  43. }
  44. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data, IPAddress remoteIP)
  45. {
  46. sendFrame = 1;
  47. // set brightness of the whole strip
  48. if (universe == 15)
  49. {
  50. leds.setBrightness(data[0]);
  51. leds.show();
  52. }
  53. // Store which universe has got in
  54. if ((universe - startUniverse) < maxUniverses)
  55. universesReceived[universe - startUniverse] = 1;
  56. for (int i = 0 ; i < maxUniverses ; i++)
  57. {
  58. if (universesReceived[i] == 0)
  59. {
  60. //Serial.println("Broke");
  61. sendFrame = 0;
  62. break;
  63. }
  64. }
  65. // read universe and put into the right part of the display buffer
  66. for (int i = 0; i < length / channelsPerLed; i++)
  67. {
  68. int led = i + (universe - startUniverse) * (previousDataLength / channelsPerLed);
  69. if (led < numLeds) {
  70. if (channelsPerLed == 4)
  71. leds.setPixelColor(led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2], data[i * channelsPerLed + 3]);
  72. if (channelsPerLed == 3)
  73. leds.setPixelColor(led, data[i * channelsPerLed], data[i * channelsPerLed + 1], data[i * channelsPerLed + 2]);
  74. }
  75. }
  76. previousDataLength = length;
  77. if (sendFrame)
  78. {
  79. leds.show();
  80. // Reset universeReceived to 0
  81. memset(universesReceived, 0, maxUniverses);
  82. }
  83. }
  84. void initTest()
  85. {
  86. for (int i = 0 ; i < numLeds ; i++)
  87. leds.setPixelColor(i, 127, 0, 0);
  88. leds.show();
  89. delay(500);
  90. for (int i = 0 ; i < numLeds ; i++)
  91. leds.setPixelColor(i, 0, 127, 0);
  92. leds.show();
  93. delay(500);
  94. for (int i = 0 ; i < numLeds ; i++)
  95. leds.setPixelColor(i, 0, 0, 127);
  96. leds.show();
  97. delay(500);
  98. for (int i = 0 ; i < numLeds ; i++)
  99. leds.setPixelColor(i, 0, 0, 0);
  100. leds.show();
  101. }