Last active
December 25, 2015 20:09
-
-
Save ohmygodwin/7032483 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
import processing.serial.*; | |
import processing.pdf.*; | |
Serial myPort; | |
//handshake method for serial communication | |
boolean firstContact = false; | |
//shape arrays | |
SquareSpin[] square = new SquareSpin[5000]; | |
CircleSpin[] circle = new CircleSpin[5000]; | |
//serial for PDF | |
int record = 0; | |
//serial for shape size, scale, color | |
int xpos, ypos, dist, color1, color2, color3; | |
//mapping scale | |
//float d = map(dist, 0, 884, 60, 80); | |
int scale; | |
//= round(d); | |
//int scale = 80; | |
int squareCount = 0; | |
int circleCount = 0; | |
void setup() { | |
size(662, 662); | |
for (int a = 0; a <= width + scale; a = a + scale) { | |
for (int b = 0; b <= height + scale; b = b + scale) { | |
square[squareCount] = new SquareSpin(a-320, b-320); | |
squareCount++; | |
circle[circleCount] = new CircleSpin(a-320, b-320); | |
circleCount++; | |
float d = map(dist, 0, 884, 30, 80); | |
scale = round(d); | |
} | |
} | |
println(scale); | |
String portName = Serial.list()[0]; | |
myPort = new Serial(this, portName, 9600); | |
myPort.bufferUntil('\n'); | |
} | |
void draw() { | |
if (record == 255) { | |
// Note that #### will be replaced with the frame number. Fancy! | |
beginRecord(PDF, "frame-####.pdf"); | |
delay(1000); | |
} | |
float c1 = map(color1, 0, 900, 0, 255); | |
float c2 = map(color2, 0, 900, 0, 255); | |
float c3 = map(color3, 0, 900, 0, 255); | |
background(54, c2, 104); | |
pushMatrix(); | |
translate(width/128, height/128); | |
float sqSize = map(xpos, 0, 1023, 7, 100); | |
float cirSize = map(ypos, 0, 1023, 7, 100); | |
int sq = 0; | |
int cir = 0; | |
while (sq < squareCount) { | |
square[sq].go(); | |
square[sq].show(c1, 150, 150); | |
square[sq].updateSize(sqSize); | |
sq++; | |
} | |
while (cir < circleCount) { | |
circle[cir].go(); | |
circle[cir].show(250, 130, c3); | |
circle[cir].updateSize(cirSize); | |
cir++; | |
} | |
popMatrix(); | |
if (record < 1) { | |
endRecord(); | |
record = 0; | |
} | |
} | |
void serialEvent(Serial myPort) { | |
// read the serial buffer: | |
String myString = myPort.readStringUntil('\n'); | |
// if you got any bytes other than the linefeed: | |
if (myString != null) { | |
myString = trim(myString); | |
// if you haven't heard from the microncontroller yet, listen: | |
if (firstContact == false) { | |
if (myString.equals("hello")) { | |
myPort.clear(); // clear the serial port buffer | |
firstContact = true; // you've had first contact from the microcontroller | |
myPort.write('A'); // ask for more | |
} | |
} | |
// if you have heard from the microcontroller, proceed: | |
else { | |
// split the string at the commas | |
// and convert the sections into integers: | |
int sensors[] = int(split(myString, ',')); | |
// print out the values you got: | |
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { | |
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); | |
} | |
// add a linefeed after all the sensor values are printed: | |
println(); | |
if (sensors.length > 1) { | |
color1 = sensors[0]; | |
color2 = sensors[1]; | |
color3 = sensors[2]; | |
dist = sensors[3]; | |
xpos = sensors[4]; | |
ypos = sensors[5]; | |
record = sensors[6] * 255; | |
} | |
} | |
// when you've parsed the data you have, ask for more: | |
myPort.write("A"); | |
} | |
} | |
class Spin { | |
float speed = random(-0.05, 0.05); | |
float angle = 0; | |
float d; | |
int x, y; | |
float hueSq, saturationSq, brightnessSq; | |
float hueCi, saturationCi, brightnessCi; | |
float hue2, brightness2, saturation2; | |
Spin(int xpos2, int ypos2) { | |
x = xpos2; | |
y = ypos2; | |
} | |
void go() { | |
angle += speed; | |
} | |
void updateSize(float size) { | |
d = size; | |
} | |
} | |
class SquareSpin extends Spin { | |
SquareSpin(int x, int y) { | |
super(x, y); | |
} | |
void show(float hueSq, float saturationSq, float brightnessSq) { | |
pushMatrix(); | |
translate(((width/2)+x), ((height/2)+y)); | |
rotate(angle); | |
rectMode(CENTER); | |
noStroke(); | |
//colorMode(HSB, 100); | |
fill(hueSq, saturationSq, brightnessSq, 200); | |
rect(x/128, y/128, d, d); | |
popMatrix(); | |
//println(hueSq); | |
} | |
} | |
class CircleSpin extends Spin { | |
CircleSpin(int x, int y) { | |
super(x, y); | |
} | |
void show(float hueCi, float saturationCi, float brightnessCi) { | |
pushMatrix(); | |
translate(((width/2)+x), ((height/2)+y)); | |
rotate(angle); | |
ellipseMode(CORNER); | |
noFill(); | |
strokeWeight(2); | |
//colorMode(HSB, 100); | |
stroke(hueCi, saturationCi, brightnessCi); | |
ellipse(x/128, y/128, d, d); | |
popMatrix(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment