Artnet.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*The MIT License (MIT)
  2. Copyright (c) 2014 Nathanaël Lécaudé
  3. https://github.com/natcl/Artnet, http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <Artnet.h>
  21. Artnet::Artnet() {}
  22. void Artnet::begin(byte mac[], byte ip[])
  23. {
  24. #if !defined(ARDUINO_SAMD_ZERO) && !defined(ESP8266) && !defined(ESP32)
  25. Ethernet.begin(mac,ip);
  26. #endif
  27. Udp.begin(ART_NET_PORT);
  28. }
  29. void Artnet::begin()
  30. {
  31. Udp.begin(ART_NET_PORT);
  32. }
  33. void Artnet::setBroadcastAuto(IPAddress ip, IPAddress sn)
  34. {
  35. //Cast in uint 32 to use bitwise operation of DWORD
  36. uint32_t ip32 = ip;
  37. uint32_t sn32 = sn;
  38. //Find the broacast Address
  39. uint32_t bc = (ip32 & sn32) | (~sn32);
  40. //sets the broadcast address
  41. setBroadcast(IPAddress(bc));
  42. }
  43. void Artnet::setBroadcast(byte bc[])
  44. {
  45. //sets the broadcast address
  46. broadcast = bc;
  47. }
  48. void Artnet::setBroadcast(IPAddress bc)
  49. {
  50. //sets the broadcast address
  51. broadcast = bc;
  52. }
  53. uint16_t Artnet::read()
  54. {
  55. packetSize = Udp.parsePacket();
  56. remoteIP = Udp.remoteIP();
  57. if (packetSize <= MAX_BUFFER_ARTNET && packetSize > 0)
  58. {
  59. Udp.read(artnetPacket, MAX_BUFFER_ARTNET);
  60. // Check that packetID is "Art-Net" else ignore
  61. for (byte i = 0 ; i < 8 ; i++)
  62. {
  63. if (artnetPacket[i] != ART_NET_ID[i])
  64. return 0;
  65. }
  66. opcode = artnetPacket[8] | artnetPacket[9] << 8;
  67. if (opcode == ART_DMX)
  68. {
  69. sequence = artnetPacket[12];
  70. incomingUniverse = artnetPacket[14] | artnetPacket[15] << 8;
  71. dmxDataLength = artnetPacket[17] | artnetPacket[16] << 8;
  72. if (artDmxCallback) (*artDmxCallback)(incomingUniverse, dmxDataLength, sequence, artnetPacket + ART_DMX_START, remoteIP);
  73. return ART_DMX;
  74. }
  75. if (opcode == ART_POLL)
  76. {
  77. //fill the reply struct, and then send it to the network's broadcast address
  78. Serial.print("POLL from ");
  79. Serial.print(remoteIP);
  80. Serial.print(" broadcast addr: ");
  81. Serial.println(broadcast);
  82. #if !defined(ARDUINO_SAMD_ZERO) && !defined(ESP8266) && !defined(ESP32)
  83. IPAddress local_ip = Ethernet.localIP();
  84. #else
  85. IPAddress local_ip = WiFi.localIP();
  86. #endif
  87. node_ip_address[0] = local_ip[0];
  88. node_ip_address[1] = local_ip[1];
  89. node_ip_address[2] = local_ip[2];
  90. node_ip_address[3] = local_ip[3];
  91. sprintf((char *)id, "Art-Net");
  92. memcpy(ArtPollReply.id, id, sizeof(ArtPollReply.id));
  93. memcpy(ArtPollReply.ip, node_ip_address, sizeof(ArtPollReply.ip));
  94. ArtPollReply.opCode = ART_POLL_REPLY;
  95. ArtPollReply.port = ART_NET_PORT;
  96. memset(ArtPollReply.goodinput, 0x08, 4);
  97. memset(ArtPollReply.goodoutput, 0x80, 4);
  98. memset(ArtPollReply.porttypes, 0xc0, 4);
  99. uint8_t shortname [18];
  100. uint8_t longname [64];
  101. sprintf((char *)shortname, "artnet arduino");
  102. sprintf((char *)longname, "Art-Net -> Arduino Bridge");
  103. memcpy(ArtPollReply.shortname, shortname, sizeof(shortname));
  104. memcpy(ArtPollReply.longname, longname, sizeof(longname));
  105. ArtPollReply.etsaman[0] = 0;
  106. ArtPollReply.etsaman[1] = 0;
  107. ArtPollReply.verH = 1;
  108. ArtPollReply.ver = 0;
  109. ArtPollReply.subH = 0;
  110. ArtPollReply.sub = 0;
  111. ArtPollReply.oemH = 0;
  112. ArtPollReply.oem = 0xFF;
  113. ArtPollReply.ubea = 0;
  114. ArtPollReply.status = 0xd2;
  115. ArtPollReply.swvideo = 0;
  116. ArtPollReply.swmacro = 0;
  117. ArtPollReply.swremote = 0;
  118. ArtPollReply.style = 0;
  119. ArtPollReply.numbportsH = 0;
  120. ArtPollReply.numbports = 4;
  121. ArtPollReply.status2 = 0x08;
  122. ArtPollReply.bindip[0] = node_ip_address[0];
  123. ArtPollReply.bindip[1] = node_ip_address[1];
  124. ArtPollReply.bindip[2] = node_ip_address[2];
  125. ArtPollReply.bindip[3] = node_ip_address[3];
  126. uint8_t swin[4] = {0x01,0x02,0x03,0x04};
  127. uint8_t swout[4] = {0x01,0x02,0x03,0x04};
  128. for(uint8_t i = 0; i < 4; i++)
  129. {
  130. ArtPollReply.swout[i] = swout[i];
  131. ArtPollReply.swin[i] = swin[i];
  132. }
  133. sprintf((char *)ArtPollReply.nodereport, "%i DMX output universes active.", ArtPollReply.numbports);
  134. Udp.beginPacket(broadcast, ART_NET_PORT);//send the packet to the broadcast address
  135. Udp.write((uint8_t *)&ArtPollReply, sizeof(ArtPollReply));
  136. Udp.endPacket();
  137. return ART_POLL;
  138. }
  139. if (opcode == ART_SYNC)
  140. {
  141. if (artSyncCallback) (*artSyncCallback)(remoteIP);
  142. return ART_SYNC;
  143. }
  144. }
  145. else
  146. {
  147. return 0;
  148. }
  149. return 0;
  150. }
  151. void Artnet::printPacketHeader()
  152. {
  153. Serial.print("packet size = ");
  154. Serial.print(packetSize);
  155. Serial.print("\topcode = ");
  156. Serial.print(opcode, HEX);
  157. Serial.print("\tuniverse number = ");
  158. Serial.print(incomingUniverse);
  159. Serial.print("\tdata length = ");
  160. Serial.print(dmxDataLength);
  161. Serial.print("\tsequence n0. = ");
  162. Serial.println(sequence);
  163. }
  164. void Artnet::printPacketContent()
  165. {
  166. for (uint16_t i = ART_DMX_START ; i < dmxDataLength ; i++){
  167. Serial.print(artnetPacket[i], DEC);
  168. Serial.print(" ");
  169. }
  170. Serial.println('\n');
  171. }