Skip to content

Commit

Permalink
Merge pull request #4 from frg-fossee/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Kaustuv942 authored Jul 20, 2021
2 parents 61f0961 + 1c4e3a9 commit 74a896f
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 46 deletions.
152 changes: 151 additions & 1 deletion ArduinoFrontend/src/app/Libs/General.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ export class BreadBoard extends CircuitElement {
*/
static PROXIMITY_DISTANCE = 20;

/**
* Set to keep track of visited nodes
*/
static visitedNodesv2 = new Set();

/**
* Nodes that are connected
*/
Expand Down Expand Up @@ -325,6 +330,147 @@ export class BreadBoard extends CircuitElement {
this.subscribeToDragStop({ id: this.id, fn: this.onOtherComponentDragStop.bind(this) });
}

/**
* Returns node connected to arduino
* @param node node to start search on
* @param startedOn label of node search started on
* @returns Arduino connected Node
*/
static getRecArduinov2(node: Point, startedOn: string) {
try {
if (node.connectedTo.start.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to start node
return node.connectedTo.start;
} else if (node.connectedTo.end.parent.keyName === 'ArduinoUno') {
// TODO: Return if arduino is connected to end node
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
*/
static 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);
}
}
}
}
}
}

}

/**
* Subscribes to drag listener of the workspace
* @param fn listener functino
Expand Down Expand Up @@ -661,7 +807,10 @@ export class BreadBoard extends CircuitElement {
for (const node of this.nodes) {
// Add a Node value change listener
node.addValueListener((value, calledBy, parent) => {
if (calledBy.y === parent.y) {
const labelCalledBy = calledBy.label;
const labelParent = parent.label;
if (calledBy.y === parent.y
&& (labelCalledBy.charCodeAt(0) !== labelParent.charCodeAt(0) || labelCalledBy === labelParent)) {
return;
}
if (node.label === '+' || node.label === '-') {
Expand Down Expand Up @@ -696,4 +845,5 @@ export class BreadBoard extends CircuitElement {
*/
closeSimulation(): void {
}

}
44 changes: 37 additions & 7 deletions ArduinoFrontend/src/app/Libs/inputs/Potentiometer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CircuitElement } from '../CircuitElement';
import { BreadBoard } from '../General';
import { Vector } from './Collision';

/**
Expand All @@ -22,6 +23,15 @@ export class Potentiometer extends CircuitElement {
* Selected potentiometer type.
*/
selectedIndex: number;
/**
* Arduino Pin connected to 0 of potentiometer
*/
arduinoEndZero: any;
/**
* Arduino Pin connected to 2 of potentiometer
*/
arduinoEndTwo: any;

/**
* Potentiometer constructor
* @param canvas Raphael Canvas (Paper)
Expand Down Expand Up @@ -76,16 +86,26 @@ export class Potentiometer extends CircuitElement {
if (ang > 268) {
ang = 268;
}
let to;
let intp = 0;
// console.log(ang / 268);
const to = Math.max(
this.nodes[0].value,
this.nodes[1].value
);
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;
}
const intp = (ang / 268) * to;
this.elements[1].transform(`r${ang}`);

this.nodes[1].setValue(intp, this.nodes[1]);
Expand Down Expand Up @@ -152,8 +172,18 @@ export class Potentiometer extends CircuitElement {
(ev: MouseEvent) => {
this.rotateDial(center, ev.clientX, ev.clientY);
});

this.nodes[1].setValue(0, this.nodes[1]);
// Get Arduino Connected ends for terminal 1 & terminal 2
this.arduinoEndZero = BreadBoard.getRecArduinov2(this.nodes[0], 'Terminal 1');
this.arduinoEndTwo = BreadBoard.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
Expand Down
29 changes: 19 additions & 10 deletions ArduinoFrontend/src/app/Libs/outputs/Led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ export class LED extends CircuitElement {
return;
}
this.prev = val;
// Run if PWM is not attached
// TODO: Run if PWM is not attached
if (this.nodes[0].connectedTo && this.nodes[1].connectedTo && !this.pwmAttached && this.allNodesConnected) {
if (val >= 5) {
this.anim();
} else if (val > 0 && val < 5) {
if (val < 0.1) {
this.fillColor('none');
} else {
this.glowWithAlpha(val);
}
} else {
this.fillColor('none');
}
Expand All @@ -115,22 +121,25 @@ export class LED extends CircuitElement {
}
} else if (this.nodes[0].connectedTo && this.nodes[1].connectedTo && this.pwmAttached && this.allNodesConnected) {
// TODO: Run if PWM is attached
/**
* create color and add alpha to color
*/
const color = `r(0.5, 0.5)${LED.glowColors[this.selectedIndex]}`;
const split = color.split('-');
let genColor = 'none';
const alpha = (this.voltage / 5) * 9;
genColor = `${split[0].substr(0, split[0].length - 2)}${alpha})-${split[1]}`;
this.elements[3].attr({ fill: genColor });
this.glowWithAlpha(this.voltage);

} else {
// TODO: Show Toast
this.handleConnectionError();
window.showToast('LED is not Connected properly');
}
}
/**
* create color and add alpha to color
*/
glowWithAlpha(value: number) {
const color = `r(0.5, 0.5)${LED.glowColors[this.selectedIndex]}`;
const split = color.split('-');
let genColor = 'none';
const alpha = (value / 5) * 9;
genColor = `${split[0].substr(0, split[0].length - 2)}${alpha})-${split[1]}`;
this.elements[3].attr({ fill: genColor });
}
/**
* Handles connection error
*/
Expand Down
26 changes: 13 additions & 13 deletions ArduinoFrontend/src/assets/jsons/Potentiometer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"className": "Potentiometer",
"pins": [
{
"x": 28,
"y": 114,
"x": 16.5,
"y": 75.025,
"name": "Terminal 1"
},
{
"x": 51,
"y": 114,
"x": 31.2,
"y": 75.025,
"name": "WIPER"
},
{
"x": 74,
"y": 114,
"x": 45.4,
"y": 75.025,
"name": "Terminal 2"
}
],
Expand All @@ -23,18 +23,18 @@
{
"type": "image",
"url": "assets/images/components/Potentiometer.svg",
"width": 109.3,
"height": 122.975,
"width": 71.1,
"height": 80,
"x": 0,
"y": 0
},
{
"type": "image",
"url": "assets/images/components/PotentiometerAbove.svg",
"width": 82.6,
"height": 82.6,
"x": 13.3,
"y": 12.5
"width": 55.6,
"height": 55.6,
"x": 7.3,
"y": 7.5
}
],
"simulation": {},
Expand Down Expand Up @@ -83,4 +83,4 @@
"Rotational Life": " 2000K cycles"
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 74a896f

Please sign in to comment.