diff --git a/inputs/Arduino/OSCuino/README.txt b/inputs/Arduino/OSCuino/README.txt new file mode 100644 index 0000000..8d5d033 --- /dev/null +++ b/inputs/Arduino/OSCuino/README.txt @@ -0,0 +1,52 @@ +OSCuino Library: Arduino Analog Input + + This example shows how to use an Arduino's analog inputs with + Wekinator. + + +Prerequisites + + You need an Arduino, any model. + + Install the Arduino IDE. + https://www.arduino.cc/en/Main/Software + + Connect one or more analog sensors to the Arduino's analog pins, + starting with A0. I used a two-axis joystick on A0 and A1. + + +Run the Demo + + Connect the Arduino to your computer. + + Open Analog_Inputs.ino in the Arduino IDE. + + Edit ANALOG_INPUT_COUNT to match the number of analog pins you're + using. + + Click Upload. + + Quit the Ardino IDE to release the serial port. + + Launch SLIPSerialToUDP for your platform. + + Select the correct serial port. + + Click START. + + Now SLIPSerialToUDP is sending OSC packets. Wekinator should be able + to receive them. + + +Build from Source + + Get the OSCuino library here. + https://github.com/CNMAT/OSC + + Get these Processing libraries. + + - UDP + http://ubaa.net/shared/processing/udp/ + + - controlP5 + http://www.sojamo.de/libraries/controlp5 diff --git a/inputs/Arduino/OSCuino/runThisInProcessing/SLIPSerialToUDP/SLIPSerialToUDP.pde b/inputs/Arduino/OSCuino/runThisInProcessing/SLIPSerialToUDP/SLIPSerialToUDP.pde new file mode 100644 index 0000000..63cc57c --- /dev/null +++ b/inputs/Arduino/OSCuino/runThisInProcessing/SLIPSerialToUDP/SLIPSerialToUDP.pde @@ -0,0 +1,383 @@ +import processing.serial.*; +//download at http://ubaa.net/shared/processing/udp/ +import hypermedia.net.*; +//download at www.sojamo.de/libraries/controlp5 +import controlP5.*; + +/************************************************************************************ + GUI + ************************************************************************************/ + +ControlP5 cp5; + +// DropdownList serialddl; +// DropdownList baudddl; +ScrollableList serialddl; +ScrollableList baudddl; +Textlabel arduinoLabel; +Textlabel UDPLabel; +Textlabel incomingPacket; +Button startButton; +Button stopButton; +Textfield ipAddressField; +Textfield incomingPortField; +Textfield outgoingPortField; + +void setupGUI() { + //the ControlP5 object + cp5 = new ControlP5(this); + + //start button + startButton = cp5.addButton("START") + .setPosition(200, 200) + .setSize(200, 19) + ; + + //stop button + stopButton = cp5.addButton("STOP") + .setPosition(200, 200) + .setSize(200, 19) + ; + stopButton.hide(); + + //Serial Port selector + // serialddl = cp5.addDropdownList("SerialPort") + serialddl = cp5.addScrollableList("SerialPort") + .setPosition(50, 100) + .setSize(200, 200) + ; + serialddl.setItemHeight(20); + serialddl.setBarHeight(15); + serialddl.getCaptionLabel().set("SELECT ARDUINO SERIAL PORT"); + serialddl.getCaptionLabel().getStyle().marginTop = 3; + serialddl.getCaptionLabel().getStyle().marginLeft = 3; + serialddl.getValueLabel().getStyle().marginTop = 3; + //set the serial options + String SerialList[] = Serial.list(); + for (int i=0;i// + serialddl.hide(); //<>// + baudddl.hide(); + startButton.hide(); + outgoingPortField.hide(); + incomingPortField.hide(); + ipAddressField.hide(); + incomingPacket.show(); + //show the stop button + stopButton.show(); +} + +void showControls() { + serialddl.show(); + baudddl.show(); + startButton.show(); + outgoingPortField.show(); + incomingPortField.show(); + ipAddressField.show(); + incomingPacket.hide(); + //hide the stop button + stopButton.hide(); +} + +public void STOP() { + stopSerial(); + stopUDP(); + showControls(); + applicationRunning = false; +} + +/************************************************************************************ + SERIAL + ************************************************************************************/ + +//the Serial communcation to the Arduino +Serial serial; + +String[] serialRateStrings = { + "300", "1200", "2400", "4800", "9600", "14400", + "19200", "28800", "38400", "57600", "115200" +}; +int baud = 115200; +int serialListNumber = 2; + +ArrayList serialBuffer = new ArrayList(); + +void setupSerial() { + println("opening " + Serial.list()[serialListNumber]); + serial = new Serial(this, Serial.list()[serialListNumber], baud); +} + +void stopSerial() { + serial.stop(); +} + +void serialEvent(Serial serial) { //<>// + //decode the message + while (serial.available () > 0) { + slipDecode(byte(serial.read())); + } +} + +void SerialSendToUDP() { + byte [] buffer = new byte[serialBuffer.size()]; + //copy the buffer over + for (int i = 0; i < serialBuffer.size(); i++) { + buffer[i] = serialBuffer.get(i); + } + //send it off + UDPSendBuffer(buffer); + //clear the buffer + serialBuffer.clear(); + //light up the indicator + drawIncomingSerial(); +} + +void serialSend(byte[] data) { + //encode the message and send it + for (int i = 0; i < data.length; i++){ + slipEncode(data[i]); + } + //write the eot + serial.write(eot); +} + +/************************************************************************************ + SLIP ENCODING + ************************************************************************************/ + +byte eot = byte(192); +byte slipesc = byte(219); +byte slipescend = byte(220); +byte slipescesc = byte(221); + +byte previousByte; + +void slipDecode(byte incoming) { + byte previous = previousByte; + previousByte = incoming; + //if the previous was the escape char + if (previous == slipesc) { + //if this one is the esc eot + if (incoming==slipescend) { + serialBuffer.add(eot); + } + else if (incoming==slipescesc) { + serialBuffer.add(slipesc); + } + } + else if (incoming==eot) { + //if it's the eot + //send off the packet + if (previous != eot) { + SerialSendToUDP(); + } + } + else if (incoming != slipesc) { + serialBuffer.add(incoming); + } +} + +void slipEncode(byte incoming) { + if(incoming == eot){ + serial.write(slipesc); + serial.write(slipescend); + } else if(incoming==slipesc) { + serial.write(slipesc); + serial.write(slipescesc); + } else { + serial.write(incoming); + } +} + + +/************************************************************************************ + UDP + ************************************************************************************/ + +//UDP communication +UDP udp; + +int inPort = 0; +int outPort = 6448; +String ipAddress = "127.0.0.1"; + +void setupUDP() { + udp = new UDP( this, inPort ); + // udp.log( true ); // <-- printout the connection activity + udp.listen( true ); +} + +void stopUDP() { + udp.close(); + +} + +void UDPSendBuffer(byte[] data) { + udp.send( data, ipAddress, outPort ); +} + +//called when UDP recieves some data +void receive( byte[] data) { + drawIncomingUDP(); + //send it over to serial + serialSend(data); +} + +/************************************************************************************ + SETUP/DRAW + ************************************************************************************/ + +void setup() { + // configure the screen size and frame rate + size(550, 250, P3D); + frameRate(30); + setupGUI(); +} + +void draw() { + background(128); + if (applicationRunning) { + drawIncomingPackets(); + } +} + + +/************************************************************************************ + VISUALIZING INCOMING PACKETS + ************************************************************************************/ + +int lastSerialPacket = 0; +int lastUDPPacket = 0; + +void drawIncomingPackets() { + //the serial packet + fill(0); + rect(75, 50, 100, 100); + //the udp packet + rect(325, 50, 100, 100); + int now = millis(); + int lightDuration = 75; + if (now - lastSerialPacket < lightDuration) { + fill(255); + rect(85, 60, 80, 80); + } + if (now - lastUDPPacket < lightDuration) { + fill(255); + rect(335, 60, 80, 80); + } +} + +void drawIncomingSerial() { + // println("drawIncomingSerial"); + lastSerialPacket = millis(); +} + +void drawIncomingUDP() { + lastUDPPacket = millis(); +} diff --git a/inputs/Arduino/OSCuino/runThisOnArduino/Analog_Inputs/Analog_Inputs.ino b/inputs/Arduino/OSCuino/runThisOnArduino/Analog_Inputs/Analog_Inputs.ino new file mode 100644 index 0000000..4b40da7 --- /dev/null +++ b/inputs/Arduino/OSCuino/runThisOnArduino/Analog_Inputs/Analog_Inputs.ino @@ -0,0 +1,52 @@ +#include + +/* + * Make an OSC message and send it over serial + */ + +#define ANALOG_INPUT_COUNT 2 +#define UPDATE_INTERVAL_MSEC 20 + +#ifdef BOARD_HAS_USB_SERIAL + +#include +SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB ); + +#else + +#include +SLIPEncodedSerial SLIPSerial(Serial1); + +#endif + +uint32_t next_time; + +void setup() +{ + // begin SLIPSerial just like Serial + SLIPSerial.begin(115200); + while (!Serial) + continue; + + next_time = millis() + UPDATE_INTERVAL_MSEC; +} + +void loop() +{ + uint32_t now = millis(); + if ((int32_t)(next_time - now) > 0) + return; + next_time += UPDATE_INTERVAL_MSEC; + + // the message wants an OSC address as first argument + OSCMessage msg("/wek/inputs"); + + // add all the analog inputs + for (int i = 0; i < ANALOG_INPUT_COUNT; i++) + msg.add((float)analogRead(i)); + + // SLIPSerial.beginPacket(); + msg.send(SLIPSerial); // send the bytes to the SLIP stream + SLIPSerial.endPacket(); // mark the end of the OSC Packet + msg.empty(); // free space occupied by message +}