-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathPotentiometer.ts
More file actions
670 lines (656 loc) · 25.7 KB
/
Potentiometer.ts
File metadata and controls
670 lines (656 loc) · 25.7 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import { CircuitElement } from '../CircuitElement';
import { BreadBoard } from '../General';
import { Vector } from './Collision';
import { Point } from '../Point';
import { LED } from '../outputs/Led';
/**
* Declare window so that custom created function don't throw error
*/
declare var window;
/**
* Potentiometer Class
*/
export class Potentiometer extends CircuitElement {
/**
* Types of potentiometer name.
*/
static variants: string[];
/**
* Types of potentiometer resistance
*/
static variantsValue: number[];
/**
* Selected potentiometer type.
*/
selectedIndex: number;
/**
* Arduino Pin connected to 0 of potentiometer
*/
arduinoEndZero: any;
/**
* Arduino Pin connected to 2 of potentiometer
*/
arduinoEndTwo: any;
/**
* Set to keep track of visited nodes
*/
visitedNodesv2 = new Set();
/**
* Check the connection type
*/
isRheostat: boolean = null;
/**
* Stores visited LEDs' ids
*/
visitedLEDs = new Set();
/**
* Stores connected LEDs
*/
connectedLEDs: LED[] = [];
/**
* Stores the anle
*/
private savedAngle = 0;
/**
* Potentiometer constructor
* @param canvas Raphael Canvas (Paper)
* @param x position x
* @param y position y
*/
constructor(public canvas: any, x: number, y: number) {
super('PotentioMeter', x, y, 'Potentiometer.json', canvas);
}
/** init is called when the component is complety drawn to the canvas */
init() {
Potentiometer.variants = this.data.variants;
Potentiometer.variantsValue = this.data.value;
this.data.value = [];
this.data.variants = [];
this.data = null;
}
/**
* Returns the client point respective to the svg
* @param x X positon
* @param y y position
*/
svgPoint(x, y) {
const pt = window['holder_svg'].createSVGPoint();
pt.x = x;
pt.y = y;
return pt.matrixTransform(window.canvas.canvas.getScreenCTM().inverse());
}
/**
* Rotates the dial and sets the analog value
* @param center Center of the Potentiometer
* @param clientX Mouse X
* @param clientY Mouse Y
*/
rotateDial(center: Vector, clientX: number, clientY: number) {
const point = this.svgPoint(clientX, clientY);
const difX = point.x - center.x;
const difY = point.y - center.y;
let ang = Math.atan2(difY, difX);
if (ang < 0) {
ang += 2 * Math.PI;
}
// if(line){
// line.remove();
// line = null;
// }
// const ex = center.x + 100*Math.cos(ang);
// const ey = center.y + 100*Math.sin(ang);
// line = this.canvas.path(`M${center.x},${center.y}L${ex},${ey}`)
ang *= (180 / Math.PI);
ang = (ang + 225) % 360;
if (ang > 268) {
ang = 268;
}
let to;
let intp = 0;
// console.log(ang / 268);
if (this.arduinoEndZero) {
to = Math.max(
this.nodes[0].value,
this.nodes[1].value
);
intp = (ang / 268) * to;
} else if (this.arduinoEndTwo) {
to = Math.max(
this.nodes[1].value,
this.nodes[2].value
);
intp = to - ((ang / 268) * to);
}
if (to < 0) {
window['showToast']('Potentiometer Not Connected');
return;
}
this.elements[1].transform(`r${ang}`);
this.nodes[1].setValue(intp, this.nodes[1]);
}
/**
* Rotates the dial and sets the analog value for Rheostat connection
* @param center Center of the Potentiometer
* @param clientX Mouse X
* @param clientY Mouse Y
*/
rotateDialRheostat(center: Vector, clientX: number, clientY: number) {
const point = this.svgPoint(clientX, clientY);
const difX = point.x - center.x;
const difY = point.y - center.y;
const resistanceValue = Potentiometer.variantsValue[this.selectedIndex];
let ang = Math.atan2(difY, difX);
if (ang < 0) {
ang += 2 * Math.PI;
}
// if(line){
// line.remove();
// line = null;
// }
// const ex = center.x + 100*Math.cos(ang);
// const ey = center.y + 100*Math.sin(ang);
// line = this.canvas.path(`M${center.x},${center.y}L${ex},${ey}`)
ang *= (180 / Math.PI);
ang = (ang + 225) % 360;
if (ang > 268) {
ang = 268;
}
let to;
let calResistance = -1;
const minVoltage = 0;
const minLEDResistance = 100;
const maxLEDResitance = 466;
if (this.arduinoEndZero) {
to = Math.max(
this.nodes[0].value,
this.nodes[1].value
);
// intp = (ang / 268) * to;
calResistance = ((ang / 268) * resistanceValue);
this.sendResistance(calResistance);
} else if (this.arduinoEndTwo) {
to = Math.max(
this.nodes[1].value,
this.nodes[2].value
);
calResistance = resistanceValue - ((ang / 268) * resistanceValue);
this.sendResistance(calResistance);
}
if (to < 0) {
window['showToast']('Potentiometer Not Connected');
return;
}
this.elements[1].transform(`r${ang}`);
this.nodes[1].setValue(to, this.nodes[1]);
}
/**
* Function provides component details
* @param keyName Unique Class name
* @param id Component id
* @param body body of property box
* @param title Component title
*/
properties(): { keyName: string; id: number; body: HTMLElement; title: string; } {
const body = document.createElement('div');
body.style.display = 'flex';
body.style.flexDirection = 'column';
const label = document.createElement('label');
label.innerText = 'Resistance';
const select = document.createElement('select');
let tmp = '';
for (const val of Potentiometer.variants) {
tmp += `<option>${val} Ω</option>`;
}
select.innerHTML = tmp;
select.selectedIndex = this.selectedIndex;
select.onchange = () => {
this.selectedIndex = select.selectedIndex;
};
body.append(label);
body.append(select);
return {
keyName: this.keyName,
id: this.id,
title: 'Potentiometer',
body
};
}
/**
* Called on start simulation. Add Event listener.
*/
initSimulation(): void {
if (!this.areNodesConnectedProperly()) {
window.showToast('Potentiometer not connected properly.');
return;
}
const attr = this.elements[1].attr();
const center = {
x: attr.x + attr.width / 2 + this.tx,
y: attr.y + attr.height / 2 + this.ty
};
// Get the saved angle from localStorage
if (this.savedAngle !== null) {
// Apply the saved rotation angle to the dial, ensuring it's centered
this.elements[1].transform(`t${this.tx},${this.ty} r${this.savedAngle} t${-this.tx},${-this.ty}`);
console.log('Restoring saved angle:', this.savedAngle);
} else {
// Default behavior if no angle is saved (first time or reset)
this.elements[1].transform(`t${this.tx},${this.ty} t${-this.tx},${-this.ty}`);
}
this.elements[1].attr({
x: attr.x + this.tx,
y: attr.y + this.ty
});
this.elements.undrag();
this.elements.unmousedown();
if (!this.isRheostat) {
this.elements.drag((_, __, mX, mY) => {
this.rotateDial(center, mX, mY);
}, (mX, mY) => {
this.rotateDial(center, mX, mY);
},
(ev: MouseEvent) => {
this.rotateDial(center, ev.clientX, ev.clientY);
});
} else {
this.elements.drag((_, __, mX, mY) => {
this.rotateDialRheostat(center, mX, mY);
}, (mX, mY) => {
this.rotateDialRheostat(center, mX, mY);
},
(ev: MouseEvent) => {
this.rotateDialRheostat(center, ev.clientX, ev.clientY);
});
}
// Find out the LEDs in the circuit
this.getRecLED(this.nodes[0], 'Terminal 1');
this.getRecLED(this.nodes[1], 'WIPER');
this.getRecLED(this.nodes[2], 'Ternimal 2');
// Get Arduino Connected ends for terminal 1 & terminal 2
this.arduinoEndZero = this.getRecArduinov2(this.nodes[0], 'Terminal 1');
this.arduinoEndTwo = this.getRecArduinov2(this.nodes[2], 'Terminal 2');
if (this.arduinoEndZero) {
// TODO : If arduino is connected to Terminal 1 of potentiometer
// set WIPER value 0
this.nodes[1].setValue(0, this.nodes[1]);
} else if (this.arduinoEndTwo) {
// TODO : If arduino is connected to Terminal 2 of potentiometer
// set WIPER value as from Terminal 2
this.nodes[1].setValue(this.nodes[2].value, this.nodes[1]);
}
}
/**
* Save the Selected type in database
*/
SaveData() {
return {
value: this.selectedIndex
};
}
/**
* Load the Selected type.
* @param data Saved Data
*/
LoadData(data: any) {
if (data.data && data.data.value > 0) {
this.selectedIndex = data.data.value;
} else {
this.selectedIndex = 0;
}
}
/**
* Reset transformation add add event listeners.
*/
closeSimulation(): void {
let rotationAngle;
rotationAngle = this.savedAngle;
console.log('Current Rotation Angle:', rotationAngle);
// Get the current transform value of the potentiometer
const transform = this.elements[1].transform();
// If the transform includes rotation (it may be in a string or matrix form)
if (Array.isArray(transform) && transform[0][0] === 'r') {
rotationAngle = transform[0][1]; // Extract the rotation angle from the array
}
if (this.savedAngle !== rotationAngle) {
this.savedAngle = rotationAngle; // Only save the new angle if it has changed
console.log('Rotation Angle to Save:', this.savedAngle);
}
const attr = this.elements[1].attr();
this.elements[1].attr({
x: attr.x - this.tx,
y: attr.y - this.ty
});
// Set the transform origin to center and apply rotation relative to center
this.elements[1].transform(`t${this.tx},${this.ty} r${rotationAngle}`);
this.elements.undrag();
this.elements.unmousedown();
this.setClickListener(null);
this.setDragListeners();
this.isRheostat = null;
this.visitedLEDs.clear();
this.connectedLEDs = [];
}
/**
* Checks if nodes of Potentiometer are connected either as
* 1. Rheostat: Wiper + Left Pin (or) Wiper + Right Pin
* 2. Voltage divider: All pins
*/
areNodesConnectedProperly() {
if (this.nodes[0].isConnected() && this.nodes[1].isConnected() && this.nodes[2].isConnected()) {
const leftNode = this.getRecArduinov2(this.nodes[0], 'Terminal 1');
const rightNode = this.getRecArduinov2(this.nodes[2], 'Terminal 2');
if (leftNode && rightNode) {
if (leftNode.label === 'GND' && rightNode.value > 0) {
this.isRheostat = false;
return true;
} else if (rightNode.label === 'GND' && leftNode.value > 0) {
this.isRheostat = false;
return true;
}
}
}
if (this.nodes[0].isConnected() && this.nodes[1].isConnected()) {
this.isRheostat = true;
return true;
} else if (this.nodes[1].isConnected() && this.nodes[2].isConnected()) {
this.isRheostat = true;
return true;
}
return false;
}
/**
* Function to transfer resistance to connected LEDs
* ToDo: Function is hardcoded
* ToDo: Make it work for other components
*/
sendResistance(resistance: number) {
for (const led of this.connectedLEDs) {
led.setVariableResistance(resistance);
}
}
/**
* Return the node which is connected to arduino by recursively finding connected node
* @param node The Node which need to be checked
*/
getRecArduinov2(node: Point, startedOn: string) {
try {
if (node.connectedTo.start.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to start node
this.visitedNodesv2.clear();
return node.connectedTo.start;
} else if (node.connectedTo.end.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to end node
this.visitedNodesv2.clear();
return node.connectedTo.end;
} else if (node.connectedTo.start.parent.keyName === 'BreadBoard' && !this.visitedNodesv2.has(node.connectedTo.start.gid)) {
// TODO: Call recursive BreadBoard handler function if node is connected to Breadboard && visited nodes doesn't have node's gid
return this.getRecArduinoBreadv2(node, startedOn);
} else if (node.connectedTo.end.parent.keyName === 'BreadBoard' && !this.visitedNodesv2.has(node.connectedTo.end.gid)) {
// TODO: Call recursive BreadBoard handler function if node is connected to Breadboard && visited nodes doesn't have node's gid
return this.getRecArduinoBreadv2(node, startedOn);
} else if (node.connectedTo.end.parent.keyName === 'Battery9v' && window.scope.ArduinoUno.length === 0) {
// TODO: Return false if node's end is connected to 9V Battery
return false;
} else if (node.connectedTo.end.parent.keyName === 'CoinCell' && window.scope.ArduinoUno.length === 0) {
// TODO: Return false if node's end is connected to Coin Cell
return false;
} else if (node.connectedTo.end.parent.keyName === 'RelayModule') {
// TODO: Handle RelayModule
if (startedOn === 'POSITIVE') {
// If search was started on Positive node then return connected node of VCC in Relay
return this.getRecArduinov2(node.connectedTo.end.parent.nodes[3], startedOn);
} else if (startedOn === 'NEGATIVE') {
// If search was started on Negative node then return connected node of GND in Relay
return this.getRecArduinov2(node.connectedTo.end.parent.nodes[5], startedOn);
}
} else {
// TODO: If nothing matches
// IF/ELSE: Determine if start is to be used OR end for further recursion
if (node.connectedTo.end.gid !== node.gid) {
// Loops through all nodes in parent
for (const e in node.connectedTo.end.parent.nodes) {
// IF: gid is different && gid not in visited node
if (node.connectedTo.end.parent.nodes[e].gid !== node.connectedTo.end.gid
&& !this.visitedNodesv2.has(node.connectedTo.end.parent.nodes[e].gid) && node.connectedTo.end.parent.nodes[e].isConnected()) {
// add gid in visited nodes
this.visitedNodesv2.add(node.connectedTo.end.parent.nodes[e].gid);
// call back Arduino Recursive Fn
return this.getRecArduinov2(node.connectedTo.end.parent.nodes[e], startedOn);
}
}
} else if (node.connectedTo.start.gid !== node.gid) {
// Loops through all nodes in parent
for (const e in node.connectedTo.start.parent.nodes) {
// IF: gid is different && gid not in visited node
if (node.connectedTo.start.parent.nodes[e].gid !== node.connectedTo.start.gid
&& !this.visitedNodesv2.has(node.connectedTo.start.parent.nodes[e].gid)
&& node.connectedTo.start.parent.nodes[e].isConnected()) {
// add gid in visited nodes
this.visitedNodesv2.add(node.connectedTo.start.parent.nodes[e].gid);
// call back Arduino Recursive Fn
return this.getRecArduinov2(node.connectedTo.start.parent.nodes[e], startedOn);
}
}
}
}
} catch (e) {
console.warn(e);
return false;
}
}
/**
* Recursive Function to handle BreadBoard
* @param node Node which is to be checked for BreadBoard
*/
private getRecArduinoBreadv2(node: Point, startedOn: string) {
// IF/ELSE: Determine if start is to be used OR end for further recursion
if (node.connectedTo.end.gid !== node.gid) {
const bb = (node.connectedTo.end.parent as BreadBoard);
// loop through joined nodes of breadboard
for (const e in bb.joined) {
if (bb.joined[e].gid !== node.connectedTo.end.gid) {
// Run only if substring matches
if (bb.joined[e].label.substring(1, bb.joined[e].label.length)
=== node.connectedTo.end.label.substring(1, node.connectedTo.end.label.length)) {
const ascii = node.connectedTo.end.label.charCodeAt(0);
const currAscii = bb.joined[e].label.charCodeAt(0);
// add gid to VisitedNode
this.visitedNodesv2.add(bb.joined[e].gid);
// IF/ELSE: determine which part of breadboard is connected
if (ascii >= 97 && ascii <= 101) {
if (bb.joined[e].isConnected() && (currAscii >= 97 && currAscii <= 101)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
} else if (ascii >= 102 && ascii <= 106) {
if (bb.joined[e].isConnected() && (currAscii >= 102 && currAscii <= 106)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
} else {
if (bb.joined[e].isConnected() && (bb.joined[e].label === node.connectedTo.end.label)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
}
}
}
}
} else if (node.connectedTo.start.gid !== node.gid) {
const bb = (node.connectedTo.start.parent as BreadBoard);
// loop through joined nodes of breadboard
for (const e in bb.joined) {
if (bb.joined[e].gid !== node.connectedTo.start.gid) {
// Run only if substring matches
if (bb.joined[e].label.substring(1, bb.joined[e].label.length)
=== node.connectedTo.start.label.substring(1, node.connectedTo.start.label.length)) {
const ascii = node.connectedTo.start.label.charCodeAt(0);
const currAscii = bb.joined[e].label.charCodeAt(0);
// add gid to VisitedNode
this.visitedNodesv2.add(bb.joined[e].gid);
// IF/ELSE: determine which part of breadboard is connected
if (ascii >= 97 && ascii <= 101) {
if (bb.joined[e].isConnected() && (currAscii >= 97 && currAscii <= 101)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
} else if (ascii >= 102 && ascii <= 106) {
if (bb.joined[e].isConnected() && (currAscii >= 102 && currAscii <= 106)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
} else {
if (bb.joined[e].isConnected() && (bb.joined[e].label === node.connectedTo.end.label)) {
return this.getRecArduinov2(bb.joined[e], startedOn);
}
}
}
}
}
}
}
/**
* Return the node which is connected to arduino by recursively finding LEDs inside the circuit
* @param node The Node which need to be checked
*/
getRecLED(node: Point, startedOn: string) {
try {
if (node.connectedTo.start.parent.keyName === 'LED') {
const led = (node.connectedTo.start.parent as LED);
if (!this.visitedLEDs.has(led.getID())) {
this.connectedLEDs.push(led);
this.visitedLEDs.add(led.getID());
}
} else if (node.connectedTo.end.parent.keyName === 'LED') {
const led = (node.connectedTo.end.parent as LED);
if (!this.visitedLEDs.has(led.getID())) {
this.connectedLEDs.push(led);
this.visitedLEDs.add(led.getID());
}
}
if (node.connectedTo.start.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to start node
this.visitedNodesv2.clear();
return node.connectedTo.start;
} else if (node.connectedTo.end.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to end node
this.visitedNodesv2.clear();
return node.connectedTo.end;
} else if (node.connectedTo.start.parent.keyName === 'BreadBoard' && !this.visitedNodesv2.has(node.connectedTo.start.gid)) {
// TODO: Call recursive BreadBoard handler function if node is connected to Breadboard && visited nodes doesn't have node's gid
return this.getRecLEDBread(node, startedOn);
} else if (node.connectedTo.end.parent.keyName === 'BreadBoard' && !this.visitedNodesv2.has(node.connectedTo.end.gid)) {
// TODO: Call recursive BreadBoard handler function if node is connected to Breadboard && visited nodes doesn't have node's gid
return this.getRecLEDBread(node, startedOn);
} else if (node.connectedTo.end.parent.keyName === 'Battery9v' && window.scope.ArduinoUno.length === 0) {
// TODO: Return false if node's end is connected to 9V Battery
return false;
} else if (node.connectedTo.end.parent.keyName === 'CoinCell' && window.scope.ArduinoUno.length === 0) {
// TODO: Return false if node's end is connected to Coin Cell
return false;
} else if (node.connectedTo.end.parent.keyName === 'RelayModule') {
// TODO: Handle RelayModule
if (startedOn === 'POSITIVE') {
// If search was started on Positive node then return connected node of VCC in Relay
return this.getRecLED(node.connectedTo.end.parent.nodes[3], startedOn);
} else if (startedOn === 'NEGATIVE') {
// If search was started on Negative node then return connected node of GND in Relay
return this.getRecLED(node.connectedTo.end.parent.nodes[5], startedOn);
}
} else {
// TODO: If nothing matches
// IF/ELSE: Determine if start is to be used OR end for further recursion
if (node.connectedTo.end.gid !== node.gid) {
// Loops through all nodes in parent
for (const e in node.connectedTo.end.parent.nodes) {
// IF: gid is different && gid not in visited node
if (node.connectedTo.end.parent.nodes[e].gid !== node.connectedTo.end.gid
&& !this.visitedNodesv2.has(node.connectedTo.end.parent.nodes[e].gid) && node.connectedTo.end.parent.nodes[e].isConnected()) {
// add gid in visited nodes
this.visitedNodesv2.add(node.connectedTo.end.parent.nodes[e].gid);
// call back Arduino Recursive Fn
return this.getRecLED(node.connectedTo.end.parent.nodes[e], startedOn);
}
}
} else if (node.connectedTo.start.gid !== node.gid) {
// Loops through all nodes in parent
for (const e in node.connectedTo.start.parent.nodes) {
// IF: gid is different && gid not in visited node
if (node.connectedTo.start.parent.nodes[e].gid !== node.connectedTo.start.gid
&& !this.visitedNodesv2.has(node.connectedTo.start.parent.nodes[e].gid)
&& node.connectedTo.start.parent.nodes[e].isConnected()) {
// add gid in visited nodes
this.visitedNodesv2.add(node.connectedTo.start.parent.nodes[e].gid);
// call back Arduino Recursive Fn
return this.getRecLED(node.connectedTo.start.parent.nodes[e], startedOn);
}
}
}
}
} catch (e) {
console.warn(e);
return false;
}
}
/**
* Recursive Function to handle BreadBoard
* @param node Node which is to be checked for BreadBoard
*/
private getRecLEDBread(node: Point, startedOn: string) {
// IF/ELSE: Determine if start is to be used OR end for further recursion
if (node.connectedTo.end.gid !== node.gid) {
const bb = (node.connectedTo.end.parent as BreadBoard);
// loop through joined nodes of breadboard
for (const e in bb.joined) {
if (bb.joined[e].gid !== node.connectedTo.end.gid) {
// Run only if substring matches
if (bb.joined[e].label.substring(1, bb.joined[e].label.length)
=== node.connectedTo.end.label.substring(1, node.connectedTo.end.label.length)) {
const ascii = node.connectedTo.end.label.charCodeAt(0);
const currAscii = bb.joined[e].label.charCodeAt(0);
// add gid to VisitedNode
this.visitedNodesv2.add(bb.joined[e].gid);
// IF/ELSE: determine which part of breadboard is connected
if (ascii >= 97 && ascii <= 101) {
if (bb.joined[e].isConnected() && (currAscii >= 97 && currAscii <= 101)) {
return this.getRecLED(bb.joined[e], startedOn);
}
} else if (ascii >= 102 && ascii <= 106) {
if (bb.joined[e].isConnected() && (currAscii >= 102 && currAscii <= 106)) {
return this.getRecLED(bb.joined[e], startedOn);
}
} else {
if (bb.joined[e].isConnected() && (bb.joined[e].label === node.connectedTo.end.label)) {
return this.getRecLED(bb.joined[e], startedOn);
}
}
}
}
}
} else if (node.connectedTo.start.gid !== node.gid) {
const bb = (node.connectedTo.start.parent as BreadBoard);
// loop through joined nodes of breadboard
for (const e in bb.joined) {
if (bb.joined[e].gid !== node.connectedTo.start.gid) {
// Run only if substring matches
if (bb.joined[e].label.substring(1, bb.joined[e].label.length)
=== node.connectedTo.start.label.substring(1, node.connectedTo.start.label.length)) {
const ascii = node.connectedTo.start.label.charCodeAt(0);
const currAscii = bb.joined[e].label.charCodeAt(0);
// add gid to VisitedNode
this.visitedNodesv2.add(bb.joined[e].gid);
// IF/ELSE: determine which part of breadboard is connected
if (ascii >= 97 && ascii <= 101) {
if (bb.joined[e].isConnected() && (currAscii >= 97 && currAscii <= 101)) {
return this.getRecLED(bb.joined[e], startedOn);
}
} else if (ascii >= 102 && ascii <= 106) {
if (bb.joined[e].isConnected() && (currAscii >= 102 && currAscii <= 106)) {
return this.getRecLED(bb.joined[e], startedOn);
}
} else {
if (bb.joined[e].isConnected() && (bb.joined[e].label === node.connectedTo.end.label)) {
return this.getRecLED(bb.joined[e], startedOn);
}
}
}
}
}
}
}
}