Visual Overview of CircuitVerse Simulator Classes and Methods
The flow chart below illustrates the relationships and workflows among the key classes and methods within the CircuitVerse simulator.
1. CanvasGradient
The CanvasGradient class is vital for visually representing circuit components. CircuitVerse utilizes canvas2svg.js to convert drawing commands into Scalable Vector Graphics (SVG) format. This conversion results in high-quality graphical outputs, making interactions with circuit diagrams more engaging.
Follow the link for Source Code:- canvas2svg.js

Method: addColorStop()
Within the CanvasGradient class, the addColorStop() method plays a critical role in defining color transitions within gradients.
Source Code:-
/**
* Adds a color stop to the gradient root
*/
CanvasGradient.prototype.addColorStop = function (offset, color) {
var stop = this.__ctx.__createElement("stop"), regex, matches;
stop.setAttribute("offset", offset);
if (color.indexOf("rgba") !== -1) {
//separate alpha value, since webkit can't handle it
regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi;
matches = regex.exec(color);
stop.setAttribute("stop-color", format("rgb({r},{g},{b})", {r:matches[1], g:matches[2], b:matches[3]}));
stop.setAttribute("stop-opacity", matches[4]);
} else {
stop.setAttribute("stop-color", color);
}
this.__root.appendChild(stop);
};
CanvasPattern = function (pattern, ctx) {
this.__root = pattern;
this.__ctx = ctx;
};
Parameters:
offset: A value between 0 and 1 representing the position of the color stop within the gradient.
color: A string denoting the color for the stop, which can be specified in standard CSS formats.
Purpose:
This method allows users to create seamless color transitions, enhancing the overall look of circuit diagrams. For example, students can create a gradient that transitions from green to yellow, making their designs more visually appealing.
2. ContentionPendingData
The ContentionPendingData class acts as a data structure for storing pending data transmissions within the circuit. It addresses data management issues in simulated environments, ensuring a more accurate representation of circuit behavior. This is particularly useful when simulating conditions like bus contention, which is crucial in more complex circuits.
Follow the link for Source Code:- contention.js

3. TestbenchData
The TestbenchData class is essential for setting up various test scenarios within the simulator. It manages parameters such as data input, group options, and case configurations, which aid in creating thorough test benches.
Follow the link for source code:- testbench.js
Methods:
The TestbenchData class includes methods (specific details omitted here) that allow users to set and execute various test cases. This functionality is invaluable for students since it lets them experiment with different conditions, observe circuit responses, and refine their designs based on extensive testin
1.

Source Code:-
/**
* Validate and go to the next case
*/
caseNext() {
const caseCount = this.testData.groups[this.currentGroup].inputs[0].values.length;
if (this.currentCase >= caseCount - 1) return this.groupNext();
this.currentCase++;
return true;
}
2.
Source Code:-
/**
* Validate and go to the previous case
*/
casePrev() {
if (this.currentCase <= 0) {
if (!this.groupPrev()) return false;
const caseCount = this.testData.groups[this.currentGroup].inputs[0].values.length;
this.currentCase = caseCount - 1;
return true;
}
this.currentCase--;
return true;
}
3.
Source Code:-
/**
* Finds and switches to the first non empty group to start the test from
*/
goToFirstValidGroup() {
const newCase = new TestbenchData(this.testData, 0, 0);
const caseCount = newCase.testData.groups[this.currentGroup].inputs[0].values.length;
// If the first group is not empty, do nothing
if (caseCount > 0) return true;
// Otherwise go next until non empty group
const validExists = newCase.groupNext();
// If all groups empty return false
if (!validExists) return false;
// else set case to the non empty group
this.currentGroup = newCase.currentGroup;
this.currentCase = newCase.currentCase;
return true;
}
4.
Source Code:-
/**
* Validate and go to the next group.
* Skips over empty groups
*/
groupNext() {
const newCase = new TestbenchData(this.testData, this.currentGroup, 0);
const groupCount = newCase.testData.groups.length;
let caseCount = newCase.testData.groups[newCase.currentGroup].inputs[0].values.length;
while (caseCount === 0 || this.currentGroup === newCase.currentGroup) {
newCase.currentGroup++;
if (newCase.currentGroup >= groupCount) return false;
caseCount = newCase.testData.groups[newCase.currentGroup].inputs[0].values.length;
}
this.currentGroup = newCase.currentGroup;
this.currentCase = newCase.currentCase;
return true;
}
5.
Source Code:-
/**
* Validate and go to the previous group.
* Skips over empty groups
*/
groupPrev() {
const newCase = new TestbenchData(this.testData, this.currentGroup, 0);
const groupCount = newCase.testData.groups.length;
let caseCount = newCase.testData.groups[newCase.currentGroup].inputs[0].values.length;
while (caseCount === 0 || this.currentGroup === newCase.currentGroup) {
newCase.currentGroup--;
if (newCase.currentGroup < 0) return false;
caseCount = newCase.testData.groups[newCase.currentGroup].inputs[0].values.length;
}
this.currentGroup = newCase.currentGroup;
this.currentCase = newCase.currentCase;
return true;
}
6.
Source Code:-
/**
* Checks whether given case-group pair exists in the test
*/
isCaseValid() {
if (this.currentGroup >= this.data.groups.length || this.currentGroup < 0) return false;
const caseCount = this.testData.groups[this.currentGroup].inputs[0].values.length;
if (this.currentCase >= caseCount || this.currentCase < 0) return false;
return true;
}
Comments
Post a Comment