Created
February 16, 2017 08:29
-
-
Save tina1998612/6248c92b96ced300de39100ada2cfcdb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 java.util.*; | |
Serial myPort; | |
int globalWidth = 80; | |
int globalHeight = 60; | |
int globalSpeed = 0; | |
int displaySpeed; | |
int pixelSide = 5; | |
Boolean[][] pixelArray = new Boolean[globalHeight][globalWidth]; | |
int imageX = 10, imageY = 10; | |
int background_color = 17; | |
color red = #ff0000; | |
color black = #000000; | |
int arrayPosX, arrayPosY; | |
int leftX = 600, leftY = 600; | |
int middleX = 700, middleY = 600; | |
int rightX = 800, rightY = 600; | |
int diameter = 50; | |
Boolean stop = false; | |
String dir = " "; | |
char c = 'x'; | |
void setup() { | |
printArray(Serial.list()); | |
myPort = new Serial(this, Serial.list()[1], 115200); | |
myPort.buffer(1); | |
size(900, 650); | |
arrayPosX = 0; | |
arrayPosY = 0; | |
background(background_color); | |
for (int y=0; y<globalHeight; y++) { | |
for (int x=0; x<globalWidth; x++) { | |
pixelArray[y][x] = false; | |
} | |
} | |
fill(0); | |
ellipse(leftX, leftY, diameter, diameter); | |
ellipse(middleX, middleY, diameter, diameter); | |
ellipse(rightX, rightY, diameter, diameter); | |
} | |
void getImage(int data) { | |
String binData = ""; | |
int strLen = 0; | |
binData = Integer.toBinaryString(data); | |
strLen = binData.length(); | |
//println(binData); | |
for (int i=0; i<8; i++) { | |
if (i < strLen) { | |
if (binData.charAt(strLen-1-i) == '1') { | |
pixelArray[arrayPosY][arrayPosX+7-i] = true; | |
} else { | |
pixelArray[arrayPosY][arrayPosX+7-i] = false; | |
} | |
} else { | |
pixelArray[arrayPosY][arrayPosX+7-i] = false; | |
} | |
} | |
arrayPosX += 8; | |
if (arrayPosX >= globalWidth) { | |
arrayPosX = 0; | |
arrayPosY++; | |
} | |
if (arrayPosY >= globalHeight) { | |
arrayPosY = 0; | |
} | |
} | |
void outputImage() { | |
for (int y=0; y<globalHeight; y++) { | |
for (int x=0; x<globalWidth; x++) { | |
if (pixelArray[y][x]) { | |
fill(black); | |
} else { | |
fill(255); | |
} | |
noStroke(); | |
rect(imageX + pixelSide*x, imageY + pixelSide*y, pixelSide, pixelSide); | |
} | |
} | |
} | |
void keyPressed() { | |
if(key == ' ') { | |
myPort.write(' '); | |
stop = true; | |
} | |
else { | |
stop = false; | |
if(key == 'w') myPort.write('w'); | |
else if(key == 'a') myPort.write('a'); | |
else if(key == 's') myPort.write('s'); | |
else if(key == 'd') myPort.write('d'); | |
else if(key == ',') { | |
myPort.write(','); | |
if(globalSpeed-20>=0) globalSpeed-=20; | |
} | |
else if(key == '.') { | |
myPort.write('.'); | |
if(globalSpeed+20<=450) globalSpeed+=20; | |
} | |
} | |
} | |
void keyReleased(){ | |
if(key != ',' && key != '.') { | |
myPort.write('r'); | |
stop = true; | |
} | |
} | |
void draw() { | |
fill(200); | |
stroke(255); | |
rect(460, 40, 370, 400, 10); | |
textSize(32); | |
fill(0); | |
text("current speed: ", 500, 90); | |
if(stop) { | |
fill(red); | |
text("stopped!", 580, 130); | |
} | |
fill(0); | |
text(globalSpeed, 750, 90); | |
textSize(32); | |
fill(0); | |
switch(key){ | |
case 'w': | |
dir = "forward"; | |
break; | |
case 'a': | |
dir = "left"; | |
break; | |
case 's': | |
dir = "backward"; | |
break; | |
case 'd': | |
dir = "right"; | |
break; | |
case '.': | |
dir = ">"; | |
break; | |
case ',': | |
dir = "<"; | |
break; | |
case ' ': | |
dir = " "; | |
break; | |
} | |
text(dir, 580, 200); | |
while (myPort.available() > 0) { | |
int inputInt = myPort.read(); | |
if (inputInt == 170) { | |
int i = 0; | |
arrayPosX = 0; | |
arrayPosY = 0; | |
while (i<globalWidth*globalHeight/8) { | |
print(' '); | |
if (myPort.available() > 0) { | |
inputInt = myPort.read(); | |
getImage(inputInt); | |
i++; | |
} | |
} | |
outputImage(); | |
delay(1); | |
} | |
else if(inputInt == 171){ | |
if(myPort.available() > 0) { | |
//c = myPort.readChar(); | |
textSize(32); | |
fill(0); | |
text(myPort.readChar(), 580, 250); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment