February 2012
M T W T F S S
« Jul «-»  
 12345
6789101112
13141516171819
20212223242526
272829  

Leaving Chris World?

Why not bring back a souvenir?

Archives

Screen Knock!

screenknock.zip

The fabled screen knock software is here.
Yes, it hit me one day that I could simply have
the motion sensor move the spaces. The interaction
was amusing and I'm hoping you'll think so too.

 
// Screen Knock Version 1
// October 1, 2008
// by Chris Stones
// http://www.welcometochrisworld.com
//
// One of the few programs I pretty much wrote from skratch.
// Special thanks to the SMS Lib though by Daniel Shiffman and others
// Note: Spaces on Mac OS 10.5 needs to be set to use
//       Command + Arrow Keys for this program to work.
//       * For best performance customize
//         KNOCK and wait_for_shake to your system.
//         as in ... you know, use trail and error values until you get it right.
 
// I had always acted out like I was knocking my laptop
// from one space to the next. But I finally sat down and made
// it do it by itself. 
 
import java.awt.Robot;
import java.awt.event.KeyEvent;
import sms.*;  // Sudden motion detection 
 
Robot robot;
int[] init_vals;
int[] vals;
int KNOCK;
int wait_for_shake;
boolean wait;
int wait_cnt;
 
int X,Y,Z; // calculated thresholds
 
void setup() {
  size(100,100);
  frameRate(20);
  wait = false;
  wait_cnt = 0;
  wait_for_shake = 15; // wait for shaking to stop (number/frames = seconds of waiting)
  KNOCK = 15;          // How big of a jolt before you want to move the screen
  try {
    robot = new Robot();
  }
  catch (Exception e) {
    println("robot Can not be created.");
    exit();
  }
 
  // Opening values don't start at zero.
  // So we need to collect values at 'rest'
  init_vals = Unimotion.getSMSArray(); 
 
  X = init_vals[0];
  Y = init_vals[1];
  Z = init_vals[2];
}
 
void draw() {
  // Read Sudden Motion Sensor
  int[] vals = Unimotion.getSMSArray();
 
  // Reset wait count to allow another jolt
  if( wait_cnt == wait_for_shake) {
    wait = false;
    wait_cnt = 0;
  }
 
  // While we are not waiting for the computer to
  // stop shaking try to make a decision about
  // the motion.
  if(!wait) {
 
    // There's always some shaking going on so we
    // need to check if the shaking was on purpose.
    if( vals[0] > KNOCK+X ) {
      screenLeft();
      wait = true;
    }
 
    if( vals[0] < -1*KNOCK+X ) {
      screenRight();
      wait = true;
    }
 
  }
  else {
    wait_cnt++;
  }
 
}
 
void screenRight() {
  try {
    robot.keyPress(KeyEvent.VK_META);   // hold meta
    robot.keyPress(KeyEvent.VK_RIGHT ); // then press arrow
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_RIGHT);
  }
  catch (Exception e) {
    println("Exception your honor!");
  }
}
 
void screenLeft() {
  try {
    robot.keyPress(KeyEvent.VK_META);   // hold meta
    robot.keyPress(KeyEvent.VK_LEFT ); // then press arrow
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_LEFT);
  }
  catch (Exception e) {
    println("Exception your honor!");
  }
}
 
void screenUp() {
  try {
    robot.keyPress(KeyEvent.VK_META);   // hold meta
    robot.keyPress(KeyEvent.VK_UP ); // then press arrow
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_UP);
  }
  catch (Exception e) {
    println("Exception your honor!");
  }
}
 
void screenDown() {
  try {
    robot.keyPress(KeyEvent.VK_META);   // hold meta
    robot.keyPress(KeyEvent.VK_DOWN );  // then press arrow
    robot.keyRelease(KeyEvent.VK_META);
    robot.keyRelease(KeyEvent.VK_DOWN);
  }
  catch (Exception e) {
    println("Exception your honor!");
  }
}
 

12 comments to Screen Knock!

You must be logged in to post a comment.