-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFancyFixtures.pde
458 lines (397 loc) · 14.7 KB
/
FancyFixtures.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
* ##copyright##
* See LICENSE.md
*
* @author Maxime Damecour (http://nnvtn.ca)
* @version 0.4
* @since 2016-06-10
*/
// a few FreeLEDing systems for fancy DMX fixtures
// packet size should be implemented in serial specificaly, pushing only the buffer size.
// mode artnet universe
class FancyFixtures {
PApplet applet;
int channelCount;
byte[] byteBuffer;
ByteSender byteSender;
ArrayList<Fixture> fixtures;
ArrayList<Fixture> individualFixtures;
PGraphics overLayCanvas;
PGraphics colorCanvas;
PVector areaSize;
PVector areaPos;
boolean initialised;
boolean recording = false;
ArrayList<Byte> recordingBuffer;
int clipCount = 0;
// to check channels
int testChannel = -1;
int testValue = 255;
int ledStart = 0;
public FancyFixtures(PApplet _pa) {
applet = _pa;
fixtures = new ArrayList<Fixture>();
areaSize = new PVector(0,0);
areaPos = new PVector(0,0);
initialised = false;
}
// it all starts here
public void loadFile(String _file) {
initialised = false;
if(byteSender instanceof SerialSender) byteSender.disconnect();
fixtures = new ArrayList<Fixture>();
XML _xml = getXML(_file);
if(_xml == null) return;
if(parseSetup(_xml)) { // only continue if setup provided
parseGroups(_xml);
parseFixtures(_xml);
cumulateFixtures();
findSize();
overLayCanvas = createGraphics((int)areaSize.x, (int)areaSize.y, P2D);
colorCanvas = createGraphics((int)areaSize.x, (int)areaSize.y, P2D);
drawAllFixtures();
listFixtures();
initialised = true;
println("New led thingy at "+areaPos+" "+areaSize);
}
}
public boolean parseSetup(XML _xml) {
XML setup = _xml.getChild("setup");
if(setup == null) return false;
if(setup.getString("type") != null) {
if(setup.getString("type").equals("LED")) {
byteSender = new SerialSender(applet);
byteSender.connect(setup.getString("port"), setup.getInt("baud"));
int _size = ((SerialSender)byteSender).getCount()*3;
setupByteBuffer(_size);
return true;
} else if(setup.getString("type").equals("DMX")) {
byteSender = new SerialSender(applet);
byteSender.connect(setup.getString("port"), setup.getInt("baud"));
int _size = ((SerialSender)byteSender).getCount();
setupByteBuffer(_size);
return true;
} else if(setup.getString("type").equals("ARTNET")) {
byteSender = new ArtNetSender();
setupByteBuffer(setup.getInt("universes")*512);
byteSender.connect(setup.getString("host"));
return true;
}
}
return false;
}
public void parseGroups(XML _xml) {
XML[] groupData = _xml.getChildren("group");
for(XML xgroup : groupData) {
XML[] xseg = xgroup.getChildren("segment");
// Segment _seg;
for(XML seg : xseg) segmentStrip(seg);
}
}
public void parseFixtures(XML _xml) {
XML[] _fixtures = _xml.getChildren("fixture");
for(XML _fix : _fixtures)
parseFixture(_fix);
}
public void parseFixture(XML _xml) {
// for(XML)
XML[] _fix = _xml.getChildren("xyled");
for(XML _xyled : _fix){
int _adr = (int) _xyled.getFloat("a");
int _x = (int) _xyled.getFloat("x");
int _y = (int) _xyled.getFloat("y");
println(_adr+" "+_x+" "+_y);
if(_x < width && _x >= 0){
if(_y < height && _y >= 0){
if(_adr < channelCount && _adr >= 0){
addRGBFixture(_adr, _x, _y);
}
}
}
}
//
// println("**********************************************");
// println("**********************************************");
// println("**********************************************");
// println(_fix);
// println("**********************************************");
// println("**********************************************");
// println("**********************************************");
}
public void addRGBFixture(int _adr, int _x, int _y) {
Fixture _fix = new RGBFixture(_adr);
_fix.setPosition(_x,_y);
_fix.drawFixtureOverlay(overLayCanvas);
fixtures.add(_fix);
}
public void setupByteBuffer(int _size) {
channelCount = _size;
byteBuffer = new byte[channelCount]; // plus one for header
for(byte _b : byteBuffer) _b = byte(0);
}
// finds the smallest buffer size;
public void findSize() {
float _minX = width;
float _minY = height;
float _maxX = 0;
float _maxY = 0;
PVector _pos;
for(Fixture _fix : individualFixtures) {
_pos = _fix.getPosition();
if(_minX > _pos.x) _minX = _pos.x;
if(_maxX < _pos.x) _maxX = _pos.x;
if(_minY > _pos.y) _minY = _pos.y;
if(_maxY < _pos.y) _maxY = _pos.y;
}
int _margin = 10;
areaPos.set(_minX - _margin, _minY - _margin);
areaSize.set(_maxX + (_margin*2), _maxY + (_margin*2));
areaSize.sub(areaPos);
// if(areaSize.x < 2) areaSize.x = 10;
// if(areaSize.y < 2) areaSize.y = 10;
for(Fixture _fix : individualFixtures) {
_pos = _fix.getPosition();
_pos.sub(areaPos);
_fix.setPosition((int)_pos.x, (int)_pos.y);
}
}
public void cumulateFixtures() {
individualFixtures = new ArrayList();
for(Fixture _fix : fixtures) {
findSubFixtures(_fix);
}
}
public void findSubFixtures(Fixture _fix) {
if(!individualFixtures.contains(_fix)) individualFixtures.add(_fix);
if(_fix.getSubFixtures() != null) {
for(Fixture _child : _fix.getSubFixtures()) findSubFixtures(_child);
}
}
public void drawAllFixtures() {
overLayCanvas.beginDraw();
overLayCanvas.clear();
overLayCanvas.stroke(255);
overLayCanvas.noFill();
overLayCanvas.rect(0,0,areaSize.x-1,areaSize.y-1);
for(Fixture _fix : fixtures) _fix.drawFixtureOverlay(overLayCanvas);
overLayCanvas.endDraw();
}
////////////////////////////////////////////////////////////////////////////////////
///////
/////// FixtureCreation
///////
////////////////////////////////////////////////////////////////////////////////////
// XML segment to RGBStrip fixture
// in this case its /led START_ADR LED_COUNT
void segmentStrip(XML _seg) {
String[] cmd = split(_seg.getString("txt"), " ");
if(cmd[0].equals("/rgb") && cmd.length>1) {
// println(cmd[1]);
int addr = int(cmd[1]);
Fixture _fix = new RGBPar(addr);
_fix.setPosition((int)_seg.getFloat("aX"),(int)_seg.getFloat("aY"));
_fix.drawFixtureOverlay(overLayCanvas);
fixtures.add(_fix);
// println("Adding LEDs from: "+from+" to: "+to);
// addRGBFixture(addr,(int)_seg.getFloat("aX"), (int)_seg.getFloat("aY"));
} else if(cmd[0].equals("/aw") && cmd.length>1) {
// println(cmd[1]);
int addr = int(cmd[1]);
Fixture _fix = new AWPar(addr);
_fix.setPosition((int)_seg.getFloat("aX"),(int)_seg.getFloat("aY"));
_fix.drawFixtureOverlay(overLayCanvas);
fixtures.add(_fix);
// println("Adding LEDs from: "+from+" to: "+to);
// addRGBFixture(addr,(int)_seg.getFloat("aX"), (int)_seg.getFloat("aY"));
} else if(cmd[0].equals("/led") && cmd.length>2) {
// println(cmd[1]);
int addr = int(cmd[1])*3;
int count = int(cmd[2]);
// println("Adding LEDs from: "+from+" to: "+to);
RGBStrip _fix;
_fix = new RGBStrip(addr, count,
(int)_seg.getFloat("aX"),
(int)_seg.getFloat("aY"),
(int)_seg.getFloat("bX"),
(int)_seg.getFloat("bY"));
fixtures.add(_fix);
_fix.drawFixtureOverlay(overLayCanvas);
} else if(cmd[0].equals("/par5") && cmd.length>1) {
// println(cmd[1]);
int addr = int(cmd[1]);
// println("Adding LEDs from: "+from+" to: "+to);
NetoParFive _fix;
_fix = new NetoParFive(addr);
_fix.setPosition((int)_seg.getFloat("aX"),(int)_seg.getFloat("aY"));
// (int)_seg.getFloat("bX"),
// (int)_seg.getFloat("bY"));
fixtures.add(_fix);
_fix.drawFixtureOverlay(overLayCanvas);
} else if(cmd[0].equals("/matrix") && cmd.length > 3){
ZigZagMatrix _fix;
int _spacing = abs((int)_seg.getFloat("aY") - (int)_seg.getFloat("bY"));
_fix = new ZigZagMatrix(int(cmd[1]), int(cmd[2]), int(cmd[3]), _spacing);
println(int(cmd[1])+" "+int(cmd[2])+" "+int(cmd[3])+" "+_spacing);
_fix.setPosition((int)_seg.getFloat("aX"),(int)_seg.getFloat("aY"));
_fix.init();
fixtures.add(_fix);
_fix.drawFixtureOverlay(overLayCanvas);
}
}
////////////////////////////////////////////////////////////////////////////////////
///////
/////// Operation
///////
////////////////////////////////////////////////////////////////////////////////////
public void update(PGraphics _pg) {
if(!initialised || _pg == null) return;
colorCanvas.beginDraw();
colorCanvas.clear();
colorCanvas.image(_pg, -areaPos.x, -areaPos.y);
colorCanvas.endDraw();
// updateFixtures
parseGraphics(colorCanvas);
updateBuffer();
if(testChannel >= 0) {
byteBuffer[testChannel*3] = (byte)testValue;
byteBuffer[testChannel*3+1] = (byte)testValue;
byteBuffer[testChannel*3+2] = (byte)testValue;
}
// outputData
if(byteBuffer.length > 0) {
byteSender.sendData(byteBuffer);
if(recording) record(byteBuffer);
}
}
public void setChannel(int _ind, int _val) {
if(_ind < byteBuffer.length) {
byteBuffer[_ind] = (byte)_val;
// println(_ind+" "+_val);
}
for(Fixture _fix : fixtures){
_fix.setChannelManual(_ind, _val);
}
}
// force on a channel
public void setTestChannel(int _chan, int _val){
if(byteBuffer == null) return;
// turn off previous
if(testChannel >= 0){
byteBuffer[testChannel*3] = 0;
byteBuffer[testChannel*3+1] = 0;
byteBuffer[testChannel*3+2] = 0;
}
// set new
if( _chan*3+2 < byteBuffer.length){
testValue = (byte)_val;
testChannel = _chan;
}
}
public void enableRecording(boolean _b) {
recording = _b;
if(recording) {
recordingBuffer = new ArrayList<Byte>();
// make a header with channelCount
recordingBuffer.add((byte)((byteBuffer.length) >> 8)); // MSB
recordingBuffer.add((byte)(byteBuffer.length & 0xFF)); // LSB
} else {
byte[] ha = new byte[recordingBuffer.size()];
for(int i = 0; i < ha.length; i++) {
ha[i] = recordingBuffer.get(i);
}
//saveBytes(dataPath(PATH_TO_CAPTURE_FILES)+"/fixture_animations/"+String.format("ani_%02d.bin", clipCount++), ha);
println("Saved led animation");
}
}
private void record(byte[] _buff) {
for(int i = 0; i < _buff.length; i++) {
recordingBuffer.add(_buff[i]);
}
}
public void drawMap(PGraphics _pg) {
if(initialised && _pg != null) {
_pg.image(overLayCanvas, areaPos.x, areaPos.y);
// debugBuffer();
}
}
public PGraphics getOverlay(){
return overLayCanvas;
}
void debugBuffer() {
if(byteBuffer == null) return;
println("|---------------------------------------------------=");
for(int i = 0; i < 512; i++) {
print(" ("+i+" -> "+int(byteBuffer[i])+") ");
if(i%8 == 1) println();
}
println(" ");
println("|---------------------------------------------------=");
}
void listFixtures() {
println("|--------DMX FIXTURES---------|");
int _cnt = 0;
for(Fixture _fix : fixtures) {
println("============ "+(_cnt++)+" ==============");
println("Address : "+_fix.getAddress());
println("Name : "+_fix.getName());
println("description : "+_fix.getDescription());
println("=============================");
}
}
void parseGraphics(PGraphics _pg) {
_pg.loadPixels();
for(Fixture _fix : fixtures)
_fix.parseGraphics(_pg);
}
void updateBuffer() {
for(Fixture _fix : fixtures)
_fix.bufferChannels(byteBuffer);
}
public Fixture getFixture(int _ind) {
if(_ind < fixtures.size() && _ind >= 0) return fixtures.get(_ind);
else return null;
}
public XML getXML(String _file) {
XML _xml = null;
try {
_xml = loadXML(_file);
} catch(Exception e) {
println("FixtureMap XML file "+_file+" not found");
}
return _xml;
}
}
/*
// will take a xml file later ;)
public void populateFixturesISM(){
overLay = createGraphics(width, height, P2D);
overLay.beginDraw();
overLay.clear();
fixtures = new ArrayList<Fixture>();
// construct fixtures here.
Fixture _fix = new ColorFlexWAUV(2);
_fix.setPosition(448, 64);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
_fix = new RGBFixture(2);
_fix.setPosition(448, 128);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
_fix = new ColorFlexWAUV(12);
_fix.setPosition(480, 64);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
_fix = new RGBFixture(12);
_fix.setPosition(480, 128);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
_fix = new MPanel(22, 64, 64);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
_fix = new MPanel(152, 256, 64);
_fix.drawFixtureOverlay(overLay);
fixtures.add(_fix);
overLay.endDraw();
listFixtures();
}
*/