Revision 0:1abf45d17b99, committed 2016-03-25
- Comitter:
- hideakitai
- Date:
- Fri Mar 25 07:39:22 2016 +0000
- Commit message:
- first commit
Changed in this revision
diff -r 000000000000 -r 1abf45d17b99 Artnet.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Artnet.cpp Fri Mar 25 07:39:22 2016 +0000
@@ -0,0 +1,91 @@
+/*The MIT License (MIT)
+
+Copyright (c) 2014 Nathanaël Lécaudé
+https://github.com/natcl/Artnet, http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <Artnet.h>
+
+Artnet::Artnet() {}
+
+void Artnet::begin(const char* ip, const char* mask, const char* gateway)
+{
+ eth.init(ip, mask, gateway);
+ eth.connect();
+ printf("\nServer IP Address is %s\n", eth.getIPAddress());
+
+ server.bind(ART_NET_PORT);
+}
+
+uint16_t Artnet::read()
+{
+ int packetSize = server.receiveFrom(client, (char*)artnetPacket, (int)MAX_BUFFER_ARTNET);
+
+ if (packetSize <= MAX_BUFFER_ARTNET && packetSize > 0)
+ {
+ // Udp.read(artnetPacket, MAX_BUFFER_ARTNET);
+
+ // Check that packetID is "Art-Net" else ignore
+ for (size_t i = 0 ; i < 9 ; i++)
+ {
+ if (artnetPacket[i] != ART_NET_ID[i])
+ return 0;
+ }
+
+ opcode = artnetPacket[8] | artnetPacket[9] << 8;
+
+ if (opcode == ART_DMX)
+ {
+ sequence = artnetPacket[12];
+ incomingUniverse = artnetPacket[14] | artnetPacket[15] << 8;
+ dmxDataLength = artnetPacket[17] | artnetPacket[16] << 8;
+// dmxDataLength = artnetPacket[16] | artnetPacket[17] << 8;
+
+ if (artDmxCallback) (*artDmxCallback)(incomingUniverse, dmxDataLength, sequence, artnetPacket + ART_DMX_START);
+ return ART_DMX;
+ }
+ if (opcode == ART_POLL)
+ {
+ return ART_POLL;
+ }
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+void Artnet::printPacketHeader()
+{
+ printf("\npacket size = %d", packetSize);
+ printf("\nopcode = %2x", opcode);
+ printf("\nuniverse number = %d", incomingUniverse);
+ printf("\ndata length = %d", dmxDataLength);
+ printf("\nsequence n0. = %d", sequence);
+}
+
+void Artnet::printPacketContent()
+{
+ for (uint16_t i = ART_DMX_START ; i < dmxDataLength ; i++){
+ printf("%d ", artnetPacket[i]);
+ }
+ printf("\n\n");
+}
diff -r 000000000000 -r 1abf45d17b99 Artnet.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Artnet.h Fri Mar 25 07:39:22 2016 +0000
@@ -0,0 +1,101 @@
+/*The MIT License (MIT)
+
+Copyright (c) 2014 Nathanaël Lécaudé
+https://github.com/natcl/Artnet, http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef ARTNET_H
+#define ARTNET_H
+
+// #include <Arduino.h>
+// #include <Ethernet.h>
+// #include <EthernetUdp.h>
+#include <mbed.h>
+#include <EthernetInterface.h>
+
+// UDP specific
+#define ART_NET_PORT 6454
+// Opcodes
+#define ART_POLL 0x2000
+#define ART_DMX 0x5000
+// Buffers
+#define MAX_BUFFER_ARTNET 530
+// Packet
+#define ART_NET_ID "Art-Net\0"
+#define ART_DMX_START 18
+
+class Artnet
+{
+public:
+ Artnet();
+
+ void begin(const char* ip, const char* mask, const char* gateway);
+ uint16_t read();
+ void printPacketHeader();
+ void printPacketContent();
+
+ // Return a pointer to the start of the DMX data
+ inline uint8_t* getDmxFrame(void)
+ {
+ return artnetPacket + ART_DMX_START;
+ }
+
+ inline uint16_t getOpcode(void)
+ {
+ return opcode;
+ }
+
+ inline uint8_t getSequence(void)
+ {
+ return sequence;
+ }
+
+ inline uint16_t getUniverse(void)
+ {
+ return incomingUniverse;
+ }
+
+ inline uint16_t getLength(void)
+ {
+ return dmxDataLength;
+ }
+
+ inline void setArtDmxCallback(void (*fptr)(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data))
+ {
+ artDmxCallback = fptr;
+ }
+
+private:
+ // EthernetUDP Udp;
+ EthernetInterface eth;
+ UDPSocket server;
+ Endpoint client;
+
+ uint8_t artnetPacket[MAX_BUFFER_ARTNET];
+ uint16_t packetSize;
+ uint16_t opcode;
+ uint8_t sequence;
+ uint16_t incomingUniverse;
+ uint16_t dmxDataLength;
+ void (*artDmxCallback)(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data);
+};
+
+#endif