Skip to content

Commit

Permalink
Logic change to compliment LED
Browse files Browse the repository at this point in the history
  • Loading branch information
rinon13 committed Mar 13, 2023
1 parent 442aef3 commit e57ba8b
Showing 1 changed file with 181 additions and 14 deletions.
195 changes: 181 additions & 14 deletions ArduinoFrontend/src/app/Libs/inputs/Potentiometer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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
Expand Down Expand Up @@ -40,6 +41,14 @@ export class Potentiometer extends CircuitElement {
* Check the connection type
*/
isRheostat: boolean = null;
/**
* Stores visited LEDs' ids
*/
visitedLEDs = new Set();
/**
* Stores connected LEDs
*/
connectedLEDs: LED[] = [];
/**
* Potentiometer constructor
* @param canvas Raphael Canvas (Paper)
Expand Down Expand Up @@ -147,7 +156,6 @@ export class Potentiometer extends CircuitElement {
ang = 268;
}
let to;
let outputVoltage = 0;
let calResistance = -1;
const minVoltage = 0;
const minLEDResistance = 100;
Expand All @@ -159,31 +167,21 @@ export class Potentiometer extends CircuitElement {
);
// intp = (ang / 268) * to;
calResistance = ((ang / 268) * resistanceValue);
if (calResistance < minLEDResistance || calResistance > maxLEDResitance) {
outputVoltage = 0;
}
else {
outputVoltage = to - (((calResistance - minLEDResistance) / (maxLEDResitance - minLEDResistance)) * (to - minVoltage)) + minVoltage;
}
this.sendResistance(calResistance);
} else if (this.arduinoEndTwo) {
to = Math.max(
this.nodes[1].value,
this.nodes[2].value
);
calResistance = resistanceValue - ((ang / 268) * resistanceValue);
if (calResistance < minLEDResistance || calResistance > maxLEDResitance) {
outputVoltage = 0;
}
else {
outputVoltage = to - (((calResistance - minLEDResistance) / (maxLEDResitance - minLEDResistance)) * (to - minVoltage)) + minVoltage;
}
this.sendResistance(calResistance);
}
if (to < 0) {
window['showToast']('Potentiometer Not Connected');
return;
}
this.elements[1].transform(`r${ang}`);
this.nodes[1].setValue(outputVoltage, this.nodes[1]);
this.nodes[1].setValue(to, this.nodes[1]);
}
/**
* Function provides component details
Expand Down Expand Up @@ -259,6 +257,12 @@ export class Potentiometer extends CircuitElement {
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');
Expand Down Expand Up @@ -306,6 +310,8 @@ export class Potentiometer extends CircuitElement {
this.setClickListener(null);
this.setDragListeners();
this.isRheostat = null;
this.visitedLEDs.clear();
this.connectedLEDs = [];
}
/**
* Checks if nodes of Potentiometer are connected either as
Expand All @@ -327,6 +333,17 @@ export class Potentiometer extends CircuitElement {
}
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) {
console.log(led);
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
Expand Down Expand Up @@ -466,4 +483,154 @@ export class Potentiometer extends CircuitElement {
}
}
}
/**
* 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);
}
}
}
}
}
}
}
}

0 comments on commit e57ba8b

Please sign in to comment.