Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "RNXV.h"
#include "settings.h"
#include <OSCMessage.h>
int led = 13; // For Teensy 3.1
RNXV rnxv = RNXV();
String usbSerialBuffer = "";
String cmds1[] = {
"set ip dhcp 1", // 0 means don't get IP form DHCP server. 1 means grab one va DHCP
"set wlan phrase " + passPhrase,
"set wlan ssid " + ssid,
"set wlan auth 4",
"set wlan join 1",
"set ip proto 3" , // should cover both TCP adn UDP
"set ip host " + remoteHost, // The remote host address
"set ip remote " + remotePort, // the remote host port
"set ip local " + localPort, // the remote host port
"set sys autoconn 1",
"" // End marker
};
void blink() {
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
}
void sendCommands(String cmds[]) {
Serial.println("Send command strings ...");
int i = 0;
while( cmds[i].length() > 0 ) {
Serial.println( cmds[i] );
rnxv.sendCommand(cmds[i], "<4.00>");
i++;
}
Serial.println("Command strings are done.");
}
void setup() {
pinMode(led, OUTPUT); // THIS IS IMPORTANT, or else you never see the light
Serial3.begin(9600); // UART, RX (D0), TX (D1) are connected to WiFly
rnxv.setUart(&Serial3); // Set up the object that wraps interaction with the WiFly
Serial.begin(9600); // USB serial
Serial.println("10");
delay(5000);
Serial.println("5");
delay(5000);
blink();
Serial.println("Are we ready?");
blink();
Serial.println("Send dollars ..");
rnxv.enterCommandMode();
sendCommands(cmds1);
rnxv.exitCommandMode();
}
void printMessageDetails(OSCMessage &msg) {
char addr[100];
for (int i=0;i<100;i++){ addr[i] = '\0'; }
msg.getAddress(addr, 0);
Serial.print(addr); Serial.print(" ,");
int msgSize = msg.size();
for(int i=0;i<msgSize;i++){
Serial.print(msg.getType(i));
}
Serial.print(" ");
for(int i=0;i<msgSize;i++) {
// The possible types: int, time, float, double, string, blob
// Punting on how to render Time and blobs
switch ( msg.getType(i) ){
case 'i':
Serial.print(msg.getInt(i));
break;
case 't':
// Serial.print(msg.getTime(i));
Serial.print("<time>");
break;
case 'f':
Serial.print(msg.getFloat(i));
break;
case 'd':
Serial.print(msg.getDouble(i));
break;
case 's':
char str[100];
msg.getString(i, str, 100);
Serial.print( str );
break;
case 'b':
Serial.print("<blob>");
break;
}
Serial.print(" ");
}
Serial.println("");
}
void loop() {
byte b;
uint8_t incomingBytes[100];
OSCMessage oscMessage;
for (int i=0;i<100;i++){
incomingBytes[i] = '\0';
}
int i = 0;
if (Serial3.available() > 0 ) {
while(Serial3.available() > 0) {
incomingBytes[i++] = Serial3.read();
delay(3);// Sadness. Without this we end up with a series of discrete reads, one byte on each call to loop();
}
oscMessage.fill(incomingBytes, i);
printMessageDetails(oscMessage);
oscMessage.dispatch("/test/double", oscDouble, 0);
// oscMessage.dispatch("/test/""*", oscDouble, 0); // Works with pattern matching too.
}
}
void oscDouble(OSCMessage &msg) {
if ( msg.isInt(0) ) {
int n = msg.getInt(0);
Serial.print("oscDouble has the i value ");
Serial.println(n);
OSCMessage msgReply("/test/doubled");
msgReply.add(n);
msgReply.add(n*2);
msgReply.send(Serial3);
} else if (msg.isFloat(0)) {
float f = msg.getFloat(0);
Serial.print("oscDouble has the f value ");
Serial.println(f);
OSCMessage msgReply("/test/doubled");
msgReply.add(f);
msgReply.add(f*2);
msgReply.send(Serial3);
} else {
Serial.println("oscDouble called but did not find the expected int or float");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.