Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created February 19, 2016 19:14
Show Gist options
  • Save gregtemp/1124b70e6957fc911ae9 to your computer and use it in GitHub Desktop.
Save gregtemp/1124b70e6957fc911ae9 to your computer and use it in GitHub Desktop.
/// press y,p, or r to send test midi for that controller#
/// 1 turns sendMidi off, 2 sends it on
import processing.serial.*;
import themidibus.*;
Serial port;
MidiBus myBus; // The MidiBus
int aligned = 0;
int interval = 0;
float[] ypr = new float[3];
int[] wasd = new int[4];
String mem;
int sendMidi;
void setup() {
size(280, 40, OPENGL);
MidiBus.list();
myBus = new MidiBus(this, -1, "IAC Bus 2");
// display serial port list for debugging/clarity
println(Serial.list());
port = new Serial(this, Serial.list()[2], 115200);
// send single character to trigger DMP init/start
// (expected by MPU6050_DMP6 example Arduino sketch)
port.write('r');
}
void draw() {
if (millis() - interval > 1000) {
// resend single character to trigger DMP init/start
// in case the MPU is halted/reset while applet is running
port.write('r');
interval = millis();
}
background(0);
int holdwasd0 = wasd[0];
int holdwasd1 = wasd[1];
int holdwasd2 = wasd[2];
int holdwasd3 = wasd[3];
// 0 = w . 1 = a, 2 = s. 3 = d
if (ypr[0] >= 0) {
wasd[0] = (int)(((ypr[0])/84.0)*127.0);
wasd[1] = 0;
}
else if (ypr[0] < 0) {
wasd[1] = (int)(((ypr[0])/84.0)*-127.0);
wasd[0] = 0;
}
if (ypr[2] >= 0) {
wasd[2] = (int)(((ypr[2])/84.0)*127.0);
wasd[3] = 0;
}
else if (ypr[2] < 0) {
wasd[3] = (int)(((ypr[2])/84.0)*-127.0);
wasd[2] = 0;
}
if (sendMidi == 1) {
////myBus.sendControllerChange(channel, number, value);
if (holdwasd0 != wasd[0]) {
myBus.sendControllerChange(2, 100, wasd[0]);
}
if (holdwasd1 != wasd[1]) {
myBus.sendControllerChange(2, 101, wasd[1]);
}
if (holdwasd2 != wasd[2]){
myBus.sendControllerChange(2, 102, wasd[2]);
}
if (holdwasd3 != wasd[3]) {
myBus.sendControllerChange(2, 103, wasd[3]);
}
}
text(wasd[0], 00, height/2);
text(wasd[1], 70, height/2);
text(wasd[2], 140, height/2);
text(wasd[3], 210, height/2);
}
void serialEvent(Serial port) {
interval = millis();
while (port.available() > 0) {
int ch = port.read();
//print((char)ch);
/// probably not the best way to do this but whatever
if ((char)ch=='y') {
ypr[0] = float(mem);
mem = "";
}
else if ((char)ch=='p') {
ypr[1] = float(mem);
mem = "";
}
else if ((char)ch=='r') {
ypr[2] = float(mem);
mem = "";
}
else {
mem += (char)ch;
}
}
}
void keyPressed() {
if (key == '1') {
sendMidi = 0; }
if (key == '2') {
sendMidi = 1; }
/// press y,p, or r to send test midi for that controller#
if (key == 'w') {
myBus.sendControllerChange(2, 100, 127); }
if (key == 'a') {
myBus.sendControllerChange(2, 101, 127); }
if (key == 's') {
myBus.sendControllerChange(2, 102, 127); }
if (key == 'd') {
myBus.sendControllerChange(2, 103, 127); }
//myBus.sendControllerChange(2, 100, (int)ypr[0]);
// myBus.sendControllerChange(2, 101, (int)ypr[1]);
// myBus.sendControllerChange(2, 102, (int)ypr[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment