forked from UrbanLienert/blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockFinder.cpp
More file actions
394 lines (367 loc) · 16.8 KB
/
BlockFinder.cpp
File metadata and controls
394 lines (367 loc) · 16.8 KB
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
//
// BlockFinder.cpp
// Blocks
//
// Created by Urban Lienert on 02.02.20.
// Copyright © 2020 Urban Lienert. All rights reserved.
//
#include "BlockFinder.hpp"
#include "m_pd.h"
using namespace juce;
typedef struct cf_data
{
BlockComponent *component;
String name;
int value;
String option;
} cf_data;
// callback function for message thread
void* settingValueFunction (void* d) {
cf_data data = *(cf_data *)d;
BlockComponent* component = data.component;
component->setSettingsValue(data.name, data.value);
return nullptr;
};
void* settingsOptionFunction (void* d) {
cf_data data = *(cf_data *)d;
BlockComponent* component = data.component;
component->setSettingsValue(data.name, data.option);
return nullptr;
};
BlockFinder::BlockFinder()
{
// Register to receive topologyChanged() callbacks from pts.
pts.addListener (this);
serialsAndNames = new StringPairArray;
}
BlockFinder::~BlockFinder() {
pts.setActive(false);
serialsAndNames->~StringPairArray();
}
void BlockFinder::topologyChanged()
{
auto currentTopology = pts.getCurrentTopology();
for (auto& component : blockComponents) {
bool found = false;
for (auto& block : currentTopology.blocks) {
if (component->block->uid==block->uid) {
found = true;
break;
}
}
if (!found) {
blockComponents.removeObject(component);
}
}
post(("Detected " + String (currentTopology.blocks.size()) + " blocks:").toUTF8());
post("");
for (auto& block : currentTopology.blocks)
{
post((" Description: " + block->getDeviceDescription()).toUTF8());
post((" Serial number: " + block->serialNumber).toUTF8());
post("");
bool found = false;
for (auto& component : blockComponents) {
if (component->block->uid==block->uid) {
found = true;
break;
}
}
if (!found) {
BlockComponent *component = new BlockComponent(block, loadPrgram);
component->out_action = out_A;
component->out_info = out_B;
blockComponents.add(component);
}
}
// setting pdNames in components
updateComponents();
if (pts.isActive()) {
// send bang to output
outlet_bang(out_D);
outputTopology();
}
}
void BlockFinder::setPdNameForSerial(const char *serial, const char *name) {
serialsAndNames->set(String(serial), String(name));
updateComponents();
}
void BlockFinder::doBlockCommand(t_symbol *name, int argc, t_atom *argv) {
// removing list atom, if there is one
if (String(name->s_name).compare("list")==0) {
name = argv[0].a_w.w_symbol;
for (int i=1; i<argc; i++) {
argv[i-1] = argv[i];
}
argc --;
}
// searching for block
bool found = false;
for (BlockComponent* component : blockComponents) {
if (component->pdName->compare(name->s_name)==0) {
found = true;
if (argc>0) {
t_atom cAtom = argv[0];
if (cAtom.a_type==A_SYMBOL) {
String command = String(cAtom.a_w.w_symbol->s_name);
// set programm as default
if (command.compare("setdefault")==0) {
component->setDefault();
post("setting default program");
}
// mode command
else if (command.compare("mode")==0 && argc>1) {
t_atom pAtom = argv[1];
if (pAtom.a_type==A_SYMBOL) {
component->setLightpadMode(String(pAtom.a_w.w_symbol->s_name));
}
if (argc>2) {
t_atom gAtom = argv[2];
if (pAtom.a_type==A_SYMBOL && gAtom.a_type==A_FLOAT) {
int size = (int)gAtom.a_w.w_float;
if (size<1) size = 1;
if (size>5) size = 5;
component->setGridSize(size);
}
} else {
component->setGridSize(2);
}
}
// color command
else if (command.compare("color")==0 && argc>1) {
// set color for pads, faders, etc.
OwnedArray<LEDColour> *colors = new OwnedArray<LEDColour>();
int numColors = argc - 1;
for (int i=1; i<=numColors; i++) {
t_atom hAtom = argv[i];
if (hAtom.a_type==A_SYMBOL) {
String *hexString = new String(hAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
colors->add(color);
}
}
if (colors->size()>0) {
component->setColors(colors);
}
colors->~OwnedArray();
}
// set fader value command
else if (command.compare("fader")==0 && argc>2) {
t_atom fAtom1 = argv[1];
t_atom fAtom2 = argv[2];
if (fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT) {
int index = (int)fAtom1.a_w.w_float;
float value = fAtom2.a_w.w_float;
component->setFaderValue(index, value);
}
}
// set mixer fader and button value command
else if (command.compare("mixer")==0 && argc>3) {
t_atom sAtom = argv[1];
t_atom fAtom1 = argv[2];
t_atom fAtom2 = argv[3];
if (sAtom.a_type==A_SYMBOL && fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT) {
String subCommand = String(sAtom.a_w.w_symbol->s_name);
if (subCommand.compare("button")==0) {
int index = (int)fAtom1.a_w.w_float;
float value = fAtom2.a_w.w_float;
component->setMixerButtonValue(index, value);
} else if (subCommand.compare("fader")==0) {
int index = (int)fAtom1.a_w.w_float;
float value = fAtom2.a_w.w_float;
component->setMixerFaderValue(index, value);
}
}
}
// set led color command
else if (command.compare("led")==0 && argc>3) {
t_atom fAtom1 = argv[1];
t_atom fAtom2 = argv[2];
t_atom sAtom = argv[3];
if (fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT && sAtom.a_type==A_SYMBOL) {
int x = (int)fAtom1.a_w.w_float;
int y = fAtom2.a_w.w_float;
String *hexString = new String(sAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
component->setLEDColor(x - 1, y - 1, color);
}
}
// draw rect with color command
else if (command.compare("rect")==0 && argc>5) {
t_atom fAtom1 = argv[1];
t_atom fAtom2 = argv[2];
t_atom fAtom3 = argv[3];
t_atom fAtom4 = argv[4];
t_atom sAtom = argv[5];
if (fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT && fAtom3.a_type==A_FLOAT && fAtom4.a_type==A_FLOAT && sAtom.a_type==A_SYMBOL) {
int x = (int)fAtom1.a_w.w_float;
int y = (int)fAtom2.a_w.w_float;
int w = (int)fAtom3.a_w.w_float;
int h = (int)fAtom4.a_w.w_float;
String *hexString = new String(sAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
component->setRectColor(x-1, y-1, w, h, color);
}
}
// draw circle with color command
else if (command.compare("circle")==0 && argc>4) {
t_atom fAtom1 = argv[1];
t_atom fAtom2 = argv[2];
t_atom fAtom3 = argv[3];
t_atom sAtom = argv[4];
if (fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT && fAtom3.a_type==A_FLOAT && sAtom.a_type==A_SYMBOL) {
int x = (int)fAtom1.a_w.w_float;
int y = (int)fAtom2.a_w.w_float;
int r = (int)fAtom3.a_w.w_float;
String *hexString = new String(sAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
component->setCircleColor(x-1, y-1, r, color);
}
}
// draw triangle with color command
else if (command.compare("triangle")==0 && argc>5) {
t_atom fAtom1 = argv[1];
t_atom fAtom2 = argv[2];
t_atom fAtom3 = argv[3];
t_atom fAtom4 = argv[4];
t_atom sAtom = argv[5];
if (fAtom1.a_type==A_FLOAT && fAtom2.a_type==A_FLOAT && fAtom3.a_type==A_FLOAT && fAtom4.a_type==A_FLOAT && sAtom.a_type==A_SYMBOL) {
int x = (int)fAtom1.a_w.w_float;
int y = (int)fAtom2.a_w.w_float;
int s = (int)fAtom3.a_w.w_float;
int d = (int)fAtom4.a_w.w_float;
String *hexString = new String(sAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
component->setTriangleColor(x-1, y-1, s, d, color);
}
}
// draw number with color command
else if (command.compare("number")==0) {
if (argc>2) {
t_atom fAtom1 = argv[1];
t_atom sAtom = argv[2];
if (fAtom1.a_type==A_FLOAT && sAtom.a_type==A_SYMBOL) {
int n = (int)fAtom1.a_w.w_float;
String *hexString = new String(sAtom.a_w.w_symbol->s_name);
uint32 argb = 0xff000000 + hexString->getHexValue32();
LEDColour *color = new LEDColour(argb);
component->setNumberColor(n, color);
}
}
if (argc>1) {
t_atom sAtom = argv[1];
if (sAtom.a_type==A_SYMBOL) {
String symbol = String(sAtom.a_w.w_symbol->s_name);
if (symbol.compare("hide")==0) {
component->hideNumberColor();
}
}
}
}
// clear screen (drawing)
else if (command.compare("clear")==0) {
component->clearScreen();
}
// set block settings command
else if (command.compare("set")==0 && argc>2) {
t_atom sAtom = argv[1];
t_atom fAtom1 = argv[2];
String setting = String(sAtom.a_w.w_symbol->s_name);
if (sAtom.a_type==A_SYMBOL) {
MessageManager *messageManager = MessageManager::getInstance();
cf_data data;
data.component = component;
data.name = setting;
if (fAtom1.a_type==A_FLOAT) {
// value
int value = (int)fAtom1.a_w.w_float;
data.value = value;
// we have to call this in the messag thread
messageManager->callFunctionOnMessageThread(settingValueFunction, &data);
} else if (fAtom1.a_type==A_SYMBOL) {
// option
String option = String(fAtom1.a_w.w_symbol->s_name);
data.option = option;
// we have to call this in the messag thread
messageManager->callFunctionOnMessageThread(settingsOptionFunction, &data);
}
}
}
else {
error("no method for '%s'", command.toStdString().c_str());
}
}
}
}
}
if (!found) {
error("block '%s' not found", name->s_name);
}
}
void BlockFinder::pollInfos() {
for (BlockComponent* component : blockComponents) {
component->outputInfos();
}
}
void BlockFinder::updateComponents() {
const MessageManagerLock *mmLock = new MessageManagerLock();
for (BlockComponent* component : blockComponents) {
String name = serialsAndNames->getValue(String(component->block->serialNumber), String());
if (name.length()>0) {
component->pdName = new String(name);
}
}
mmLock->~MessageManagerLock();
mmLock = nullptr;
}
void BlockFinder::outputTopology() {
auto currentTopology = pts.getCurrentTopology();
for (BlockComponent* component : blockComponents) {
t_symbol *name = gensym(component->pdName->toStdString().c_str());
t_atom at[3];
Block::Array connectedBlocks = currentTopology.getDirectlyConnectedBlocks(component->block->uid);
for (auto& connectedBlock : connectedBlocks) {
String blockName = String();
for (BlockComponent* component : blockComponents) {
if (component->block->uid==connectedBlock->uid) {
blockName = *component->pdName;
}
}
Array<BlockDeviceConnection> connections = currentTopology.getConnectionsBetweenBlocks(component->block->uid, connectedBlock->uid);
for (auto& connection : connections) {
Block::ConnectionPort port;
if (connection.device1==component->block->uid) {
port = connection.connectionPortOnDevice1;
} else {
port = connection.connectionPortOnDevice2;
}
String edge = String();
switch (port.edge) {
case juce::Block::ConnectionPort::DeviceEdge::north:
edge = String("north");
break;
case juce::Block::ConnectionPort::DeviceEdge::east:
edge = String("east");
break;
case juce::Block::ConnectionPort::DeviceEdge::south:
edge = String("south");
break;
case juce::Block::ConnectionPort::DeviceEdge::west:
edge = String("west");
break;
default:
break;
}
SETSYMBOL(at, gensym(edge.toStdString().c_str()));
SETFLOAT(at + 1, (t_float)static_cast<float>(port.index));
SETSYMBOL(at + 2, gensym(blockName.toStdString().c_str()));
outlet_anything(out_C, name, 3, at);
}
}
}
}