Lab_interaccio/2020/OSC_receive/OSC_receive.ino
2025-02-25 21:29:42 +01:00

128 lines
2.9 KiB
C++

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCBoards.h>
#include <OSCMessage.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 3);
IPAddress destIp(192, 168, 1, 11);
unsigned int localPort = 8000; // local port to listen on
unsigned int destPort = 9000; // TO SET SENDING PORT
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
OSCErrorCode error;
#define UDP_RX_PACKET_MAX_SIZE 8
uint32_t CMD[ 8 ];
void setup()
{
Serial.begin(115200);
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
Udp.begin(localPort);
}
void test(OSCMessage &msg)
{
Serial.println("test");
}
void cmdProcess( OSCMessage &msg )
{
unsigned int frequency = 0;
//Serial.println("CMD received");
if (msg.isInt(0)){
CMD[0] = msg.getInt(0);
} //otherwise it's a floating point frequency in Hz
if (msg.isInt(1)){
CMD[1] = msg.getInt(1);
} //otherwise it's a floating point frequency in Hz
}
void loop() {
////////////////////////////////////////////////////////////
/////// RUTINA TRATAMIENTO DE COMANDOS OSC /////////////
////////////////////////////////////////////////////////////
// SEND A OSC MESSAGE
//OSCMessage msg("/test");
//msg.add("hello, osc.");
//Udp.beginPacket(outIp, outPort);
//msg.send(Udp);
//Udp.endPacket();
//msg.empty();
//unsigned long currentMillis = millis();
//if (currentMillis - previousMillis >= interval)
//{
// previousMillis = currentMillis;
OSCMessage msg;
int size;
if ( (size = Udp.parsePacket()) > 0)
{
//Serial.print("mensaje recibido: ");
while (size--)
{
msg.fill(Udp.read());
// uint8_t packetBuffer[UDP_RX_PACKET_MAX_SIZE];
// Udp.read(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
// msg.fill(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
}
if (!msg.hasError())
{
//Serial.println("packetGuay");
//Serial.println(msg.match("/test"));
msg.dispatch("/test", test ); // this is how it is€ marked on the silkscreen
msg.dispatch("/cmd", cmdProcess ); // this is how it is marked on the silkscreen
}
else
{
error = msg.getError();
Serial.print("error: ");
Serial.println(error);
}
}
analogWrite(5, CMD[0]);
//Serial.println(CMD[0]);
}