#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCBundle.h>

// you can find this written on the board of some Arduino Ethernets or shields
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xC5, 0x3C };

int serverPort = 8000; //TouchOSC (incoming port)
int destPort = 9000; //TouchOSC (outgoing port)

//Create UDP message object
EthernetUDP Udp;

//resources
//http://tomshani.com/ep491-part1-arduino-dmx/
//http://opensoundcontrol.org/spec-1_0-examples#OSCaddress
//http://opensoundcontrol.org/spec-1_0
//http://trippylighting.com/teensy-arduino-ect/touchosc-and-arduino-oscuino/

void setup(){
Serial.begin(9600); //9600 for a "normal" Arduino board (Uno for example). 115200 for a Teensy ++2
Serial.println("OSC test");

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("Arduino IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Udp.begin(serverPort);
}

void loop(){
//process received messages
OSCMsgReceive();
}

void OSCMsgReceive(){
OSCMessage msgIN;
int size;
if((size = Udp.parsePacket())>0){
while(size--)
msgIN.fill(Udp.read());
if(!msgIN.hasError()){
msgIN.route("/1/fdr_RED_01",osc_dmx);
}
}
}

void osc_dmx(OSCMessage &msg, int addrOffset){

//bundleIN.route("/dmx", osc_dmx); // route /dmx messages to osc_dmx() ( ex. /dmx/2 134 )

char addrChar[100]; // char to store the OSC address
int addressLength = msg.getAddress(addrChar, addrOffset); //fills addrChar

/*
for (int i = 0; i <= 100; i++){
Serial.print(addrChar[i],HEX);
Serial.print(",");
}
Serial.println("END HEX");
*/

for (int i = 0; i <= 100; i++){
Serial.print(byte(addrChar[i]));
Serial.print(",");
}
Serial.println("END BYTE");

int value = msg.getInt(0); // get the value
//dmx_master.setChannelValue(channel, value); //output to DMX
}