-
Notifications
You must be signed in to change notification settings - Fork 0
/
kindredspirit.pde
419 lines (381 loc) · 12.3 KB
/
kindredspirit.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import com.heroicrobot.dropbit.registry.*;
import com.heroicrobot.dropbit.devices.pixelpusher.Pixel;
import com.heroicrobot.dropbit.devices.pixelpusher.Strip;
import java.util.*;
class PixelPusherObserver implements Observer {
public boolean hasStrips = false;
public void update(Observable registry, Object updatedDevice) {
println("=== registry changed");
if (updatedDevice != null) {
println("device change: " + updatedDevice);
}
this.hasStrips = true;
}
}
final float globalShadeFactor = 0.0; //1.0; //0.85;
// my late 2011 MacBook Pro can easily handle 60fps
final int globalFrameRate = 60;
final int myScreenWidth = 800;
final int myScreenHeight = 700;
class Coordinate {
final float x, y, z;
Coordinate(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
float distance(Coordinate b) {
Coordinate a = this;
float dx = b.x - a.x;
float dy = b.y - a.y;
float dz = b.z - a.z;
return sqrt(pow(dx,2) + pow(dy,2) + pow(dz,2));
}
}
/* Represents a pixel in the KS model. Directly corresponds to a controller/strip/index. */
class VirtualPixel {
final int controllerId;
final int stripId;
final int pixelId;
final Coordinate coord;
int currentColor;
VirtualPixel(int controllerId, int stripId, int pixelId, float x, float y, float z) {
this.controllerId = controllerId;
this.stripId = stripId;
this.pixelId = pixelId;
this.coord = new Coordinate(x, y, z);
this.currentColor = 0;
}
}
/* Represents a waypoint in the KS model. */
class VirtualWayPoint {
final Coordinate coord;
VirtualWayPoint(int x, int y, int z) {
this.coord = new Coordinate(x, y, z);
}
}
/* Represents a strip of pixels in the KS model. */
class VirtualStrip {
final int controllerId;
final int stripId;
VirtualWayPoint wayPoints[];
VirtualStrip(int controllerId, int stripId, VirtualWayPoint wayPoints[]) {
this.controllerId = controllerId;
this.stripId = stripId;
this.wayPoints = wayPoints;
}
}
VirtualStrip ksVirtualStrips[];
VirtualPixel ksVirtualPixels[];
int controllerStripMap[][][];
float minX, maxX;
float minY, maxY;
void loadModel() {
String lines[] = loadStrings("model.csv");
List<VirtualStrip> virtualStrips = new ArrayList<VirtualStrip>();
for (String line : lines) {
line = trim(line);
if (line.equals("") || line.charAt(0) == '#') continue;
String[] parts = split(trim(line), "|");
if (parts.length != 2) {
println("*** line "+line+" split on | into too many parts: "+parts.length);
exit();
}
String controllerAndStripId[] = split(trim(parts[0]), ":");
int controllerId = int(controllerAndStripId[0]);
int stripId = int(controllerAndStripId[1]);
println("loadModel: controller:strip="+controllerId+":"+stripId);
String points[] = split(trim(parts[1]), ';');
List<VirtualWayPoint> virtualPoints = new ArrayList<VirtualWayPoint>();
for (String point : points) {
String pos[] = split(trim(point), ',');
int x = int(pos[0]);
int y = int(pos[1]);
int z = int(pos[2]);
virtualPoints.add(new VirtualWayPoint(x, y, z));
}
if (virtualPoints.size() < 2) {
println("!!! too few waypoints: " + virtualPoints.size());
exit();
}
VirtualStrip vs = new VirtualStrip(controllerId, stripId,
virtualPoints.toArray(new VirtualWayPoint[virtualPoints.size()]));
virtualStrips.add(vs);
}
ksVirtualStrips = virtualStrips.toArray(new VirtualStrip[virtualStrips.size()]);
println("loadModel: "+ksVirtualStrips.length+" strips loaded");
}
Animation live;
Animation preview;
DeviceRegistry registry;
PixelPusherObserver observer;
PFont titleFont;
PFont subtitleFont;
PFont sidebarFont;
ColorPicker previewPrimaryColorPicker, previewSecondaryColorPicker;
ColorPicker livePrimaryColorPicker, liveSecondaryColorPicker;
final int sidebarWidth = 100;
final int sidebarTextHeight = 18;
final int mainAreaWidth = myScreenWidth-sidebarWidth;
final int paneWidth = mainAreaWidth/2;
final int paneHeight = myScreenHeight-15;
final int leftPaneX = sidebarWidth;
final int rightPaneX = sidebarWidth + paneWidth;
final int colorPickerWidth = paneWidth/2;
final int colorPickerHeight = 100;
final int colorPickerY = paneHeight-(colorPickerHeight+30);
ColorPicker placeColorPicker(int x) {
return new ColorPicker(x, colorPickerY, colorPickerWidth, colorPickerHeight);
}
void setLiveAnimation(int index) {
Animation animation = createAnimation(index);
live = animation;
if (live.primaryColor != null) {
livePrimaryColorPicker = placeColorPicker(rightPaneX);
live.primaryColor = preview.primaryColor;
} else {
livePrimaryColorPicker = null;
}
if (live.secondaryColor != null) {
liveSecondaryColorPicker = placeColorPicker(rightPaneX+colorPickerWidth);
live.secondaryColor = preview.secondaryColor;
} else {
liveSecondaryColorPicker = null;
}
}
void setPreviewAnimation(int index) {
Animation animation = createAnimation(index);
preview = animation;
if (preview.primaryColor != null) {
previewPrimaryColorPicker = placeColorPicker(leftPaneX);
} else {
previewPrimaryColorPicker = null;
}
if (preview.secondaryColor != null) {
previewSecondaryColorPicker = placeColorPicker(leftPaneX+colorPickerWidth);
} else {
previewSecondaryColorPicker = null;
}
}
void setup() {
loadModel();
rasterizeModelToPixels();
initControllerStripMap();
titleFont = createFont("Arial", 16, true);
subtitleFont = createFont("Arial", 14, true);
sidebarFont = createFont("Arial", 12, true);
size(800, 700);
stroke(255);
background(0, 0, 0);
frameRate(globalFrameRate);
setLiveAnimation(0);
setPreviewAnimation(0);
registry = new DeviceRegistry();
observer = new PixelPusherObserver();
registry.addObserver(observer);
setupSpectro();
}
void drawTitle() {
textFont(titleFont);
fill(255);
text("Kindred Spirit Lighting Console", sidebarWidth, 15);
textFont(sidebarFont);
text("fps: "+int(frameRate), width-50, 15);
}
boolean isSidebarItemHovered(int i) {
int x = 0;
int y = sidebarTextHeight * i;
if (mouseX >= x && mouseX < x+sidebarWidth
&& mouseY >= y && mouseY < y+sidebarTextHeight) {
return true;
}
return false;
}
void drawSidebar() {
textFont(sidebarFont);
for (int i = 0; i < animations.length; i++) {
int y = sidebarTextHeight * i;
if (isSidebarItemHovered(i)) {
fill(0x00, 0x99, 0x00);
stroke(0xFF, 0xFF, 0xFF);
rect(0, y+2, sidebarWidth-5, sidebarTextHeight+2);
fill(0xFF, 0xFF, 0xFF);
} else {
fill(0x99, 0x99, 0x99);
}
text(animations[i], 0, y+sidebarTextHeight);
}
}
void drawColorSelector(Animation a, ColorPicker colorPicker, int leftSide) {
if (colorPicker != null) {
colorPicker.draw();
} else {
stroke(0x99, 0x99, 0x99);
line(leftSide, colorPickerY, leftSide+colorPickerWidth, colorPickerY+colorPickerHeight);
line(leftSide+colorPickerWidth, colorPickerY, leftSide, colorPickerY+colorPickerHeight);
}
}
void drawLED(int x, int y, int c)
{
// draw the point at x,y and also immediately
// above and below and to either side
// TODO: get fancy and render the neighboring ones a bit darker, maybe
pixels[y*width + x] = c;
pixels[(y-1)*width + x] = c;
pixels[(y+1)*width + x] = c;
pixels[y*width + (x-1)] = c;
pixels[y*width + (x+1)] = c;
}
void drawDemoControls(String which, Animation animation, ColorPicker primaryColorPicker,
ColorPicker secondaryColorPicker, int x, int y, int w, int h) {
textFont(subtitleFont);
fill(255);
text(which+": "+animation.name, x, y);
drawSlider(animation, x, w);
drawColorSelector(animation, primaryColorPicker, x);
drawColorSelector(animation, secondaryColorPicker, x+colorPickerWidth);
}
void drawDemoLEDs(Animation animation, int x, int y, int w) {
int xoff = x;
int yoff = y+30;
for (VirtualPixel vpixel : animation.pixels) {
int c = vpixel.currentColor;
if (c == 0) continue; // no need to render black
int r = (c >> 16) & 0xFF;
int g = (c >> 8) & 0xFF;
int b = c & 0xFF;
color col = color(r,g,b);
float flatten_x = 0;
float flatten_y = 0;
// "squash" the model into 2d space
if (vpixel.coord.x >= 0) {
if (vpixel.coord.z >= 0) {
flatten_x = -vpixel.coord.y;
flatten_y = vpixel.coord.x;
} else {
flatten_x = vpixel.coord.y;
flatten_y = -vpixel.coord.x;
}
if (flatten_y < 0) {
//point(xoff+(w/2)+flatten_x, yoff-flatten_y);
drawLED(int(xoff+(w/2)+flatten_x), int((yoff+100)-flatten_y), col);
} else {
//point(xoff+(w/2)+flatten_x, yoff+flatten_y);
drawLED(int(xoff+(w/2)+flatten_x), int((yoff+100)+flatten_y), col);
}
} else {
flatten_x = -vpixel.coord.z;
flatten_y = 140-vpixel.coord.y;
drawLED(int(xoff+(w/2)+flatten_x), int(yoff+flatten_y), col);
}
}
}
void sendState(Animation animation) {
if (!observer.hasStrips) {
//println("observer.hasStrips is false; no strips available?");
return;
}
registry.startPushing();
registry.setAutoThrottle(true);
for (Strip strip : registry.getStrips()) {
int controllerId = strip.getPusher().getControllerOrdinal();
if (controllerId > maxControllerId) continue;
int stripId = strip.getStripNumber();
int pixelIndex = 0;
int c = 0;
// TODO: use the length of the strip in the model rather than asking the
// controller how long it thinks the strips are
for (int i = 0; i < strip.getLength(); i++) {
try {
pixelIndex = controllerStripMap[controllerId][stripId][i];
c = animation.pixels[pixelIndex].currentColor;
// TODO: track a prevColor so that we don't strip.setPixel unless it changed?
strip.setPixel(shade(c, globalShadeFactor), i);
} catch (ArrayIndexOutOfBoundsException e) {
// skip LED strips that aren't specified in the model
}
}
}
}
void draw() {
if (mousePressed) { // support dragging
if (previewPrimaryColorPicker != null) {
Integer changeColor = previewPrimaryColorPicker.getColorOverMouse();
if (changeColor != null) {
preview.primaryColor = changeColor;
println("preview primary color change: "+changeColor);
}
}
if (previewSecondaryColorPicker != null) {
Integer changeColor = previewSecondaryColorPicker.getColorOverMouse();
if (changeColor != null) {
preview.secondaryColor = changeColor;
println("preview secondary color change: "+changeColor);
}
}
if (livePrimaryColorPicker != null) {
Integer changeColor = livePrimaryColorPicker.getColorOverMouse();
if (changeColor != null) {
live.primaryColor = changeColor;
println("live primary color change: "+changeColor);
}
}
if (liveSecondaryColorPicker != null) {
Integer changeColor = liveSecondaryColorPicker.getColorOverMouse();
if (changeColor != null) {
live.secondaryColor = changeColor;
println("live secondary color change: "+changeColor);
}
}
}
background(0);
drawTitle();
drawSidebar();
tickSpectro();
preview.tick();
live.tick();
drawDemoControls("preview", preview, previewPrimaryColorPicker,
previewSecondaryColorPicker, leftPaneX, 30,
paneWidth, paneHeight);
drawDemoControls("live", live, livePrimaryColorPicker,
liveSecondaryColorPicker, rightPaneX, 30,
paneWidth, paneHeight);
// point() is ridiculously slow; do this {load,update}Pixels() thing so
// we can access the pixels[] buffer directly
loadPixels();
drawDemoLEDs(preview, leftPaneX, 30, paneWidth);
drawDemoLEDs(live, rightPaneX, 30, paneWidth);
updatePixels();
sendState(live);
//println("frameRate: "+frameRate);
}
void mouseClicked() {
/* Was a sidebar list item clicked? */
for (int i = 0; i < animations.length; i++) {
if (isSidebarItemHovered(i)) {
setPreviewAnimation(i);
break;
}
}
}
void keyPressed() {
if (keyCode == ENTER || keyCode == RETURN) {
for (int i = 0; i < animations.length; i++) {
if (preview.name == animations[i]) {
setLiveAnimation(i);
live.speedPct = preview.speedPct;
break;
}
}
} else if (key == 'm') {
String file = "/tmp/vertices.txt";
dumpControllerStripMap(file);
println("** dumped controller-strip-vertex map to "+file);
} else {
println("unknown keyPressed: "+key);
}
}
void stop() {
stopSpectro();
super.stop();
}