Permalink
Browse files

New Processing example (button_array)

Using a moveButton class to create an array of buttons and variables to
abstract the API calls.
  • Loading branch information...
1 parent d4a35bc commit 7373834bbb82e478b5e7bcdbd1cb9431b25762d0 @SableRaf SableRaf committed Dec 19, 2012
Showing with 172 additions and 0 deletions.
  1. +130 −0 examples/processing/button_array/button_array.pde
  2. +42 −0 examples/processing/button_array/moveButton.pde
@@ -0,0 +1,130 @@
+/*
+Create a button array to store the values and press status of each button/trigger
+ */
+
+import io.thp.psmove.*;
+
+PSMove move;
+
+int triggerValue;
+boolean isTriggerPressed, isMovePressed, isSquarePressed, isTrianglePressed, isCrossPressed, isCirclePressed, isStartPressed, isSelectPressed, isPsPressed;
+
+int rumbleLevel;
+
+color sphereColor;
+int r, g, b;
+
+moveButton[] moveButtons = new moveButton[9]; // The move controller has 9 buttons
+
+void setup() {
+ move = new PSMove(); // We need one controller
+ sphereColor = color(0); // Default sphere color (0 means ligths off)
+
+ moveInit(); // Create the buttons
+}
+
+void draw() {
+ sphereColor = color(0, 255-triggerValue, triggerValue);
+
+ if(isMovePressed) {
+ rumbleLevel = 255;
+ sphereColor = color(random(0,255), 0, 0);
+ }
+ else {
+ rumbleLevel = 0;
+ }
+
+ moveUpdate(rumbleLevel, sphereColor); // Get the buttons value (trigger only) and presses, and update actuators/indicators
+}
+
+
+//-------------------------------------------------------------
+
+void moveInit() {
+ for (int i=0; i<moveButtons.length; i++) {
+ moveButtons[i] = new moveButton();
+ }
+}
+
+void moveUpdate(int _rumbleLevel, color _sphereColor) {
+ // Read buttons
+ while (move.poll () != 0) {
+
+ int trigger = move.get_trigger();
+ move.set_leds(0, 255-trigger, trigger);
+ moveButtons[0].setValue(trigger);
+
+ int buttons = move.get_buttons();
+ if ((buttons & Button.Btn_MOVE.swigValue()) != 0) {
+ moveButtons[1].press();
+ sphereColor = color((int)(random(255)), 0, 0);
+ }
+ else {
+ moveButtons[1].release();
+ move.set_rumble(0);
+ }
+ if ((buttons & Button.Btn_SQUARE.swigValue()) != 0) {
+ moveButtons[2].press();
+ }
+ else {
+ moveButtons[2].release();
+ }
+ if ((buttons & Button.Btn_TRIANGLE.swigValue()) != 0) {
+ moveButtons[3].press();
+ }
+ else {
+ moveButtons[3].release();
+ }
+ if ((buttons & Button.Btn_CROSS.swigValue()) != 0) {
+ moveButtons[4].press();
+ }
+ else {
+ moveButtons[4].release();
+ }
+ if ((buttons & Button.Btn_CIRCLE.swigValue()) != 0) {
+ moveButtons[5].press();
+ }
+ else {
+ moveButtons[5].release();
+ }
+ if ((buttons & Button.Btn_SELECT.swigValue()) != 0) {
+ moveButtons[6].press();
+ }
+ else {
+ moveButtons[6].release();
+ }
+ if ((buttons & Button.Btn_START.swigValue()) != 0) {
+ moveButtons[7].press();
+ }
+ else {
+ moveButtons[7].release();
+ }
+ if ((buttons & Button.Btn_PS.swigValue()) != 0) {
+ moveButtons[8].press();
+ }
+ else {
+ moveButtons[8].release();
+ }
+ }
+
+ // Store the values in conveniently named variables
+ triggerValue = moveButtons[0].value;
+ isTriggerPressed = moveButtons[0].getPressed(); // The trigger is considered pressed if value > 0
+ isMovePressed = moveButtons[1].getPressed();
+ isSquarePressed = moveButtons[2].getPressed();
+ isTrianglePressed = moveButtons[3].getPressed();
+ isCrossPressed = moveButtons[4].getPressed();
+ isCirclePressed = moveButtons[5].getPressed();
+ isSelectPressed = moveButtons[6].getPressed();
+ isStartPressed = moveButtons[7].getPressed();
+ isPsPressed = moveButtons[8].getPressed();
+
+ move.set_rumble(_rumbleLevel);
+
+ r = (int)red(_sphereColor);
+ g = (int)green(_sphereColor);
+ b = (int)blue(_sphereColor);
+ move.set_leds(r, g, b);
+ move.update_leds();
+}
+
@@ -0,0 +1,42 @@
+class moveButton {
+
+ int isPressed;
+ int value; // For analog buttons only (triggers)
+ PVector analog; // For analog sticks (navigation controller only)
+
+ moveButton() {
+ isPressed = 0;
+ value = 0;
+ analog = new PVector(0,0);
+ }
+
+ void press() {
+ isPressed = 1;
+ }
+
+ void release() {
+ isPressed = 0;
+ }
+
+ boolean getPressed() {
+ if(isPressed == 1) return true;
+ return false;
+ }
+
+ void setValue(int _val) {
+ value = _val;
+ }
+
+ int getValue() {
+ return value;
+ }
+
+ void setAnalog(float _x, float _y) {
+ analog.x = _x;
+ analog.y = _y;
+ }
+
+ PVector getAnalog() {
+ return analog;
+ }
+};

0 comments on commit 7373834

Please sign in to comment.