forked from mhenstell/elsignSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelsignSimulator.pde
101 lines (73 loc) · 2.03 KB
/
elsignSimulator.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;
import javax.media.opengl.GL;
import hypermedia.net.*;
import java.util.concurrent.*;
LinkedBlockingQueue newImageQueue;
PeasyCam pCamera;
Sign sign;
UDP udp;
int SIGN_WIDTH = 50;
int SIGN_HEIGHT = 6;
void setup() {
size(1024, 850, OPENGL);
colorMode(RGB,255);
frameRate(60);
// Turn on vsync to prevent tearing
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; //processing graphics object
GL gl = pgl.beginGL(); //begin opengl
gl.setSwapInterval(2); //set vertical sync on
pgl.endGL(); //end opengl
//size(1680, 1000, OPENGL);
pCamera = new PeasyCam(this, 0, 0, 0, 200);
pCamera.setMinimumDistance(.2);
pCamera.setMaximumDistance(150*10);
pCamera.setSuppressRollRotationMode();
pCamera.rotateX(.6);
pCamera.setWheelScale(0.05);
sign = new Sign(SIGN_WIDTH, SIGN_HEIGHT);
udp = new UDP(this, 58082);
udp.listen(true);
newImageQueue = new LinkedBlockingQueue();
}
int convertByte(byte b) {
return (b<0) ? 256+b : b;
}
void receive(byte[] data, String ip, int port) {
if (data[0] != 1) {
println("Packet header mismatch. Expected 1, got " + data[0]);
return;
}
if (data.length != (SIGN_WIDTH * SIGN_HEIGHT) * 3 + 1) {
println("Packet size mismatch. Expected many, got " + data.length);
return;
}
if (newImageQueue.size() > 0) {
println("Buffer full, dropping frame!");
return;
}
color[] newImage = new color[SIGN_WIDTH * SIGN_HEIGHT];
for (int i=0; i< SIGN_WIDTH * SIGN_HEIGHT; i++) {
newImage[i] = color(convertByte(data[i*3 + 1]),
convertByte(data[i*3 + 2]),
convertByte(data[i*3 + 3]));
}
try {
newImageQueue.put(newImage);
}
catch( InterruptedException e ) {
println("Interrupted Exception caught");
}
}
void draw() {
background(0);
lights();
noStroke();
if (newImageQueue.size() > 0) {
color[] newImage = (color[]) newImageQueue.poll();
sign.update(newImage);
}
sign.draw();
}