Skip to content

Commit

Permalink
Merge pull request #41 from SableRaf/master
Browse files Browse the repository at this point in the history
New Processing example
  • Loading branch information
thp committed Dec 19, 2012
2 parents 08fa585 + 7373834 commit 2f0eabc
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
130 changes: 130 additions & 0 deletions examples/processing/button_array/button_array.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();
}

42 changes: 42 additions & 0 deletions examples/processing/button_array/moveButton.pde
@@ -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;
}
};
52 changes: 52 additions & 0 deletions examples/processing/flip/flip.pde
@@ -0,0 +1,52 @@

// Import the PS Move API Package
import io.thp.psmove.*;

PSMove [] moves;

void setup() {
moves = new PSMove[psmoveapi.count_connected()];
for (int i=0; i<moves.length; i++) {
moves[i] = new PSMove(i);
}
}

void draw() {

for (int i=0; i<moves.length; i++) {
handle(i, moves[i]);
}

}

void handle(int i, PSMove move)
{
float [] ax = {0.f}, ay = {0.f}, az = {0.f};
float [] gx = {0.f}, gy = {0.f}, gz = {0.f};
float [] mx = {0.f}, my = {0.f}, mz = {0.f};

while (move.poll () != 0) { } // get button presses here

move.get_accelerometer_frame(io.thp.psmove.Frame.Frame_SecondHalf, ax, ay, az);
move.get_gyroscope_frame(io.thp.psmove.Frame.Frame_SecondHalf, gx, gy, gz);
move.get_magnetometer_vector(mx, my, mz);

int glow = (int)map(sin(frameCount*.02), -1, 1, 20, 200);

boolean flip = isFlipped(ay[0], my[0]);
if (!flip) { // Pointing up-> green
move.set_leds(0, glow, glow);
move.set_rumble(0);
}
else { // Pointing down -> flashing red + rumble
move.set_leds((int)random(255), 0, 0);
move.set_rumble(255);
}
move.update_leds();
}

boolean isFlipped(float ay, float my) {
if (ay<0 && my>0) return true;
return false;
}

0 comments on commit 2f0eabc

Please sign in to comment.