Skip to content

feat(gsoc2025): add pull resistor component #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: gsoc25-open-hardware-component-library
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions v0/src/assets/img/PullResistor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
{{ obj[name] }}
</textarea>
</p>
<DropdownSelect
v-if="value.type === 'dropdown'"
:dropdown-array="value.dropdownArray"
:property-name="value.func"
:property-value="obj[name]"
:property-input-name="value.name"
:property-input-id="value.name"
/>
</div>
</template>

Expand Down
10 changes: 7 additions & 3 deletions v0/src/simulator/src/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"Dlatch",
"TB_Input",
"TB_Output",
"ForceGate"
"ForceGate",
"PullResistor"
],
"annotationList": ["Text", "Rectangle", "Arrow", "ImageAnnotation"],
"inputList": [
Expand All @@ -78,7 +79,8 @@
"Input",
"Clock",
"Button",
"Counter"
"Counter",
"PullResistor"
],
"subCircuitInputList": [
"Random",
Expand All @@ -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" },
Expand Down
2 changes: 2 additions & 0 deletions v0/src/simulator/src/moduleSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -130,6 +131,7 @@ export default function setupModules() {
TB_Input,
TB_Output,
ForceGate,
PullResistor
}
Object.assign(modules, moduleSet)
}
128 changes: 128 additions & 0 deletions v0/src/simulator/src/modules/PullResistor.js
Original file line number Diff line number Diff line change
@@ -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/<to be updated>'
PullResistor.prototype.objectType = 'PullResistor'
PullResistor.prototype.objectType = 'PullResistor'