Last active
January 31, 2021 19:30
-
-
Save kadamwhite/45a9e50de118a029e727b45683de15d2 to your computer and use it in GitHub Desktop.
Processing Sketch controlled by USB DDR pad
This file contains hidden or 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
// PREREQUISITES! | |
// | |
// 1. Plug in the controller before running sketch | |
// 2. Follow the steps below once to download the Game Control Plus library | |
// | |
// In the "Sketch" menu in Processing's menubar, go to | |
// Sketch > Import Library > Add Library | |
// Then search for "Game Control". You should find a library called "Game Control Plus"; | |
// install it and let Processing download all the files. | |
// | |
// Game Control Plus is a library used to interact with USB gamepads, and it is now | |
// installed on your copy of Processing! | |
// If we were starting from scratch, you would now go to the Sketch menu again to add that | |
// library as a dependency to your sketch file, by selecting | |
// Sketch > Import Library > Game Control Plus | |
// at the bottom of the "Import Library" sub-menu. However, all that "import library" does | |
// is print out these next three lines; so in this case, once it's installed that should be | |
// enough to try running this sketch! | |
import net.java.games.input.*; | |
import org.gamecontrolplus.*; | |
import org.gamecontrolplus.gui.*; | |
class DDRPad { | |
ControlIO control; | |
ControlDevice device; | |
HashMap<String, ControlButton> buttons; | |
DDRPad( PApplet context ) { | |
buttons = new HashMap<String, ControlButton>(); | |
control = ControlIO.getInstance( context ); | |
println( "About to try to assign a controller!" ); | |
println( "If this fails, try reading the output below and changing the device index" ); | |
println( control.deviceListToText( " " ) ); | |
// What the println means is, we assume the DDR Pad is "device 0". If it is | |
// not registered at that index (possibly your computer has detected another | |
// gamepad, etc), our assumption about the button IDs will likely fail. In | |
// that case, you'll need to change the 0 in the line below to match the right | |
// device ID. | |
// On my system, when I start this sketch with the DDR pad plugged in, I see | |
// | |
// Linux plugin claims to have found 1 controllers | |
// ######################################################### | |
// Game Control Plus - available devices | |
// -------------------------------------- | |
// 0 USB Gamepad [Stick] on [USB port] | |
// ######################################################### | |
// | |
// The ID 0 on that line reading "USB Gamepad" is why we pass 0 in the line | |
// below. If you see different output and the program does NOT work, try other | |
// IDs from the list in the output that looks like the sample above. | |
device = control.getDevice( 0 ); | |
// println( device.buttonsToText( " " ) ); | |
buttons.put( "up", device.getButton( 0 ) ); | |
buttons.put( "left", device.getButton( 2 ) ); | |
buttons.put( "down", device.getButton( 1 ) ); | |
buttons.put( "right", device.getButton( 3 ) ); | |
buttons.put( "circle", device.getButton( 7 ) ); | |
buttons.put( "cross", device.getButton( 6 ) ); | |
} | |
boolean pressed( String button ) { | |
return buttons.get( button ).pressed(); | |
} | |
boolean up() { return this.pressed( "up" ); } | |
boolean down() { return this.pressed( "down" ); } | |
boolean left() { return this.pressed( "left" ); } | |
boolean right() { return this.pressed( "right" ); } | |
boolean circle() { return this.pressed( "circle" ); } | |
boolean cross() { return this.pressed( "cross" ); } | |
void onPress( String button, String methodName ) { | |
this.buttons.get( button ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressLeft( String methodName ) { | |
this.buttons.get( "left" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressRight( String methodName ) { | |
this.buttons.get( "right" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressUp( String methodName ) { | |
this.buttons.get( "up" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressDown( String methodName ) { | |
this.buttons.get( "down" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressCircle( String methodName ) { | |
this.buttons.get( "circle" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
void onPressCross( String methodName ) { | |
this.buttons.get( "cross" ).plug( methodName, ControlIO.ON_PRESS ); | |
} | |
} | |
// OK, all the setup is done; let's write a sketch using a DDR Pad! | |
float x, y; | |
boolean isRed; | |
DDRPad pad; | |
String direction = "up"; | |
// Pluggable functions: write what should happen on each button press, | |
// and then use `pad.onPress( 'button', 'nameOfMethod' ) in the setup | |
// function to bind those responses. | |
void setDirectionUp() { | |
direction = "up"; | |
} | |
void setDirectionRight() { | |
direction = "right"; | |
} | |
void setDirectionLeft() { | |
direction = "left"; | |
} | |
void setDirectionDown() { | |
direction = "down"; | |
} | |
void toggleColor() { | |
isRed = ! isRed; | |
} | |
// Set things up | |
void setup() { | |
pad = new DDRPad( this ); | |
isRed = true; | |
x = 500; | |
y = 500; | |
size( 1000, 1000 ); | |
pad.onPress( "up", "setDirectionUp" ); | |
pad.onPress( "right", "setDirectionRight" ); | |
pad.onPress( "left", "setDirectionLeft" ); | |
pad.onPress( "down", "setDirectionDown" ); | |
pad.onPress( "cross", "toggleColor" ); | |
} | |
void draw() { | |
background( 255 ); | |
stroke( 0 ); | |
if ( isRed ) { | |
fill( 255, 0, 0 ); | |
} else { | |
fill( 0, 255, 0 ); | |
} | |
rect( x - 100, y - 100, 200, 200 ); | |
// Allow the buttons to move the box | |
if ( direction == "right" ) { x += 10; } | |
if ( direction == "left" ) { x -= 10; } | |
if ( direction == "up" ) { y -= 10; } | |
if ( direction == "down" ) { y += 10; } | |
// Ensure box can't go off-screen by constraining min & max values | |
if ( x < 0 ) { x = 0; } | |
if ( x > 1000 ) { x = 1000; }; | |
if ( y < 0 ) { y = 0; } | |
if ( y > 1000 ) { y = 1000; }; | |
//// Allow the circle & cross buttons to change color | |
//if ( pad.circle() ) { isRed = true; } | |
//if ( pad.cross() ) { isRed = false; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment