ArtNet node to drive TM1809 LED strips

Dependencies:   DMX DmxArtNet EthernetNetIf mbed

Fork of ArtNode by Suga koubou

Committer:
gipmad
Date:
Tue Sep 24 20:41:05 2013 +0000
Revision:
1:6ea5c460fdf2
Parent:
0:e3d0bc58141e
Child:
2:6d6e8afeb3b7
Funziona!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:e3d0bc58141e 1 /*
okini3939 0:e3d0bc58141e 2 * "Node" Device
okini3939 0:e3d0bc58141e 3 * "Universe" 512 DMX datas
okini3939 0:e3d0bc58141e 4 * "Sub-Net" 16 Universe
okini3939 0:e3d0bc58141e 5 * "Server" Controller
okini3939 0:e3d0bc58141e 6 *
okini3939 0:e3d0bc58141e 7 * can use over 40 Sub-Net on network.
okini3939 0:e3d0bc58141e 8 */
okini3939 0:e3d0bc58141e 9
okini3939 0:e3d0bc58141e 10 #include "mbed.h"
okini3939 0:e3d0bc58141e 11 #include "EthernetNetIf.h"
okini3939 0:e3d0bc58141e 12 #include "DmxArtNet.h"
okini3939 0:e3d0bc58141e 13 #include "DMX.h"
okini3939 0:e3d0bc58141e 14
okini3939 0:e3d0bc58141e 15
okini3939 0:e3d0bc58141e 16 #define LED_NET_ACT_ON led_yk = 0
okini3939 0:e3d0bc58141e 17 #define LED_NET_ACT_OFF led_yk = 1
okini3939 0:e3d0bc58141e 18 #define LED_NET_G_ON led_gayk = 1; led_gkya = 0
okini3939 0:e3d0bc58141e 19 #define LED_NET_Y_ON led_gayk = 0; led_gkya = 1
okini3939 0:e3d0bc58141e 20 #define LED_NET_GY_OFF led_gayk = 0; led_gkya = 0
okini3939 0:e3d0bc58141e 21
okini3939 0:e3d0bc58141e 22 extern "C" void mbed_mac_address(char *s);
okini3939 0:e3d0bc58141e 23
okini3939 0:e3d0bc58141e 24 DigitalOut led_red(p22), led_yellow(p23);
okini3939 0:e3d0bc58141e 25 DigitalOut led_gayk(p24),led_gkya(p25), led_yk(p26);
okini3939 0:e3d0bc58141e 26 DigitalIn eth_link(P1_25), eth_speed(P1_26);
okini3939 0:e3d0bc58141e 27 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
okini3939 0:e3d0bc58141e 28 EthernetNetIf *eth;
okini3939 0:e3d0bc58141e 29 DmxArtNet art;
okini3939 0:e3d0bc58141e 30 DMX dmx1(p13, p14);
okini3939 0:e3d0bc58141e 31 DMX dmx2(p28, p27);
okini3939 0:e3d0bc58141e 32 Serial pc(USBTX, USBRX);
okini3939 0:e3d0bc58141e 33
gipmad 1:6ea5c460fdf2 34 SPI spi(p11, p12, p13); // mosi, miso, sclk
gipmad 1:6ea5c460fdf2 35
okini3939 0:e3d0bc58141e 36 void no_memory () {
okini3939 0:e3d0bc58141e 37 printf("panic: can't allocate to memory!\r\n");
okini3939 0:e3d0bc58141e 38 exit(-1);
okini3939 0:e3d0bc58141e 39 }
okini3939 0:e3d0bc58141e 40
okini3939 0:e3d0bc58141e 41 void shutdown () {
okini3939 0:e3d0bc58141e 42 art.ArtPollReply.NumPorts = 0;
okini3939 0:e3d0bc58141e 43 strcpy(art.ArtPollReply.NodeReport, "Shutdown");
okini3939 0:e3d0bc58141e 44 art.SendArtPollReply();
okini3939 0:e3d0bc58141e 45 art.Done();
okini3939 0:e3d0bc58141e 46 }
okini3939 0:e3d0bc58141e 47
gipmad 1:6ea5c460fdf2 48 void PutLed(long GRB)
gipmad 1:6ea5c460fdf2 49 {
gipmad 1:6ea5c460fdf2 50 int SpiPack = 0;
gipmad 1:6ea5c460fdf2 51 int c = 0;
gipmad 1:6ea5c460fdf2 52 int i;
gipmad 1:6ea5c460fdf2 53 for(i = 0; i < 24; i++)
gipmad 1:6ea5c460fdf2 54 {
gipmad 1:6ea5c460fdf2 55 if(GRB & ((long)(1) << (23-i)))
gipmad 1:6ea5c460fdf2 56 SpiPack += 6;
gipmad 1:6ea5c460fdf2 57 else
gipmad 1:6ea5c460fdf2 58 SpiPack += 4;
gipmad 1:6ea5c460fdf2 59
gipmad 1:6ea5c460fdf2 60 if(c++ >= 3)
gipmad 1:6ea5c460fdf2 61 {
gipmad 1:6ea5c460fdf2 62
gipmad 1:6ea5c460fdf2 63 // First don't write to the FIFO buffer if it is full
gipmad 1:6ea5c460fdf2 64 while (!(LPC_SSP0->SR & 2)) // While TNF-Bit = 0 (FIFO full)...
gipmad 1:6ea5c460fdf2 65 ; // Wait
gipmad 1:6ea5c460fdf2 66 LPC_SSP0->DR = SpiPack; // Write to FIFO buffer
gipmad 1:6ea5c460fdf2 67
gipmad 1:6ea5c460fdf2 68
gipmad 1:6ea5c460fdf2 69 // spi.write(SpiPack);
gipmad 1:6ea5c460fdf2 70 SpiPack = 0;
gipmad 1:6ea5c460fdf2 71 c = 0;
gipmad 1:6ea5c460fdf2 72 }
gipmad 1:6ea5c460fdf2 73
gipmad 1:6ea5c460fdf2 74 SpiPack = SpiPack << 3;
gipmad 1:6ea5c460fdf2 75
gipmad 1:6ea5c460fdf2 76 }
gipmad 1:6ea5c460fdf2 77 }
gipmad 1:6ea5c460fdf2 78
gipmad 1:6ea5c460fdf2 79
okini3939 0:e3d0bc58141e 80 int main () {
okini3939 0:e3d0bc58141e 81 int i, u;
okini3939 0:e3d0bc58141e 82 char mac[6];
okini3939 0:e3d0bc58141e 83 IpAddr ip;
okini3939 0:e3d0bc58141e 84 EthernetErr ethErr;
gipmad 1:6ea5c460fdf2 85
gipmad 1:6ea5c460fdf2 86 uint32_t PixIn;
gipmad 1:6ea5c460fdf2 87
gipmad 1:6ea5c460fdf2 88 spi.format(12,0); //4 bit alla volta
gipmad 1:6ea5c460fdf2 89 //spi.frequency(1000000);
gipmad 1:6ea5c460fdf2 90 spi.frequency(3125000);
gipmad 1:6ea5c460fdf2 91
gipmad 1:6ea5c460fdf2 92
gipmad 1:6ea5c460fdf2 93 /* PutLed(0);
gipmad 1:6ea5c460fdf2 94 PutLed(0);
gipmad 1:6ea5c460fdf2 95 PutLed(0);
gipmad 1:6ea5c460fdf2 96 PutLed(0);
gipmad 1:6ea5c460fdf2 97 PutLed(0);
gipmad 1:6ea5c460fdf2 98 PutLed(0);
gipmad 1:6ea5c460fdf2 99 PutLed(0);
gipmad 1:6ea5c460fdf2 100 PutLed(0);
gipmad 1:6ea5c460fdf2 101 PutLed(0);
gipmad 1:6ea5c460fdf2 102 wait_us(25);*/
gipmad 1:6ea5c460fdf2 103
gipmad 1:6ea5c460fdf2 104 //while(1){};
okini3939 0:e3d0bc58141e 105
okini3939 0:e3d0bc58141e 106 set_new_handler(no_memory); // new handler function
okini3939 0:e3d0bc58141e 107
okini3939 0:e3d0bc58141e 108 pc.baud(112500);
okini3939 0:e3d0bc58141e 109
okini3939 0:e3d0bc58141e 110 eth_link.mode(PullUp);
okini3939 0:e3d0bc58141e 111 eth_speed.mode(PullUp);
okini3939 0:e3d0bc58141e 112
okini3939 0:e3d0bc58141e 113 if (! eth_link) {
okini3939 0:e3d0bc58141e 114 LED_NET_G_ON;
okini3939 0:e3d0bc58141e 115 }
okini3939 0:e3d0bc58141e 116 LED_NET_ACT_ON;
okini3939 0:e3d0bc58141e 117
okini3939 0:e3d0bc58141e 118 mbed_mac_address(mac);
okini3939 0:e3d0bc58141e 119 ip = IpAddr(2, mac[3], mac[4], mac[5]);
okini3939 0:e3d0bc58141e 120 eth = new EthernetNetIf(ip, IpAddr(255,0,0,0), IpAddr(0,0,0,0), IpAddr(0,0,0,0));
okini3939 0:e3d0bc58141e 121 ethErr = eth->setup();
okini3939 0:e3d0bc58141e 122 if (ethErr) {
okini3939 0:e3d0bc58141e 123 LED_NET_Y_ON;
okini3939 0:e3d0bc58141e 124 led_red = 1;
okini3939 0:e3d0bc58141e 125 return -1;
okini3939 0:e3d0bc58141e 126 }
okini3939 0:e3d0bc58141e 127 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]);
okini3939 0:e3d0bc58141e 128
okini3939 0:e3d0bc58141e 129 art.BindIpAddress = ip;
okini3939 0:e3d0bc58141e 130 art.BCastAddress = IpAddr(2,255,255,255);
okini3939 0:e3d0bc58141e 131
okini3939 0:e3d0bc58141e 132 art.InitArtPollReplyDefaults();
okini3939 0:e3d0bc58141e 133 // Device
okini3939 0:e3d0bc58141e 134 art.ArtPollReply.PortType[0] = 128; // output
okini3939 0:e3d0bc58141e 135 art.ArtPollReply.PortType[1] = 128; // output
okini3939 0:e3d0bc58141e 136 art.ArtPollReply.PortType[2] = 64; // input
okini3939 0:e3d0bc58141e 137 art.ArtPollReply.GoodInput[2] = 4;
okini3939 0:e3d0bc58141e 138 art.ArtPollReply.PortType[3] = 64; // input
okini3939 0:e3d0bc58141e 139 art.ArtPollReply.GoodInput[3] = 4;
okini3939 0:e3d0bc58141e 140
okini3939 0:e3d0bc58141e 141 art.Init();
okini3939 0:e3d0bc58141e 142 art.SendArtPollReply(); // announce to art-net nodes
okini3939 0:e3d0bc58141e 143
okini3939 0:e3d0bc58141e 144 while (1) {
okini3939 0:e3d0bc58141e 145 Net::poll();
okini3939 0:e3d0bc58141e 146 if (! eth_link) {
okini3939 0:e3d0bc58141e 147 LED_NET_G_ON;
okini3939 0:e3d0bc58141e 148 } else {
okini3939 0:e3d0bc58141e 149 LED_NET_GY_OFF;
okini3939 0:e3d0bc58141e 150 }
gipmad 1:6ea5c460fdf2 151
okini3939 0:e3d0bc58141e 152 if (art.Work()) {
gipmad 1:6ea5c460fdf2 153 u = art.LastRecievedUniverse;
gipmad 1:6ea5c460fdf2 154 if(u == 0){
gipmad 1:6ea5c460fdf2 155 led1 = 1;
gipmad 1:6ea5c460fdf2 156 for(i = 0; i < 510; i += 3)
gipmad 1:6ea5c460fdf2 157 {
gipmad 1:6ea5c460fdf2 158 PixIn = art.DmxIn[u][i+1];
gipmad 1:6ea5c460fdf2 159 PixIn <<= 8;
gipmad 1:6ea5c460fdf2 160 PixIn += art.DmxIn[u][i];
gipmad 1:6ea5c460fdf2 161 PixIn <<= 8;
gipmad 1:6ea5c460fdf2 162 PixIn += art.DmxIn[u][i+2];
gipmad 1:6ea5c460fdf2 163 PutLed(PixIn);
gipmad 1:6ea5c460fdf2 164 }
gipmad 1:6ea5c460fdf2 165 wait_us(25);
gipmad 1:6ea5c460fdf2 166 led1 = 0;
gipmad 1:6ea5c460fdf2 167 }
gipmad 1:6ea5c460fdf2 168 pc.printf("recv, node %d, data %x %x %x\r\n", u, art.DmxIn[u][0], art.DmxIn[u][1], art.DmxIn[u][2]);
gipmad 1:6ea5c460fdf2 169 /*if (u == 0) {
gipmad 1:6ea5c460fdf2 170 led1 = 1;
gipmad 1:6ea5c460fdf2 171 for (i = 0; i < 512; i ++) {
gipmad 1:6ea5c460fdf2 172 dmx1.put(i, art.DmxIn[u][i]);
gipmad 1:6ea5c460fdf2 173 }
gipmad 1:6ea5c460fdf2 174 } else
gipmad 1:6ea5c460fdf2 175 if (u == 1) {
gipmad 1:6ea5c460fdf2 176 led2 = 1;
gipmad 1:6ea5c460fdf2 177 for (i = 0; i < 512; i ++) {
gipmad 1:6ea5c460fdf2 178 dmx2.put(i, art.DmxIn[u][i]);
gipmad 1:6ea5c460fdf2 179 }
gipmad 1:6ea5c460fdf2 180 }*/
gipmad 1:6ea5c460fdf2 181 // pc.printf("recv, node %d, data %d\r\n", u, art.DmxIn[u][0]);
gipmad 1:6ea5c460fdf2 182 }
gipmad 1:6ea5c460fdf2 183
gipmad 1:6ea5c460fdf2 184
gipmad 1:6ea5c460fdf2 185 /*if (art.Work()) {
okini3939 0:e3d0bc58141e 186 LED_NET_ACT_ON;
okini3939 0:e3d0bc58141e 187 led_yellow = 1;
okini3939 0:e3d0bc58141e 188 u = art.LastRecievedUniverse;
okini3939 0:e3d0bc58141e 189 if (u == 0) {
okini3939 0:e3d0bc58141e 190 led1 = 1;
okini3939 0:e3d0bc58141e 191 for (i = 0; i < 512; i ++) {
okini3939 0:e3d0bc58141e 192 dmx1.put(i, art.DmxIn[u][i]);
okini3939 0:e3d0bc58141e 193 }
okini3939 0:e3d0bc58141e 194 } else
okini3939 0:e3d0bc58141e 195 if (u == 1) {
okini3939 0:e3d0bc58141e 196 led2 = 1;
okini3939 0:e3d0bc58141e 197 for (i = 0; i < 512; i ++) {
okini3939 0:e3d0bc58141e 198 dmx2.put(i, art.DmxIn[u][i]);
okini3939 0:e3d0bc58141e 199 }
okini3939 0:e3d0bc58141e 200 }
okini3939 0:e3d0bc58141e 201 pc.printf("recv, node %d, data %d\r\n", u, art.DmxIn[u][0]);
okini3939 0:e3d0bc58141e 202 led1 = 0;
okini3939 0:e3d0bc58141e 203 led2 = 0;
okini3939 0:e3d0bc58141e 204 led_yellow = 0;
gipmad 1:6ea5c460fdf2 205 }*/
okini3939 0:e3d0bc58141e 206
gipmad 1:6ea5c460fdf2 207 /*if (dmx1.is_recived) {
okini3939 0:e3d0bc58141e 208 led3 = 1;
okini3939 0:e3d0bc58141e 209 led_yellow = 1;
okini3939 0:e3d0bc58141e 210 dmx1.is_recived = 0;
okini3939 0:e3d0bc58141e 211 u = 0;
okini3939 0:e3d0bc58141e 212 for (i = 0; i < 512; i ++) {
okini3939 0:e3d0bc58141e 213 art.DmxIn[u][i] = dmx1.get(i);
okini3939 0:e3d0bc58141e 214 }
okini3939 0:e3d0bc58141e 215 LED_NET_ACT_ON;
okini3939 0:e3d0bc58141e 216 art.ArtPollReply.GoodInput[u] = 128;
okini3939 0:e3d0bc58141e 217 art.Send_ArtDmx(u, 0, (char*)art.DmxIn[u], 512);
okini3939 0:e3d0bc58141e 218 pc.printf("send, node %d, data %d\r\n", u, art.DmxIn[u][0]);
okini3939 0:e3d0bc58141e 219 led3 = 0;
okini3939 0:e3d0bc58141e 220 led_yellow = 0;
okini3939 0:e3d0bc58141e 221 }
okini3939 0:e3d0bc58141e 222
okini3939 0:e3d0bc58141e 223 if (dmx2.is_recived) {
okini3939 0:e3d0bc58141e 224 led4 = 1;
okini3939 0:e3d0bc58141e 225 led_yellow = 1;
okini3939 0:e3d0bc58141e 226 dmx2.is_recived = 0;
okini3939 0:e3d0bc58141e 227 u = 1;
okini3939 0:e3d0bc58141e 228 for (i = 0; i < 512; i ++) {
okini3939 0:e3d0bc58141e 229 art.DmxIn[u][i] = dmx2.get(i);
okini3939 0:e3d0bc58141e 230 }
okini3939 0:e3d0bc58141e 231 LED_NET_ACT_ON;
okini3939 0:e3d0bc58141e 232 art.ArtPollReply.GoodInput[u] = 128;
okini3939 0:e3d0bc58141e 233 art.Send_ArtDmx(u, 0, (char*)art.DmxIn[u], 512);
okini3939 0:e3d0bc58141e 234 pc.printf("send, node %d, data %d\r\n", u, art.DmxIn[u][0]);
okini3939 0:e3d0bc58141e 235 led4 = 0;
okini3939 0:e3d0bc58141e 236 led_yellow = 0;
okini3939 0:e3d0bc58141e 237 }
okini3939 0:e3d0bc58141e 238
gipmad 1:6ea5c460fdf2 239 LED_NET_ACT_OFF;*/
okini3939 0:e3d0bc58141e 240 }
okini3939 0:e3d0bc58141e 241 }
okini3939 0:e3d0bc58141e 242