ArtnetNeoPixelSD.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Same as ArtnetNeoPixel.ino but with controls to record and playback sequences from an SD card.
  3. To record, send 255 to the first channel of universe 14. To stop, send 0 and to playback send 127.
  4. The limit of leds seems to be around 450 to get 44 fps. The playback routine is not optimzed yet.
  5. This example may be copied under the terms of the MIT license, see the LICENSE file for details
  6. */
  7. #include <Artnet.h>
  8. #include <Ethernet.h>
  9. #include <EthernetUdp.h>
  10. #include <SPI.h>
  11. #include <Adafruit_NeoPixel.h>
  12. #include <SD.h>
  13. // Neopixel settings
  14. const int numLeds = 200; // change for your setup
  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 zero.
  20. const int numberOfChannels = numLeds * 3; // Total number of channels you want to receive (1 led = 3 channels)
  21. byte channelBuffer[numberOfChannels]; // Combined universes into a single array
  22. // SD card
  23. File datafile;
  24. char fileName[] = "data.txt";
  25. const int chipSelect = 4;
  26. bool record = 0;
  27. bool playback = 0;
  28. // Check if we got all universes
  29. const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
  30. bool universesReceived[maxUniverses];
  31. bool sendFrame = 1;
  32. // Change ip and mac address for your setup
  33. byte ip[] = {192, 168, 2, 2};
  34. byte broadcast[] = {192, 168, 2, 255};
  35. byte mac[] = {0x04, 0xE9, 0xE5, 0x00, 0x69, 0xEC};
  36. void setup()
  37. {
  38. Serial.begin(115200);
  39. artnet.begin(mac, ip);
  40. artnet.setBroadcast(broadcast);
  41. leds.begin();
  42. initTest();
  43. if (!SD.begin(chipSelect)) {
  44. Serial.println("initialization failed!");
  45. }
  46. else
  47. Serial.println("initialization done.");
  48. // this will be called for each packet received
  49. artnet.setArtDmxCallback(onDmxFrame);
  50. }
  51. void loop()
  52. {
  53. // we call the read function inside the loop
  54. artnet.read();
  55. if (playback)
  56. {
  57. while(datafile.available() && playback)
  58. {
  59. artnet.read();
  60. datafile.read(channelBuffer, numberOfChannels);
  61. for (int i = 0; i < numLeds; i++)
  62. leds.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
  63. leds.show();
  64. delay(20);
  65. }
  66. playback = 0;
  67. datafile.close();
  68. }
  69. }
  70. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data, IPAddress remoteIP)
  71. {
  72. sendFrame = 1;
  73. // set brightness of the whole strip
  74. if (universe == 15)
  75. {
  76. leds.setBrightness(data[0]);
  77. leds.show();
  78. }
  79. if (universe == 14)
  80. {
  81. // record
  82. if (data[0] == 255)
  83. {
  84. if (SD.exists(fileName))
  85. {
  86. SD.remove(fileName);
  87. }
  88. datafile = SD.open(fileName, FILE_WRITE);
  89. record = 1;
  90. playback = 0;
  91. }
  92. // play
  93. if (data[0] == 127)
  94. {
  95. record = 0;
  96. playback = 1;
  97. datafile = SD.open(fileName, FILE_READ);
  98. }
  99. // stop
  100. if (data[0] == 0)
  101. {
  102. record = 0;
  103. playback = 0;
  104. datafile.close();
  105. }
  106. }
  107. // Store which universe has got in
  108. if (universe < maxUniverses)
  109. universesReceived[universe] = 1;
  110. for (int i = 0 ; i < maxUniverses ; i++)
  111. {
  112. if (universesReceived[i] == 0)
  113. {
  114. //Serial.println("Broke");
  115. sendFrame = 0;
  116. break;
  117. }
  118. }
  119. if (!playback)
  120. {
  121. // read universe and put into the right part of the display buffer
  122. for (int i = 0 ; i < length ; i++)
  123. {
  124. int bufferIndex = i + ((universe - startUniverse) * length);
  125. if (bufferIndex < numberOfChannels) // to verify
  126. channelBuffer[bufferIndex] = byte(data[i]);
  127. }
  128. }
  129. if (record && sendFrame)
  130. {
  131. datafile.write(channelBuffer, numberOfChannels);
  132. memset(universesReceived, 0, maxUniverses);
  133. }
  134. if (!playback && !record)
  135. {
  136. // send to leds
  137. for (int i = 0; i < numLeds; i++)
  138. {
  139. leds.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
  140. }
  141. if (sendFrame)
  142. {
  143. leds.show();
  144. // Reset universeReceived to 0
  145. memset(universesReceived, 0, maxUniverses);
  146. }
  147. }
  148. }
  149. void initTest()
  150. {
  151. for (int i = 0 ; i < numLeds ; i++)
  152. leds.setPixelColor(i, 127, 0, 0);
  153. leds.show();
  154. delay(500);
  155. for (int i = 0 ; i < numLeds ; i++)
  156. leds.setPixelColor(i, 0, 127, 0);
  157. leds.show();
  158. delay(500);
  159. for (int i = 0 ; i < numLeds ; i++)
  160. leds.setPixelColor(i, 0, 0, 127);
  161. leds.show();
  162. delay(500);
  163. for (int i = 0 ; i < numLeds ; i++)
  164. leds.setPixelColor(i, 0, 0, 0);
  165. leds.show();
  166. }