PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.4KB

  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 mac[] = {0x04, 0xE9, 0xE5, 0x00, 0x69, 0xEC};
  35. void setup()
  36. {
  37. Serial.begin(115200);
  38. artnet.begin(mac, ip);
  39. leds.begin();
  40. initTest();
  41. if (!SD.begin(chipSelect)) {
  42. Serial.println("initialization failed!");
  43. }
  44. else
  45. Serial.println("initialization done.");
  46. // this will be called for each packet received
  47. artnet.setArtDmxCallback(onDmxFrame);
  48. }
  49. void loop()
  50. {
  51. // we call the read function inside the loop
  52. artnet.read();
  53. if (playback)
  54. {
  55. while(datafile.available() && playback)
  56. {
  57. artnet.read();
  58. datafile.read(channelBuffer, numberOfChannels);
  59. for (int i = 0; i < numLeds; i++)
  60. leds.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
  61. leds.show();
  62. delay(20);
  63. }
  64. playback = 0;
  65. datafile.close();
  66. }
  67. }
  68. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
  69. {
  70. sendFrame = 1;
  71. // set brightness of the whole strip
  72. if (universe == 15)
  73. {
  74. leds.setBrightness(data[0]);
  75. leds.show();
  76. }
  77. if (universe == 14)
  78. {
  79. // record
  80. if (data[0] == 255)
  81. {
  82. if (SD.exists(fileName))
  83. {
  84. SD.remove(fileName);
  85. }
  86. datafile = SD.open(fileName, FILE_WRITE);
  87. record = 1;
  88. playback = 0;
  89. }
  90. // play
  91. if (data[0] == 127)
  92. {
  93. record = 0;
  94. playback = 1;
  95. datafile = SD.open(fileName, FILE_READ);
  96. }
  97. // stop
  98. if (data[0] == 0)
  99. {
  100. record = 0;
  101. playback = 0;
  102. datafile.close();
  103. }
  104. }
  105. // Store which universe has got in
  106. if (universe < maxUniverses)
  107. universesReceived[universe] = 1;
  108. for (int i = 0 ; i < maxUniverses ; i++)
  109. {
  110. if (universesReceived[i] == 0)
  111. {
  112. //Serial.println("Broke");
  113. sendFrame = 0;
  114. break;
  115. }
  116. }
  117. if (!playback)
  118. {
  119. // read universe and put into the right part of the display buffer
  120. for (int i = 0 ; i < length ; i++)
  121. {
  122. int bufferIndex = i + ((universe - startUniverse) * length);
  123. if (bufferIndex < numberOfChannels) // to verify
  124. channelBuffer[bufferIndex] = byte(data[i]);
  125. }
  126. }
  127. if (record && sendFrame)
  128. {
  129. datafile.write(channelBuffer, numberOfChannels);
  130. memset(universesReceived, 0, maxUniverses);
  131. }
  132. if (!playback && !record)
  133. {
  134. // send to leds
  135. for (int i = 0; i < numLeds; i++)
  136. {
  137. leds.setPixelColor(i, channelBuffer[(i) * 3], channelBuffer[(i * 3) + 1], channelBuffer[(i * 3) + 2]);
  138. }
  139. if (sendFrame)
  140. {
  141. leds.show();
  142. // Reset universeReceived to 0
  143. memset(universesReceived, 0, maxUniverses);
  144. }
  145. }
  146. }
  147. void initTest()
  148. {
  149. for (int i = 0 ; i < numLeds ; i++)
  150. leds.setPixelColor(i, 127, 0, 0);
  151. leds.show();
  152. delay(500);
  153. for (int i = 0 ; i < numLeds ; i++)
  154. leds.setPixelColor(i, 0, 127, 0);
  155. leds.show();
  156. delay(500);
  157. for (int i = 0 ; i < numLeds ; i++)
  158. leds.setPixelColor(i, 0, 0, 127);
  159. leds.show();
  160. delay(500);
  161. for (int i = 0 ; i < numLeds ; i++)
  162. leds.setPixelColor(i, 0, 0, 0);
  163. leds.show();
  164. }