Art-Net node http://mbed.org/users/okini3939/notebook/art-net/

Dependencies:   EthernetNetIf mbed DmxArtNet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * "Node" Device
00003  * "Universe" 512 DMX datas
00004  * "Sub-Net" 16 Universe
00005  * "Server" Controller
00006  *
00007  * can use over 40 Sub-Net on network.
00008  */
00009 
00010 #include "mbed.h"
00011 #include "EthernetNetIf.h"
00012 #include "DmxArtNet.h"
00013 #include "DMX.h"
00014 
00015 
00016 #define LED_NET_ACT_ON led_yk = 0
00017 #define LED_NET_ACT_OFF led_yk = 1
00018 #define LED_NET_G_ON led_gayk = 1; led_gkya = 0
00019 #define LED_NET_Y_ON led_gayk = 0; led_gkya = 1
00020 #define LED_NET_GY_OFF led_gayk = 0; led_gkya = 0
00021 
00022 extern "C" void mbed_mac_address(char *s);
00023 
00024 DigitalOut led_red(p22), led_yellow(p23);
00025 DigitalOut led_gayk(p24),led_gkya(p25), led_yk(p26);
00026 DigitalIn eth_link(P1_25), eth_speed(P1_26);
00027 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
00028 EthernetNetIf *eth;
00029 DmxArtNet art;
00030 DMX dmx1(p13, p14);
00031 DMX dmx2(p28, p27);
00032 Serial pc(USBTX, USBRX);
00033 
00034 void no_memory () {
00035     printf("panic: can't allocate to memory!\r\n");
00036     exit(-1);
00037 }
00038 
00039 void shutdown () {
00040    art.ArtPollReply.NumPorts = 0;
00041    strcpy(art.ArtPollReply.NodeReport, "Shutdown");
00042    art.SendArtPollReply();
00043    art.Done();
00044 }
00045 
00046 int main () {
00047     int i, u;
00048     char mac[6];
00049     IpAddr ip;
00050     EthernetErr ethErr;
00051 
00052     set_new_handler(no_memory); // new handler function
00053 
00054     pc.baud(112500);
00055 
00056     eth_link.mode(PullUp);
00057     eth_speed.mode(PullUp);
00058 
00059     if (! eth_link) {
00060         LED_NET_G_ON;
00061     }
00062     LED_NET_ACT_ON;
00063 
00064     mbed_mac_address(mac);
00065     ip = IpAddr(2, mac[3], mac[4], mac[5]);
00066     eth = new EthernetNetIf(ip, IpAddr(255,0,0,0), IpAddr(0,0,0,0), IpAddr(0,0,0,0));
00067     ethErr = eth->setup();
00068     if (ethErr) {
00069         LED_NET_Y_ON;
00070         led_red = 1;
00071         return -1;
00072     }
00073     pc.printf("Bind to interface: %d.%d.%d.%d\r\n", (unsigned char)ip[0], (unsigned char)ip[1], (unsigned char)ip[2], (unsigned char)ip[3]);
00074 
00075     art.BindIpAddress = ip;
00076     art.BCastAddress = IpAddr(2,255,255,255);
00077 
00078     art.InitArtPollReplyDefaults();
00079     // Device
00080     art.ArtPollReply.PortType[0] = 128; // output
00081     art.ArtPollReply.PortType[1] = 128; // output
00082     art.ArtPollReply.PortType[2] = 64; // input
00083     art.ArtPollReply.GoodInput[2] = 4;
00084     art.ArtPollReply.PortType[3] = 64; // input
00085     art.ArtPollReply.GoodInput[3] = 4;
00086 
00087     art.Init();
00088     art.SendArtPollReply(); // announce to art-net nodes
00089 
00090     while (1) {
00091         Net::poll();
00092         if (! eth_link) {
00093             LED_NET_G_ON;
00094         } else {
00095             LED_NET_GY_OFF;
00096         }
00097 
00098         if (art.Work()) {
00099             LED_NET_ACT_ON;
00100             led_yellow = 1;
00101             u = art.LastRecievedUniverse;
00102             if (u == 0) {
00103                 led1 = 1;
00104                 for (i = 0; i < 512; i ++) {
00105                     dmx1.put(i, art.DmxIn[u][i]);
00106                 }
00107             } else
00108             if (u == 1) {
00109                 led2 = 1;
00110                 for (i = 0; i < 512; i ++) {
00111                     dmx2.put(i, art.DmxIn[u][i]);
00112                 }
00113             }
00114             pc.printf("recv, node %d, data %d\r\n", u, art.DmxIn[u][0]);
00115             led1 = 0;
00116             led2 = 0;
00117             led_yellow = 0;
00118         }
00119         
00120         if (dmx1.is_recived) {
00121             led3 = 1;
00122             led_yellow = 1;
00123             dmx1.is_recived = 0;
00124             u = 0;
00125             for (i = 0; i < 512; i ++) {
00126                 art.DmxIn[u][i] = dmx1.get(i);
00127             }
00128             LED_NET_ACT_ON;
00129             art.ArtPollReply.GoodInput[u] = 128;
00130             art.Send_ArtDmx(u, 0, (char*)art.DmxIn[u], 512);
00131             pc.printf("send, node %d, data %d\r\n", u, art.DmxIn[u][0]);
00132             led3 = 0;
00133             led_yellow = 0;
00134         }
00135 
00136         if (dmx2.is_recived) {
00137             led4 = 1;
00138             led_yellow = 1;
00139             dmx2.is_recived = 0;
00140             u = 1;
00141             for (i = 0; i < 512; i ++) {
00142                 art.DmxIn[u][i] = dmx2.get(i);
00143             }
00144             LED_NET_ACT_ON;
00145             art.ArtPollReply.GoodInput[u] = 128;
00146             art.Send_ArtDmx(u, 0, (char*)art.DmxIn[u], 512);
00147             pc.printf("send, node %d, data %d\r\n", u, art.DmxIn[u][0]);
00148             led4 = 0;
00149             led_yellow = 0;
00150         }
00151 
00152         LED_NET_ACT_OFF;
00153     }
00154 }
00155