-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOSCuino_TouchOSC.ino
116 lines (87 loc) · 2.94 KB
/
OSCuino_TouchOSC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//DHCP-based OSC server test code
//for use with IDE 1.0.5
//for use with W5100 or W5200 based ethernet shields
#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[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// NOTE: Alternatively, you can assign a fixed IP to configure your
// Ethernet shield.
// byte ip[] = { 192, 168, 0, 154 };
int serverPort = 8000; //TouchOSC (incoming port)
int destPort = 9000; //TouchOSC (outgoing port)
int ledPin = 13; //pin 13 on Arduino Uno. Pin 6 on a Teensy++2
int ledState = LOW;
//Create UDP message object
EthernetUDP Udp;
void setup(){
Serial.begin(9600); //9600 for a "normal" Arduino board (Uno for example). 115200 for a Teensy ++2
Serial.println("OSC test");
// start the Ethernet connection:
// NOTE: Alternatively, you can assign a fixed IP to configure your
// Ethernet shield.
// Ethernet.begin(mac, ip);
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(".");
}
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("/OnOff/toggle1",toggleOnOff);
msgIN.route("/Fader/Value",funcValue);
}
}
}
void toggleOnOff(OSCMessage &msg, int addrOffset){
ledState = (boolean) msg.getFloat(0);
OSCMessage msgOUT("/OnOff/toggle1");
digitalWrite(ledPin, ledState);
msgOUT.add(ledState);
if (ledState) {
Serial.println("LED on");
}
else {
Serial.println("LED off");
}
ledState = !ledState; // toggle the state from HIGH to LOW to HIGH to LOW ...
//send osc message back to controll object in TouchOSC
//Local feedback is turned off in the TouchOSC interface.
//The button is turned on in TouchOSC interface whe the conrol receives this message.
Udp.beginPacket(Udp.remoteIP(), destPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void funcValue(OSCMessage &msg, int addrOffset ){
int value = msg.getFloat(0);
OSCMessage msgOUT("/Fader/Value");
Serial.print("Value = : ");
Serial.println(value);
msgOUT.add(value);
Udp.beginPacket(Udp.remoteIP(), destPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}