Created
January 11, 2013 03:41
-
-
Save hilukasz/4507774 to your computer and use it in GitHub Desktop.
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
/* | |
code by Łukasz Wieczorek : http://hellowoo.com | |
feel free to modify code, just give me some credz | |
and keep this message in tact | |
kloveyou. | |
*/ | |
// TODO: | |
// Add "flick" physics | |
// Break up into classes | |
// make the scroll smoother | |
//IR sensor | |
import java.awt.AWTException; | |
import java.awt.Robot; | |
import processing.serial.*; | |
import java.awt.MouseInfo; | |
import java.awt.event.InputEvent; | |
import java.awt.PointerInfo.*; | |
//controls | |
import controlP5.*; | |
ControlP5 cp5; | |
boolean autoClick = false; | |
// scrolls | |
Robot myMouseWheel; | |
Robot mouseWheel2; | |
Serial myPort; // Create object from Serial class | |
float posX; // data from msg fields will be stored here | |
int windowHeight = 500, | |
windowWidth = 500, | |
smallestVal; | |
void setup() { | |
//.... | |
//slider controls | |
////////////////// | |
cp5 = new ControlP5(this); | |
// noise | |
cp5.addToggle("autoClick") | |
.setPosition(10,10) | |
.setSize(50,20) | |
; | |
// scrolls | |
myPort = new Serial(this, Serial.list()[4], 9600); | |
myPort.bufferUntil('\n'); | |
size(windowWidth, windowHeight); | |
try { | |
myMouseWheel = new Robot(); | |
mouseWheel2 = new Robot(); | |
} | |
catch (AWTException e){ | |
println("Robot class not supported by your system!"); | |
exit(); | |
} | |
fill(255); | |
} | |
int checkScroll; | |
int scrollMe; | |
int active, top, bottom; | |
int myMouseX, myMouseY; | |
int lastMouseX, lastMouseY; | |
void draw(){ | |
//get global mouse position | |
myMouseX = MouseInfo.getPointerInfo().getLocation().x; | |
myMouseY = MouseInfo.getPointerInfo().getLocation().y; | |
// only scroll when val changes | |
if(checkScroll != scrollMe){ | |
myMouseWheel.mouseWheel(scrollMe); | |
// check to see user has moved the mouse, if it has then click (focus window hack) could use | |
// some improvement here | |
if( lastMouseX != myMouseX && lastMouseY != myMouseY && autoClick == true){ | |
print("CLICK!!!"); | |
try { | |
Robot clickRobot = new Robot(); | |
// Simulate a mouse click | |
clickRobot.mousePress(InputEvent.BUTTON1_MASK); | |
clickRobot.mouseRelease(InputEvent.BUTTON1_MASK); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
lastMouseX = myMouseX; | |
lastMouseY = myMouseX; | |
} | |
if(scrollMe == 0){ | |
top = 0; | |
bottom = 0; | |
} | |
if(scrollMe > 0){ | |
top = 0; | |
bottom = 255; | |
} | |
if (scrollMe < 0){ | |
top = 255; | |
bottom = 0; | |
} | |
} | |
checkScroll = scrollMe; | |
fill(top); rect(0, 0, windowWidth, windowHeight/2); | |
fill(bottom); rect(0, windowHeight/2, windowWidth, windowHeight/2); | |
} | |
float lastVal = 1000; | |
int currentVal; | |
void serialEvent(Serial p) { | |
String message = myPort.readStringUntil('\n'); // read serial data | |
if(message != null) { | |
String[] data = splitTokens(message,","); // Split the comma-separated message | |
///////////// | |
// callibrate | |
//........... | |
//int myMessage = int(message); | |
Calibrate(message); | |
if (data[0].equals("handSensor") && data.length > 1){ | |
posX = float(data[1]); | |
//println(posX); | |
try { | |
if(posX < 100) { | |
scrollMe = 0; | |
return; // dont check other ifs after this | |
} | |
if(posX > smallestVal){ // smallestVal comes from calibrate function | |
if(posX > lastVal){ | |
scrollMe += 1; | |
} | |
if(posX < lastVal){ | |
scrollMe -= 1; | |
} | |
} | |
lastVal = posX; | |
} | |
catch (Throwable t) { | |
print("parse error"); | |
} | |
} | |
} | |
} | |
int calibrateCount = 1; | |
void Calibrate(String theMessage) { | |
if(calibrateCount < 30){ | |
String message = theMessage; | |
if(message != null) { | |
String[] data = splitTokens(message,","); | |
int currentIRVal = int(data[1]); | |
if(currentIRVal < smallestVal){ | |
smallestVal = currentIRVal; | |
smallestVal = int(smallestVal); | |
} | |
calibrateCount++; | |
} | |
if(calibrateCount == 30){ | |
println("done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment