Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Processing example (display "realistic" sphere color) #42

Merged
merged 3 commits into from
Dec 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions examples/processing/display_sphere_color/display_sphere_color.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Give a realistic feedback of the sphere color
*/

import io.thp.psmove.*;

PSMove move;
color sphereColor;
int diam;

void setup() {
size(300,300);
colorMode(HSB); //

move = new PSMove(); // We need one controller
sphereColor = color(0); // Default sphere color (0 means ligths off)

diam = (int)(min(width,height)*.9); // Diameter of the circle
}



void draw() {
background(200);

// We define color values that will vary over time
int h = (int) map( sin( frameCount*.001 ), -1, 1, 0, 255 ); // Hue
int s = (int) map( sin( frameCount*.005 ), -1, 1, 200, 255 ); // Saturation
int b = (int) map( sin( frameCount*.01 ), -1, 1, 0, 255 ); // Brightness

sphereColor = color( h, s, b ); // Set the new color value

moveUpdate(sphereColor); // Send the new value to the controller

drawColorCircle(sphereColor); // Display the color in the sketch window
}



// rgb color (0,0,0) is black, but we want it to be white...
void drawColorCircle(color c) {

stroke( 0,0,255 ); // White outline, for style
strokeWeight(3);

int alpha = (int)brightness(c); // Transparency

pushMatrix(); // Temporary adjustment of the coordinates system
translate( width*.5, height*.5 ); // To the center

fill( 0,0,255 ); // Paint any shape that follows white
ellipse( 0,0,diam,diam ); // Draw a white background circle

fill( c, alpha ); // Set color & transparency
ellipse( 0,0,diam,diam ); // Draw the color circle

popMatrix(); // Forget the adjustment of the coord system

}



void moveUpdate(color _sphereColor) {

// Convert from HSB to RGB
int r = (int)red(_sphereColor);
int g = (int)green(_sphereColor);
int b = (int)blue(_sphereColor);

move.set_leds(r, g, b); // Send the info to the controller
move.update_leds(); // Display the new color

}



98 changes: 98 additions & 0 deletions examples/processing/shake_detect/shake_detect.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* A simple but effective "shake" motion detection
* Works best on left to right shaking motion
*/

import io.thp.psmove.*;

PSMove move;

color sphereColor;
int rumbleLevel;

boolean isShaken;
int shakeCount;

int threshold = 9; // The higher the number, the harder it is to shake
int falloff = 5; // Max nb of frames it takes for the shake state to wear off

void setup() {
size(200,200);

move = new PSMove(); // We need one controller
sphereColor = color(0); // Default sphere color (0 means ligths off)
}

void draw() {

if(isShaken) {
sphereColor = color(255,168,0);
rumbleLevel = 255;
}

else {
sphereColor = color(0,200,200);
rumbleLevel = 0;
}

moveUpdate( rumbleLevel, sphereColor );
}


void moveUpdate(int _rumbleLevel, color _sphereColor) {

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) {

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);

detectShake(ax, az, threshold, falloff); // check if the accelerometers send extreme values
}

move.set_rumble(_rumbleLevel);

int r = (int)red(_sphereColor);
int g = (int)green(_sphereColor);
int b = (int)blue(_sphereColor);
move.set_leds(r, g, b);
move.update_leds();
}


void detectShake(float [] _xAcc, float [] _zAcc, int _threshold, int _falloff) {

if(abs(_xAcc[0]) > 1.2 || abs(_zAcc[0]) > 1.2) {
shakeCount+=2;
}

if(shakeCount > _threshold) {
isShaken = true;
println("Stop shaking me!!");
if(shakeCount > _threshold + _falloff) shakeCount = _threshold + _falloff;
}

else {
isShaken = false;
}

if(shakeCount>0) shakeCount--;
}

void keyPressed() {
if (key == 27) { // escape key
quit();
}
}

void quit() {
move.set_rumble(0);
move.set_leds(0, 0, 0);
move.update_leds(); // we switch of the sphere and rumble before we quit
exit();
}