diff --git a/v0/src/assets/img/PullResistor.svg b/v0/src/assets/img/PullResistor.svg new file mode 100644 index 00000000..9bf5b7f3 --- /dev/null +++ b/v0/src/assets/img/PullResistor.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/v0/src/components/Panels/PropertiesPanel/ModuleProperty/ElementProperty/ElementProperty.vue b/v0/src/components/Panels/PropertiesPanel/ModuleProperty/ElementProperty/ElementProperty.vue index fec2c571..62a6531a 100644 --- a/v0/src/components/Panels/PropertiesPanel/ModuleProperty/ElementProperty/ElementProperty.vue +++ b/v0/src/components/Panels/PropertiesPanel/ModuleProperty/ElementProperty/ElementProperty.vue @@ -107,6 +107,14 @@ {{ obj[name] }}

+ diff --git a/v0/src/simulator/src/metadata.json b/v0/src/simulator/src/metadata.json index 8ed4f225..838ad788 100644 --- a/v0/src/simulator/src/metadata.json +++ b/v0/src/simulator/src/metadata.json @@ -60,7 +60,8 @@ "Dlatch", "TB_Input", "TB_Output", - "ForceGate" + "ForceGate", + "PullResistor" ], "annotationList": ["Text", "Rectangle", "Arrow", "ImageAnnotation"], "inputList": [ @@ -78,7 +79,8 @@ "Input", "Clock", "Button", - "Counter" + "Counter", + "PullResistor" ], "subCircuitInputList": [ "Random", @@ -94,10 +96,12 @@ "ConstantVal", "Clock", "Button", - "Counter" + "Counter", + "PullResistor" ], "elementHierarchy": { "Input": [ + { "name": "PullResistor", "label": "Pull Resistor" }, { "name": "Input", "label": "Input" }, { "name": "Button", "label": "Button" }, { "name": "Power", "label": "Power" }, diff --git a/v0/src/simulator/src/moduleSetup.js b/v0/src/simulator/src/moduleSetup.js index 5002bc69..eae48dfa 100644 --- a/v0/src/simulator/src/moduleSetup.js +++ b/v0/src/simulator/src/moduleSetup.js @@ -63,6 +63,7 @@ import verilogPower from './modules/verilogPower' import verilogShiftLeft from './modules/verilogShiftLeft' import verilogShiftRight from './modules/verilogShiftRight' import verilogRAM from './sequential/verilogRAM' +import PullResistor from './modules/PullResistor' export default function setupModules() { var moduleSet = { @@ -130,6 +131,7 @@ export default function setupModules() { TB_Input, TB_Output, ForceGate, + PullResistor } Object.assign(modules, moduleSet) } diff --git a/v0/src/simulator/src/modules/PullResistor.js b/v0/src/simulator/src/modules/PullResistor.js new file mode 100644 index 00000000..9a3a2876 --- /dev/null +++ b/v0/src/simulator/src/modules/PullResistor.js @@ -0,0 +1,128 @@ +import { correctWidth, lineTo, moveTo } from '../canvasApi' +import CircuitElement from '../circuitElement' +import Node, { findNode } from '../node' +import simulationArea from '../simulationArea' +import { colors } from '../themer/themer' + + +/** + * @class + * PullResistor + * @extends CircuitElement + * @param {number} x - x coordinate of the element + * @param {number} y - y coordinate of the element + * @param {Scope=} scope - Circuit on which the element is drawn + * @param {string=} dir - Direction of the element (default: RIGHT) + * @param {string=} pullDirection - Pull direction: "Up" or "Down" (default: "Down") + * @category modules + */ +export default class PullResistor extends CircuitElement { + constructor(x, y, scope = globalScope, dir = 'RIGHT', pullDirection = "Down") { + super(x, y, scope, dir, 1) + + this.rectangleObject = false; + this.fixedBitWidth = true; + this.setDimensions(10, 30) + this.pullDirection = pullDirection ?? "Down"; + this.inp = new Node(0, -10, 0, this, 1, 'inp') + } + + /** + * Creates JSON data for saving the component state + * @returns {Object} Save data including constructor parameters and nodes + */ + customSave() { + const data = { + constructorParamaters: [this.direction, this.bitWidth], + nodes: { + inp: findNode(this.inp) + }, + } + return data + } + + isResolvable() { + return true + } + + /** + * Resolve the input if it's floating (undefined) by assigning pull value + */ + resolve() { + if (this.inp.value == undefined) { + this.inp.value = this.pullDirection == "Up" ? 1 : 0; + simulationArea.simulationQueue.add(this.inp) + } + } + + /** + * Draw the zig-zag resistor and connection lines + */ + customDraw() { + const ctx = simulationArea.context; + ctx.fillStyle = colors['fill'] + ctx.strokeStyle = colors['stroke'] + ctx.beginPath() + var xx = this.x + var yy = this.y + ctx.lineWidth = correctWidth(3) + + // Start line from top (Vcc or Input)// Top wire + moveTo(ctx, 0, 0, xx, yy, this.direction) + lineTo(ctx, 0, 10, xx, yy, this.direction); + + // Zigzag shape for resistor + const segmentLength = 5; + const amplitude = 5; + let currentY = 10 + let toggle = true; + + for (let i = 0; i < 9; i++) { + const dx = toggle ? amplitude : -amplitude; + currentY += segmentLength; + lineTo(ctx, dx, currentY, xx, yy, this.direction); + toggle = !toggle; + } + + + ctx.stroke(); + } + + /** + * Changes the pull direction property and redraws the component + * @param {string} pullDirection - New pull direction + */ + changePullDirection(pullDirection) { + if (pullDirection !== undefined && this.pullDirection !== pullDirection) { + this.pullDirection = pullDirection; + var obj = new PullResistor(this.x, this.y, this.scope, this.dir, this.pullDirection) + this.delete() + simulationArea.lastSelected = obj + return obj + } + + } + +} + +/** + * Mutable properties shown in the UI (for dropdown selection) + * @memberof PullResistor + * @type {Object} + * @category modules + */ +PullResistor.prototype.mutableProperties = { + pullDirection: { + name: 'Pull Direction: ', + type: 'dropdown', + func: 'changePullDirection', + dropdownArray: ['Down', 'Up'] + } +} + +PullResistor.prototype.tooltipText = + 'PullResistor' +PullResistor.prototype.helplink = + 'https://docs.circuitverse.org/#/chapter4/' +PullResistor.prototype.objectType = 'PullResistor' +PullResistor.prototype.objectType = 'PullResistor'