Skip to content

Instantly share code, notes, and snippets.

@drscotthawley
Last active October 4, 2022 18:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drscotthawley/dd74db91dd2f8ec3c810a87d4d26b576 to your computer and use it in GitHub Desktop.
Save drscotthawley/dd74db91dd2f8ec3c810a87d4d26b576 to your computer and use it in GitHub Desktop.
"Gamepad" Input example for Wekinator, e.g. for Xbox 360 controller
// Gamepad_Eyes_OSC: demo of using game controller for OSC input.
// Uses the two thumb-sticks and either the left or right bumper buttons.
//
// Peter Lager maintains the Game Control Plus library (for Processing),
// which provides control data for joysticks and other game controllers
// ...such as my Xbox 360 controller.
//
// This is a quick mash-up using Lager's Gcp_gamepad animated eyes example code,
// (which is in the GCP library: In Processing, go to File > Examples..., then Contributed Libraries > Game Control Plus)
// and Rebecca Fiebrink's Simple_Mouse_DraggedObject_2Inputs example code for Wekinator.
//
// Required Processing libraries: Game Control Plus, oscP5. Also needs Lager's file "data/gamepad_eyes" text file from his example.
//
// See Lager's website & videos at http://lagers.org.uk/gamecontrol/ for more info.
//
// Mashed by @drscotthawley
// Demo video: https://youtu.be/VZ73RlDlnNQ
// (Controller animation provided by 360 Controller for Mac driver, https://github.com/360Controller/360Controller/releases)
import org.gamecontrolplus.gui.*;
import org.gamecontrolplus.*;
import net.java.games.input.*;
import oscP5.*;
import netP5.*;
ControlIO control;
Configuration config;
ControlDevice gpad;
float pupilPosX, pupilPosY, pupilSize;
float eyeRad = 80, eyeSize = eyeRad * 2;
float browSize = eyeSize * 1.2f, browFactor;
float irisRad = 42, irisSize = irisRad * 2;
float lidPos, restLid = PI * 0.3f, minLid = restLid/1.5f, maxLid = PI * 0.92f;
OscP5 oscP5;
NetAddress dest;
public void setup() {
size(400, 240);
// Initialise the ControlIO
control = ControlIO.getInstance(this);
// Find a device that matches the configuration file
gpad = control.getMatchedDevice("gamepad_eyes"); // the file gamepad_eyes should exist in a data/ directory
if (gpad == null) {
println("No suitable device configured");
System.exit(-1); // End the program NOW!
}
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this,9000);
dest = new NetAddress("127.0.0.1",6448);
}
void draw() {
background(255, 200, 255);
// Either button will dilate pupils
boolean dilated = gpad.getButton("PUPILSIZE1").pressed() || gpad.getButton("PUPILSIZE2").pressed();
pupilSize = dilated ? irisSize * 0.6f : irisSize * 0.45f;
pupilPosX = 0.9f * map(gpad.getSlider("XPOS").getValue(), -1, 1, -(eyeRad - irisRad), eyeRad - irisRad);
pupilPosY = 0.9f * map(gpad.getSlider("YPOS").getValue(), -1, 1, -(eyeRad - irisRad), eyeRad - irisRad);
// Eyebrow first
lidPos = gpad.getSlider("EYELID").getValue();
browFactor = (lidPos >= 0) ? 1 : map(lidPos, 0, -1, 1.1f, 1.3f);
// Now the actual lids
lidPos = map(lidPos, -0.12f, 1, restLid, maxLid);
lidPos = constrain(lidPos, minLid, maxLid);
// DRaw a pir of eyes
drawEye(100, 140);
drawEye(300, 140);
//Send the OSC message with current position
sendOsc();
}
public void drawEye(int x, int y) {
pushMatrix();
translate(x, y);
// draw white of eye
stroke(0, 96, 0);
strokeWeight(3);
fill(255);
ellipse(0, 0, eyeSize, eyeSize);
// draw iris
noStroke();
fill(120, 100, 220);
ellipse(pupilPosX, pupilPosY, irisSize, irisSize);
// draw pupil
fill(32);
ellipse(pupilPosX, pupilPosY, pupilSize, pupilSize);
// draw eye lid
stroke(0, 96, 0);
strokeWeight(4);
fill(220, 160, 220);
arc(0, 0, eyeSize, eyeSize, 1.5f*PI-lidPos, 1.5f*PI+lidPos, CHORD);
// Draw eyebrow
stroke(100, 100, 10);
strokeWeight(10);
noFill();
arc(0, 0, browSize, browSize * browFactor, 1.2f*PI, 1.8f*PI);
popMatrix();
}
void sendOsc() {
OscMessage msg = new OscMessage("/wek/inputs");
msg.add((float)pupilPosX);
msg.add((float)pupilPosY);
msg.add((float)lidPos);
msg.add((float)pupilSize);
oscP5.send(msg, dest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment