Last active
January 18, 2018 05:58
-
-
Save kitschpatrol/550e378cb2c4fc7cfacf0884a7053aca to your computer and use it in GitHub Desktop.
Mindflex --> Arduino --> Processing --> OSC --> PD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Mindflex --> Arduino --> Processing --> OSC --> PD | |
// Eric Mika Jan 2018 | |
import processing.serial.*; | |
import netP5.*; | |
import oscP5.*; | |
Serial serial; | |
OscP5 oscProcessing; | |
NetAddress oscPD; | |
int packetCount = 0; | |
void setup() { | |
// Set up serial connection | |
println("Find your Arduino in the list below, note its [index]:\n"); | |
for (int i = 0; i < Serial.list().length; i++) { | |
println("[" + i + "] " + Serial.list()[i]); | |
} | |
// Put the index found above here: | |
serial = new Serial(this, Serial.list()[0], 9600); | |
// Watch for separate brain packets | |
serial.bufferUntil(10); | |
// Set up PD as OSC target | |
oscProcessing = new OscP5(this, 12000); | |
oscPD = new NetAddress("127.0.0.1", 13855); | |
} | |
void draw() { | |
// Nothing really to draw, this is just proxying data from the Arduino to OSC | |
} | |
void serialEvent(Serial p) { | |
// Split incoming packet on commas | |
// See https://github.com/kitschpatrol/Brain/blob/master/README.md for information on the CSV packet format | |
String incomingString = p.readString().trim(); | |
print("Received string over serial: "); | |
println(incomingString); | |
String[] incomingValues = split(incomingString, ','); | |
// Verify that the packet looks legit | |
if (incomingValues.length > 3) { | |
packetCount++; | |
// Wait till the third packet or so to start recording to avoid initialization garbage. | |
if (packetCount > 3) { | |
// Fish out the values (could get more EEG power channel values here if you wanted) | |
int signalStrength = Integer.parseInt(incomingValues[0]); | |
int attention = Integer.parseInt(incomingValues[1]); | |
int meditation = Integer.parseInt(incomingValues[2]); | |
// Send these values out over OSC to PD (Can PD handle this type of OSC object?) | |
oscProcessing.send("/brain", | |
new Object[] { | |
new Integer(signalStrength), | |
new Integer(attention), | |
new Integer(meditation) | |
}, | |
oscPD); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment