/
BlinkeyDomeSimulator.pde
171 lines (135 loc) · 3.84 KB
/
BlinkeyDomeSimulator.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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.*;
int DOME_RADIUS = 8;
int strips = 25; // Number of strips around the circumference of the sphere
int lights_per_strip = 45*3; // Number of lights along the strip
Boolean demoMode = true;
LinkedBlockingQueue newImageQueue;
UDP udp;
PeasyCam pCamera;
BlinkeyLights blinkeyLights;
Dome dome;
Hud hud;
ImageHud imageHud;
PFont font;
PImage groundTexture;
void setup() {
size(1024, 850, OPENGL);
frameRate(60);
// 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, 10, 200);
pCamera.setMinimumDistance(1);
pCamera.setMaximumDistance(150*10);
pCamera.setSuppressRollRotationMode();
pCamera.rotateX(-0.2);
// pCamera.lookAt(0, -10, -20);
// pCamera.rotateY(-HALF_PI);
pCamera.setWheelScale(0.05);
newImageQueue = new LinkedBlockingQueue();
udp = new UDP( this, 58082 );
udp.listen( true );
font = loadFont("Serif-24.vlw");
hud = new Hud();
dome = new Dome(DOME_RADIUS);
blinkeyLights = new BlinkeyLights(DOME_RADIUS, strips, lights_per_strip);
imageHud = new ImageHud(20, height-135-20, strips, lights_per_strip);
groundTexture = loadImage("Lost Lake.jpg");
}
int animationStep = 0;
int convertByte(byte b) {
return (b<0) ? 256+b : b;
}
void receive(byte[] data, String ip, int port) {
//println(" new datas!");
if (demoMode) {
println("Started receiving data from " + ip + ". Demo mode disabled.");
demoMode = false;
}
if (data[0] != 1) {
println("Packet header mismatch. Expected 1, got " + data[0]);
return;
}
if (data.length != strips*lights_per_strip*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[strips*lights_per_strip];
for (int i=0; i< strips*lights_per_strip; 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");
}
}
color[] MakeDemoFrame() {
int image_size = strips*lights_per_strip;
color[] imageData = new color[image_size];
for (int i = 0; i < imageData.length; i++) {
if (animationStep == i%10) {
imageData[i] = color(255, 255, 255);
}
else {
imageData[i] = color(0, 0, 0);
}
}
//animationStep = (animationStep+1)%blinkeyLights.size();
animationStep = (animationStep + 1)%10;
return imageData;
}
void draw() {
if (demoMode) {
color imageData[] = MakeDemoFrame();
blinkeyLights.update(imageData);
imageHud.update(imageData);
}
else if (newImageQueue.size() > 0) {
color[] newImage = (color[]) newImageQueue.poll();
blinkeyLights.update(newImage);
imageHud.update(newImage);
}
background(0);
lights();
stroke(92, 51);
fill(92, 51);
pushMatrix();
translate(0, 0.5, 0);
beginShape();
texture(groundTexture);
textureMode(NORMALIZED);
float bound = DOME_RADIUS*10*4;
vertex(-bound, .5, -bound, 0, 0);
vertex(bound, .5, -bound, 1, 0);
vertex(bound, .5, bound, 1, 1);
vertex(-bound, .5, bound, 0, 1);
endShape();
popMatrix();
dome.draw();
blinkeyLights.draw();
hud.draw();
imageHud.draw();
pCamera.beginHUD();
noLights();
fill(255,255,255);
textFont(font);
textAlign(CENTER);
text(int(frameRate),width - 50, height - 50);
pCamera.endHUD();
}