Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from SableRaf/master
New Processing example
- Loading branch information
Showing
3 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|