Created
July 4, 2016 15:52
-
-
Save erichoco/8b1077845c2de5b31796cf1b7fa6d535 to your computer and use it in GitHub Desktop.
GaussSense for Processing Widget #3: creates WebSocket client for GaussSense 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
import websockets.*; | |
import processing.serial.*; | |
import gausstoys.core.*; | |
//Thresholds for sensing (Unit: Gauss) | |
final int W = 400; | |
final int H = 400; | |
final float LO_PRESENCE_THLD = 3; | |
final float HI_PRESENCE_THLD = 10; | |
final float TILT_THLD = 75; | |
//constant for visualization | |
final float PIE_R = H * 0.75; | |
final float SLIDER_H = H/10.; | |
final float plateR = sqrt(PIE_R*PIE_R+SLIDER_H*SLIDER_H); | |
//GaussSense | |
GaussSense gs; | |
//Websocket | |
WebsocketClient wsc; | |
String wsData; | |
void setup() { | |
size(400, 400); | |
//size(400,400) to disable debugger's view | |
frameRate(25); //fail-safe for most micro-controllers. | |
//frameRate(100); //is OK for Arduino Leonardo | |
//frameRate(400); //is OK for teensy2.0 | |
//setup the canvas | |
ellipseMode(CENTER); | |
rectMode(CENTER); | |
noStroke(); | |
wsc = new WebsocketClient(this, "ws://localhost:5100"); | |
// List all available serial ports | |
//GaussSense.printSerialPortList(); | |
// Initialize the GaussSense | |
//gs = new GaussSense(this, GaussSense.GSType.GAUSSSENSE_MINI, 1, 1, Serial.list()[Serial.list().length - 1], 115200); | |
// Initialize the 2x2 GaussSense Grid | |
//gs = new GaussSense(this, GaussSense.GSType.GAUSSSENSE_MINI, 2, 2, Serial.list()[Serial.list().length - 1], 115200); | |
} | |
void draw() { | |
background(255); | |
pushStyle(); | |
//gs.setUpsampledData2D(W, H, 5); //Get the upsampled magnetic-field image (Upsample rate = 5) | |
//GData pNorth = gs.getNorthPoint(HI_PRESENCE_THLD); //Get the basic North point | |
//GData pSouth = gs.getSouthPoint(HI_PRESENCE_THLD); //Get the basic South point | |
//GData pBipolarMidpoint = gs.getBipolarMidpoint(LO_PRESENCE_THLD); //Get the basic Bi-polar Midpoint | |
GData pNorth = getNorthPoint(); | |
GData pSouth = getSouthPoint(); | |
GData pBipolarMidpoint = getBipolarMidpoint(); | |
float v1 = 0, v2 = 0, v3 = 0; | |
//"3. Sensing Tilt" | |
v1 = pBipolarMidpoint.getPitch(); | |
v2 = pBipolarMidpoint.getAngle()+3*PI/2; | |
if (v1>TILT_THLD) v1 = TILT_THLD; | |
if (v1<-TILT_THLD) v1 = -TILT_THLD; | |
//==[Alternative ways to use the parameters]== | |
//float tiltAngle = v1; //range = [-TILT_THLD, TILT_THLD]; | |
//float tiltDirection = v2; //range = [0, TWO_PI]; | |
//============================================ | |
//Draw background | |
fill((v1<0? lerpColor(color(240, 65, 100), color(0, 165, 210), 0.5-(v1 / (TILT_THLD*2))) : lerpColor(color(0, 165, 210), color(240, 65, 100), 0.5+(v1 / (TILT_THLD*2))))); | |
rect(W/2., H/2., H, H); | |
//Draw foreground | |
pushMatrix(); | |
translate(W/2., H/2.); | |
rotate(radians(v1)); | |
fill(255); | |
ellipse(0, 0, plateR+4, plateR+4); | |
fill(52); | |
ellipse(0, 0, plateR, plateR); | |
fill(255); | |
arc(0, 0, PIE_R, PIE_R, 0, PI, PIE); | |
fill(52); | |
ellipse(0, 0, PIE_R-SLIDER_H*2, PIE_R-SLIDER_H*2); | |
popMatrix(); | |
/* | |
// Show Debugger's View if window's width > height | |
if (width*gs.getSensorWidth()>height*gs.getSensorHeight()) { | |
pushMatrix(); | |
translate(400, 0); | |
gs.drawUpsampledData2D(W, H, 5); | |
stroke(0); | |
strokeWeight(5); | |
gs.drawBasicNorthPoint(HI_PRESENCE_THLD, W/10.); | |
gs.drawBasicSouthPoint(HI_PRESENCE_THLD, W/10.); | |
gs.drawBasicBipolarMidpoint(LO_PRESENCE_THLD, W/4.); | |
noFill(); | |
strokeWeight(2); | |
rectMode(CENTER); | |
rect(W/2., H/2., H-1, H-1); | |
popMatrix(); | |
} | |
*/ | |
popStyle(); | |
} | |
void keyReleased() { | |
if (key == ENTER) {//Press Enter for re-calibration | |
if (gs != null) gs.redoCalibration(); | |
} | |
if (key == TAB) {//Press Tab for mode-switching | |
//no-op | |
} | |
} | |
void webSocketEvent(String msg) { | |
println(msg); | |
wsData = msg; | |
} | |
GData getNorthPoint() { | |
if (wsData == null) { | |
return new GData(); | |
} | |
JSONObject north = JSONObject.parse(wsData).getJSONObject("northPoint"); | |
//JSONObject north = obj.getJSONObject("northPoint"); | |
return new GData(north.getFloat("x")*W, north.getFloat("y")*H, north.getFloat("intensity"), north.getFloat("angle"), north.getFloat("pitch"), north.getJSONArray("areaArray").getIntArray()); | |
} | |
GData getSouthPoint() { | |
if (wsData == null) { | |
return new GData(); | |
} | |
JSONObject south = JSONObject.parse(wsData).getJSONObject("southPoint"); | |
return new GData(south.getFloat("x")*W, south.getFloat("y")*H, south.getFloat("intensity"), south.getFloat("angle"), south.getFloat("pitch"), south.getJSONArray("areaArray").getIntArray()); | |
} | |
GData getBipolarMidpoint() { | |
if (wsData == null) { | |
return new GData(); | |
} | |
JSONObject mid = JSONObject.parse(wsData).getJSONObject("bipolarMidpoint"); | |
return new GData(mid.getFloat("x")*W, mid.getFloat("y")*H, mid.getFloat("intensity"), mid.getFloat("angle"), mid.getFloat("pitch"), mid.getJSONArray("areaArray").getIntArray()); | |
} | |
int[] getTagID() { | |
if (wsData == null) { | |
int[] tagID = {-128, -128, -128, -128, -128}; | |
return tagID; | |
} | |
return JSONObject.parse(wsData).getJSONArray("tagID").getIntArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment