http://mbed.org/users/okini3939/notebook/art-net/
Dependents: ArtNode ArtNode DMXStation ArtNodeLED ... more
DmxArtNet.h@1:c59dc374fc64, 2011-09-29 (annotated)
- Committer:
- okini3939
- Date:
- Thu Sep 29 16:42:34 2011 +0000
- Revision:
- 1:c59dc374fc64
- Parent:
- 0:629617d401de
- Child:
- 2:0753f1ed1dec
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:629617d401de | 1 | /* |
okini3939 | 0:629617d401de | 2 | * Control Art-Net from freepascal & delphi |
okini3939 | 0:629617d401de | 3 | * (c) Rowan Maclachlan (hippy) rowanmac@optusnet.com.au [15d/01m/06y] |
okini3939 | 0:629617d401de | 4 | * |
okini3939 | 0:629617d401de | 5 | * Free for personal not-for-profit use only, please contact me if you are |
okini3939 | 0:629617d401de | 6 | * using it in a commercial product, as i would like a copy :) |
okini3939 | 0:629617d401de | 7 | * |
okini3939 | 0:629617d401de | 8 | * http://members.westnet.com.au/rowanmac/ |
okini3939 | 0:629617d401de | 9 | * |
okini3939 | 0:629617d401de | 10 | * for mbed ported by Suga 2011 |
okini3939 | 0:629617d401de | 11 | */ |
okini3939 | 0:629617d401de | 12 | |
okini3939 | 0:629617d401de | 13 | /** @file |
okini3939 | 0:629617d401de | 14 | */ |
okini3939 | 0:629617d401de | 15 | |
okini3939 | 0:629617d401de | 16 | #include "mbed.h" |
okini3939 | 0:629617d401de | 17 | #include "EthernetNetIf.h" |
okini3939 | 0:629617d401de | 18 | #include "UDPSocket.h" |
okini3939 | 0:629617d401de | 19 | |
okini3939 | 0:629617d401de | 20 | //#define ArtMaxUniv 16 // Universe |
okini3939 | 0:629617d401de | 21 | #define ArtMaxUniv 4 // Universe |
okini3939 | 0:629617d401de | 22 | |
okini3939 | 0:629617d401de | 23 | //#define SizeRecvBuffer (530*4) |
okini3939 | 0:629617d401de | 24 | #define SizeRecvBuffer 700 |
okini3939 | 0:629617d401de | 25 | |
okini3939 | 0:629617d401de | 26 | // Art-Net Standard Stuff |
okini3939 | 0:629617d401de | 27 | #define ArtHeaderID "Art-Net" // packet header |
okini3939 | 0:629617d401de | 28 | #define ArtUDPPort 0x1936 // UDP port 6454 for Art-Net |
okini3939 | 0:629617d401de | 29 | #define ArtVersion 14 // Art-Net version |
okini3939 | 0:629617d401de | 30 | |
okini3939 | 0:629617d401de | 31 | // Art-Net OpCodes - defines type of packet |
okini3939 | 0:629617d401de | 32 | #define OP_Output 0x5000 //Art-Net DMX Packet 'Output' |
okini3939 | 0:629617d401de | 33 | #define OP_Poll 0x2000 // ArtPoll |
okini3939 | 0:629617d401de | 34 | #define OP_PollReply 0x2100 // ArtPoll Reply |
okini3939 | 0:629617d401de | 35 | |
okini3939 | 0:629617d401de | 36 | #define StyleNode 0 |
okini3939 | 0:629617d401de | 37 | #define StyleServer 1 |
okini3939 | 0:629617d401de | 38 | |
okini3939 | 0:629617d401de | 39 | #define STR_LongName "MbedArtNode - By Suga (2011), Rowan Maclachlan (2005)" |
okini3939 | 0:629617d401de | 40 | #define STR_ShortName "MbedArtNode" |
okini3939 | 0:629617d401de | 41 | |
okini3939 | 0:629617d401de | 42 | |
okini3939 | 0:629617d401de | 43 | // a DMX universe |
okini3939 | 0:629617d401de | 44 | //#define DMXArray = array[0..511] of byte; |
okini3939 | 0:629617d401de | 45 | |
okini3939 | 0:629617d401de | 46 | struct ArtAddr { |
okini3939 | 0:629617d401de | 47 | unsigned char IP[4]; // ip addess 0.1.2.3 |
okini3939 | 0:629617d401de | 48 | unsigned short Port; |
okini3939 | 0:629617d401de | 49 | } __attribute__((packed)); |
okini3939 | 0:629617d401de | 50 | |
okini3939 | 0:629617d401de | 51 | struct ArtPacketHeader { |
okini3939 | 0:629617d401de | 52 | char ID[8]; |
okini3939 | 0:629617d401de | 53 | unsigned short OpCode; // 0x5000 |
okini3939 | 0:629617d401de | 54 | } __attribute__((packed)); |
okini3939 | 0:629617d401de | 55 | |
okini3939 | 0:629617d401de | 56 | |
okini3939 | 0:629617d401de | 57 | // dmx transport packet |
okini3939 | 0:629617d401de | 58 | struct ArtDMX_Packet { |
okini3939 | 0:629617d401de | 59 | char ID[8]; |
okini3939 | 0:629617d401de | 60 | unsigned short OpCode; // 0x5000 |
okini3939 | 0:629617d401de | 61 | unsigned char VersionH; // 0 |
okini3939 | 0:629617d401de | 62 | unsigned char Version; // 14 |
okini3939 | 0:629617d401de | 63 | unsigned char Sequence; // 0 |
okini3939 | 0:629617d401de | 64 | unsigned char Physical; // 0 |
okini3939 | 0:629617d401de | 65 | unsigned short Universes; |
okini3939 | 0:629617d401de | 66 | unsigned short Length; // size of data segment |
okini3939 | 0:629617d401de | 67 | unsigned char Data[512]; // data segment |
okini3939 | 0:629617d401de | 68 | } __attribute__((packed)); |
okini3939 | 0:629617d401de | 69 | |
okini3939 | 0:629617d401de | 70 | struct ArtPoll_Packet { |
okini3939 | 0:629617d401de | 71 | char ID[8]; |
okini3939 | 0:629617d401de | 72 | unsigned short OpCode; // 0x5000 |
okini3939 | 0:629617d401de | 73 | unsigned char VersionH; // 0 |
okini3939 | 0:629617d401de | 74 | unsigned char Version; // 14 |
okini3939 | 0:629617d401de | 75 | unsigned char TalkToMe; // 0 |
okini3939 | 0:629617d401de | 76 | } __attribute__((packed)); |
okini3939 | 0:629617d401de | 77 | |
okini3939 | 0:629617d401de | 78 | // a responce to a artpoll packet |
okini3939 | 0:629617d401de | 79 | struct ArtPollReply_Packet { |
okini3939 | 0:629617d401de | 80 | char ID[8]; |
okini3939 | 0:629617d401de | 81 | unsigned short OpCode; // 0x2000 |
okini3939 | 0:629617d401de | 82 | struct ArtAddr Addr; // our ip address |
okini3939 | 0:629617d401de | 83 | unsigned char VersionH; |
okini3939 | 0:629617d401de | 84 | unsigned char Version; |
okini3939 | 0:629617d401de | 85 | unsigned char SubSwitchH; |
okini3939 | 0:629617d401de | 86 | unsigned char SubSwitch; |
okini3939 | 0:629617d401de | 87 | unsigned short OEM; |
okini3939 | 0:629617d401de | 88 | char UbeaVersion; |
okini3939 | 0:629617d401de | 89 | char Status; |
okini3939 | 0:629617d401de | 90 | unsigned short EstaMan; |
okini3939 | 0:629617d401de | 91 | char ShortName[18]; |
okini3939 | 0:629617d401de | 92 | char LongName[64]; |
okini3939 | 0:629617d401de | 93 | char NodeReport[64]; |
okini3939 | 0:629617d401de | 94 | unsigned char NumPortsH; |
okini3939 | 0:629617d401de | 95 | unsigned char NumPorts; |
okini3939 | 0:629617d401de | 96 | unsigned char PortType[4]; |
okini3939 | 0:629617d401de | 97 | unsigned char GoodInput[4]; |
okini3939 | 0:629617d401de | 98 | unsigned char GoodOutput[4]; |
okini3939 | 0:629617d401de | 99 | unsigned char Swin[4]; |
okini3939 | 0:629617d401de | 100 | unsigned char Swout[4]; |
okini3939 | 0:629617d401de | 101 | unsigned char SwVideo; |
okini3939 | 0:629617d401de | 102 | unsigned char SwMacro; |
okini3939 | 0:629617d401de | 103 | unsigned char SwRemote; |
okini3939 | 0:629617d401de | 104 | unsigned char Spare[3]; // three spare bytes |
okini3939 | 0:629617d401de | 105 | unsigned char Style; |
okini3939 | 0:629617d401de | 106 | unsigned char Mac[6]; |
okini3939 | 0:629617d401de | 107 | unsigned char Padding[32]; // padding |
okini3939 | 0:629617d401de | 108 | } __attribute__((packed)); |
okini3939 | 0:629617d401de | 109 | |
okini3939 | 1:c59dc374fc64 | 110 | /** DmxArtNet class |
okini3939 | 1:c59dc374fc64 | 111 | */ |
okini3939 | 0:629617d401de | 112 | class DmxArtNet { |
okini3939 | 0:629617d401de | 113 | public: |
okini3939 | 0:629617d401de | 114 | IpAddr BindIpAddress; // Local IP Address to bind onto |
okini3939 | 0:629617d401de | 115 | IpAddr BCastAddress; // address to broadcast on |
okini3939 | 0:629617d401de | 116 | |
okini3939 | 0:629617d401de | 117 | /** create network socket, setup for NetworkWork |
okini3939 | 0:629617d401de | 118 | * @return true if socket creation succeded |
okini3939 | 0:629617d401de | 119 | */ |
okini3939 | 0:629617d401de | 120 | int Init(); |
okini3939 | 0:629617d401de | 121 | |
okini3939 | 0:629617d401de | 122 | /** socket shutdown |
okini3939 | 0:629617d401de | 123 | */ |
okini3939 | 0:629617d401de | 124 | void Done(); |
okini3939 | 0:629617d401de | 125 | |
okini3939 | 0:629617d401de | 126 | /** last error |
okini3939 | 0:629617d401de | 127 | * @retval 0 ok |
okini3939 | 0:629617d401de | 128 | */ |
okini3939 | 0:629617d401de | 129 | int LastError() { |
okini3939 | 0:629617d401de | 130 | return LError; |
okini3939 | 0:629617d401de | 131 | } |
okini3939 | 0:629617d401de | 132 | /** returns descrip of last error |
okini3939 | 0:629617d401de | 133 | * @return descrip of last error |
okini3939 | 0:629617d401de | 134 | */ |
okini3939 | 0:629617d401de | 135 | char *LastErrorString() { |
okini3939 | 0:629617d401de | 136 | return LErrorString; |
okini3939 | 0:629617d401de | 137 | } |
okini3939 | 0:629617d401de | 138 | /** clear error |
okini3939 | 0:629617d401de | 139 | */ |
okini3939 | 0:629617d401de | 140 | void ClearError(); |
okini3939 | 0:629617d401de | 141 | |
okini3939 | 0:629617d401de | 142 | /** recieve our own packets |
okini3939 | 0:629617d401de | 143 | * @return recieve our own packets |
okini3939 | 0:629617d401de | 144 | */ |
okini3939 | 0:629617d401de | 145 | int loopback() { |
okini3939 | 0:629617d401de | 146 | return net_loopback; |
okini3939 | 0:629617d401de | 147 | } |
okini3939 | 0:629617d401de | 148 | |
okini3939 | 0:629617d401de | 149 | /** Send |
okini3939 | 0:629617d401de | 150 | * send an array [0..511] of (length) bytes representing the dmx you want to send to |
okini3939 | 0:629617d401de | 151 | * univ[0..15] , physical is info only, can be the universe number or port of device |
okini3939 | 0:629617d401de | 152 | */ |
okini3939 | 0:629617d401de | 153 | int Send_ArtDmx(int univ, int physical, char *data, int length); |
okini3939 | 0:629617d401de | 154 | /** execute often |
okini3939 | 0:629617d401de | 155 | * returns true if something happened |
okini3939 | 0:629617d401de | 156 | * @return 1 something happened |
okini3939 | 0:629617d401de | 157 | */ |
okini3939 | 0:629617d401de | 158 | int Work(); |
okini3939 | 0:629617d401de | 159 | |
okini3939 | 0:629617d401de | 160 | unsigned char *DmxIn[ArtMaxUniv]; // Recieved ArtDMX, 16 Dmx Arrays |
okini3939 | 0:629617d401de | 161 | int LastRecievedUniverse; // the number of the last recieved universe |
okini3939 | 0:629617d401de | 162 | |
okini3939 | 0:629617d401de | 163 | struct ArtPollReply_Packet ArtPollReply; // a response to ArtPoll |
okini3939 | 0:629617d401de | 164 | // contains many variables you may set |
okini3939 | 0:629617d401de | 165 | |
okini3939 | 0:629617d401de | 166 | void InitArtPollReplyDefaults(); |
okini3939 | 0:629617d401de | 167 | int SendArtPollReply(); |
okini3939 | 0:629617d401de | 168 | |
okini3939 | 0:629617d401de | 169 | private: |
okini3939 | 0:629617d401de | 170 | UDPSocket _art; // the network socket |
okini3939 | 0:629617d401de | 171 | struct ArtPacketHeader ArtHead; // packet header |
okini3939 | 0:629617d401de | 172 | struct ArtDMX_Packet ArtDMX; // an Art-Net DMX Packet |
okini3939 | 0:629617d401de | 173 | struct ArtPoll_Packet ArtPoll; // an Art-Poll Packet |
okini3939 | 0:629617d401de | 174 | unsigned char buf[SizeRecvBuffer]; // a temp buffer |
okini3939 | 0:629617d401de | 175 | struct ArtAddr localaddr; |
okini3939 | 0:629617d401de | 176 | int LError; |
okini3939 | 0:629617d401de | 177 | char LErrorString[80]; |
okini3939 | 0:629617d401de | 178 | |
okini3939 | 0:629617d401de | 179 | int makeword16 (int lsb, int msb); |
okini3939 | 0:629617d401de | 180 | |
okini3939 | 0:629617d401de | 181 | void Init_ArtDMX(); |
okini3939 | 0:629617d401de | 182 | void SocketErrorOccured(char *proc); |
okini3939 | 0:629617d401de | 183 | |
okini3939 | 0:629617d401de | 184 | void on_UDPSocketEvent (UDPSocketEvent e); |
okini3939 | 0:629617d401de | 185 | |
okini3939 | 0:629617d401de | 186 | int net_loopback; // if true, then listen to our own packets |
okini3939 | 0:629617d401de | 187 | // is crap and does not work |
okini3939 | 0:629617d401de | 188 | Host RemoteSin; |
okini3939 | 0:629617d401de | 189 | volatile int rxlen; |
okini3939 | 0:629617d401de | 190 | }; |