Last active
December 28, 2015 14:59
-
-
Save sonalisharma/7518916 to your computer and use it in GitHub Desktop.
Processing code for Match glass. Reads input from one cube only.
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
/* | |
* Match Glass | |
* | |
* Created 16 November 2013 | |
* Sonali Sharma | |
* Vaidy | |
*/ | |
import processing.serial.*; | |
//Initialising variables | |
String portname = "/dev/tty.usbmodem1411"; | |
String[] temp; | |
Serial port; | |
String buf=""; | |
int cr = 13; // ASCII return == 13 | |
int lf = 10; // ASCII linefeed == 10 | |
PFont f; | |
int quesloc = -2; | |
String newques=""; | |
int maxpin = 0; | |
String[] questions = { | |
"##### hours of fun, easy to use, perfect for _____", | |
"With enough time and pressure, ##### will turn into _____". | |
"!! ##### hours of fun, easy to use, perfect for _____!!!", | |
"Hours of fun, ##### easy to use, perfect _____ for." | |
}; | |
String[] answers1 = { | |
"An ether-soaked rag", | |
"Some God-damn peace and quite", | |
"Big head", | |
"Gigantic" | |
}; | |
int[] pins = {0,1,2,3}; | |
//Basic Setup | |
void setup() { | |
size(800,800); | |
frameRate(10); | |
noStroke(); | |
background(40,40,40); | |
port = new Serial(this, portname, 9600); | |
} | |
void draw() { | |
//text(newques,width/3,40); | |
} | |
void clearScreen(int val) { | |
if (val!=8 || val!=10 || val!=11 || val!=13) { | |
background(40,40,40); // erase screen | |
} | |
} | |
//Display the text. Font size and the headline text controlled by the values passed from serial port. | |
int textdisplay(int val) | |
{ | |
if (quesloc ==-2) | |
{ | |
quesloc = int(random(4)); | |
} | |
newques = questions[quesloc].replace("#####","_____"); | |
if(val >= 0 && val<=3) | |
{ | |
//int size = (int)(((20.0/225.0)*(float)val)+20.0); | |
//println("SIZE:"+size); | |
int size = 30; | |
f = createFont("Arial",size,true); | |
textFont(f); | |
fill(255); | |
textAlign(CENTER); | |
//Queston display | |
String newques = ""; | |
fill(251); | |
textAlign(CENTER); | |
clearScreen(val); | |
//Calculating index to display the text | |
int index = 0; | |
for (int i =0; i<pins.length; i++) | |
{ | |
if (val == pins[i]) | |
{ | |
index = i; | |
break; | |
} | |
} | |
//background(40,40,40); | |
println("index:"+index); | |
//text(newques,width/3,40); | |
temp = questions[quesloc].split("\\s+"); | |
println(temp); | |
int initloc = 50; | |
//questions[quesloc].replace("_____",answers[index]); | |
for (int z=0; z<temp.length;z++) | |
{ | |
initloc = initloc+50; | |
if (temp[z].equals("#####")) | |
{ | |
println("initloc="+initloc); | |
fill(0,255,0); | |
stroke(5); | |
text(answers1[index],width/3,initloc); | |
//initloc = initloc+10+answers[index].length()*5; | |
} | |
else | |
{ | |
println("initloc="+initloc); | |
fill(255,0,0); | |
text(temp[z],width/3,initloc); | |
//initloc = initloc+10+temp[z].length()*5; | |
} | |
} | |
//text(questions[quesloc].replace("_____","<"+answers[index]+">"),width/3,70,100,100); | |
// String s = newques; | |
//fill(255,0,0); | |
//text(s, width/3, 40, 250, 250); // Text wraps within text box | |
//text(questions[quesloc].replace("_____","<"+answers[index]+">"), 10, 10, 70, 80); | |
//text(questions[quesloc].replace("_____",answers[index]),10, 10, 70, 80); | |
//background(40,40,40); | |
return 1; | |
} | |
else | |
{ | |
temp = questions[quesloc].split("\\s+"); | |
for (int z=0; z<temp.length;z++) | |
{ | |
initloc = initloc+50; | |
if (temp[z].equals("#####")) | |
{ | |
println("initloc="+initloc); | |
fill(0,255,0); | |
stroke(5); | |
text(answers1[index],width/3,initloc); | |
//initloc = initloc+10+answers[index].length()*5; | |
} | |
else | |
{ | |
println("initloc="+initloc); | |
fill(255,0,0); | |
text(temp[z],width/3,initloc); | |
//initloc = initloc+10+temp[z].length()*5; | |
} | |
} | |
return 0; | |
} | |
//Calculating font size | |
} | |
// called whenever serial data arrives | |
void serialEvent(Serial p) { | |
int c = port.read(); | |
println("valcccc="+buf); | |
if (c != lf && c != cr) { | |
buf += char(c); | |
} | |
if (c == lf) { | |
int val = int(buf); | |
println("val="+val); | |
textdisplay(val); | |
buf = ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment