Dependents:   ArtNode ArtNode DMXStation ArtNodeLED ... more



File content as of revision 0:629617d401de:

 * Control Art-Net from freepascal & delphi
 * (c) Rowan Maclachlan (hippy) [15d/01m/06y]
 * Free for personal not-for-profit use only, please contact me if you are
 * using it in a commercial product, as i would like a copy :)
 * for mbed ported by Suga 2011

/** @file

#include "mbed.h"
#include "EthernetNetIf.h"
#include "UDPSocket.h"

//#define ArtMaxUniv 16 // Universe
#define ArtMaxUniv 4 // Universe

//#define SizeRecvBuffer (530*4)
#define SizeRecvBuffer 700

// Art-Net Standard Stuff
#define ArtHeaderID "Art-Net" // packet header
#define ArtUDPPort 0x1936 // UDP port 6454 for Art-Net
#define ArtVersion 14    // Art-Net version

// Art-Net OpCodes - defines type of packet
#define OP_Output 0x5000 //Art-Net DMX Packet 'Output'
#define OP_Poll 0x2000 // ArtPoll
#define OP_PollReply 0x2100 // ArtPoll Reply

#define StyleNode 0
#define StyleServer 1

#define STR_LongName "MbedArtNode - By Suga (2011), Rowan Maclachlan (2005)"
#define STR_ShortName "MbedArtNode"

// a DMX universe
//#define DMXArray = array[0..511] of byte;

struct ArtAddr {
     unsigned char IP[4]; // ip addess
     unsigned short Port;
} __attribute__((packed));

struct ArtPacketHeader {
     char ID[8];
     unsigned short OpCode; // 0x5000
} __attribute__((packed));

// dmx transport packet
struct ArtDMX_Packet {
     char ID[8];
     unsigned short OpCode; // 0x5000
     unsigned char VersionH; // 0
     unsigned char Version; // 14
     unsigned char Sequence; // 0
     unsigned char Physical; // 0
     unsigned short Universes;
     unsigned short Length; // size of data segment
     unsigned char Data[512]; // data segment
} __attribute__((packed));

struct ArtPoll_Packet {
     char ID[8];
     unsigned short OpCode; // 0x5000
     unsigned char VersionH; // 0
     unsigned char Version; // 14
     unsigned char TalkToMe; // 0
} __attribute__((packed));

// a responce to a artpoll packet
struct ArtPollReply_Packet {
     char ID[8];
     unsigned short OpCode; // 0x2000
     struct ArtAddr Addr; // our ip address
     unsigned char VersionH;
     unsigned char Version;
     unsigned char SubSwitchH;
     unsigned char SubSwitch;
     unsigned short OEM;
     char UbeaVersion;
     char Status;
     unsigned short EstaMan;
     char ShortName[18];
     char LongName[64];
     char NodeReport[64];
     unsigned char NumPortsH;
     unsigned char NumPorts;
     unsigned char PortType[4];
     unsigned char GoodInput[4];
     unsigned char GoodOutput[4];
     unsigned char Swin[4];
     unsigned char Swout[4];
     unsigned char SwVideo;
     unsigned char SwMacro;
     unsigned char SwRemote;
     unsigned char Spare[3]; // three spare bytes
     unsigned char Style;
     unsigned char Mac[6];
     unsigned char Padding[32]; // padding
} __attribute__((packed));

class DmxArtNet {
    IpAddr BindIpAddress; // Local IP Address to bind onto
    IpAddr BCastAddress; // address to broadcast on

    /** create network socket, setup for NetworkWork
     * @return true if socket creation succeded
    int Init();

    /** socket shutdown
    void Done();

    /** last error
     * @retval 0 ok
    int LastError() {
        return LError;
    /** returns descrip of last error
     * @return descrip of last error
    char *LastErrorString() {
        return LErrorString;
    /** clear error
    void ClearError();

    /** recieve our own packets
     * @return recieve our own packets
    int loopback() {
        return net_loopback;

    /** Send
     * send an array [0..511] of (length) bytes representing the dmx you want to send to
     * univ[0..15] ,  physical is info only, can be the universe number or port of device
    int Send_ArtDmx(int univ, int physical, char *data, int length);
    /** execute often
     * returns true if something happened
     * @return 1 something happened
    int Work();

    unsigned char *DmxIn[ArtMaxUniv]; // Recieved ArtDMX, 16 Dmx Arrays
    int LastRecievedUniverse; // the number of the last recieved universe

    struct ArtPollReply_Packet ArtPollReply; // a response to ArtPoll
                                      // contains many variables you may set

    void InitArtPollReplyDefaults();
    int SendArtPollReply();

    UDPSocket _art; // the network socket
    struct ArtPacketHeader ArtHead; // packet header
    struct ArtDMX_Packet ArtDMX; // an Art-Net DMX Packet
    struct ArtPoll_Packet ArtPoll; // an Art-Poll Packet
    unsigned char buf[SizeRecvBuffer]; // a temp buffer
    struct ArtAddr localaddr;
    int LError;
    char LErrorString[80];

    int makeword16 (int lsb, int msb);

    void Init_ArtDMX();
    void SocketErrorOccured(char *proc);

    void on_UDPSocketEvent (UDPSocketEvent e);

    int net_loopback; // if true, then listen to our own packets
                      // is crap and does not work
    Host RemoteSin;
    volatile int rxlen;