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.

103 lines
2.8KB

  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)
  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. uint16_t Artnet::read()
  34. {
  35. packetSize = Udp.parsePacket();
  36. if (packetSize <= MAX_BUFFER_ARTNET && packetSize > 0)
  37. {
  38. Udp.read(artnetPacket, MAX_BUFFER_ARTNET);
  39. // Check that packetID is "Art-Net" else ignore
  40. for (byte i = 0 ; i < 8 ; i++)
  41. {
  42. if (artnetPacket[i] != ART_NET_ID[i])
  43. return 0;
  44. }
  45. opcode = artnetPacket[8] | artnetPacket[9] << 8;
  46. if (opcode == ART_DMX)
  47. {
  48. sequence = artnetPacket[12];
  49. incomingUniverse = artnetPacket[14] | artnetPacket[15] << 8;
  50. dmxDataLength = artnetPacket[17] | artnetPacket[16] << 8;
  51. if (artDmxCallback) (*artDmxCallback)(incomingUniverse, dmxDataLength, sequence, artnetPacket + ART_DMX_START);
  52. return ART_DMX;
  53. }
  54. if (opcode == ART_POLL)
  55. {
  56. return ART_POLL;
  57. }
  58. return 0;
  59. }
  60. else
  61. {
  62. return 0;
  63. }
  64. }
  65. void Artnet::printPacketHeader()
  66. {
  67. Serial.print("packet size = ");
  68. Serial.print(packetSize);
  69. Serial.print("\topcode = ");
  70. Serial.print(opcode, HEX);
  71. Serial.print("\tuniverse number = ");
  72. Serial.print(incomingUniverse);
  73. Serial.print("\tdata length = ");
  74. Serial.print(dmxDataLength);
  75. Serial.print("\tsequence n0. = ");
  76. Serial.println(sequence);
  77. }
  78. void Artnet::printPacketContent()
  79. {
  80. for (uint16_t i = ART_DMX_START ; i < dmxDataLength ; i++){
  81. Serial.print(artnetPacket[i], DEC);
  82. Serial.print(" ");
  83. }
  84. Serial.println('\n');
  85. }