-
Notifications
You must be signed in to change notification settings - Fork 176
feat: Add Clock Divider Component (#587) #680
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
base: main
Are you sure you want to change the base?
feat: Add Clock Divider Component (#587) #680
Conversation
- Create ClockDivider module for dividing clock frequencies by a configurable factor - Support divide factors: 2, 4, 8, 16, and custom values - Register component in module system for simulator availability - Add Clock Divider to Sequential Elements menu in UI - Component displays division factor (N) on canvas for visual clarity - Uses internal counter-based logic for clock division - Output frequency = Input frequency / divideFactor - Simplifies timing visualization for educational projects
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughAdds a new ClockDivider circuit element: metadata entries to expose it in the UI, module registration, and a new ClockDivider class implementing divideFactor, edge-detection counter logic, serialization, and drawing. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/simulator/src/modules/ClockDivider.js (1)
89-107: Minor text rendering nit: rely onfillText’s fontSize parameter
fillTextalready setsctx.fontbased on afontSizeargument, so the manualctx.font = '14px Raleway'is overridden and effectively unused. You can simplify and ensure consistent sizing by passing the desired font size directly:- ctx.font = '14px Raleway' ctx.fillStyle = colors['input_text'] ctx.textAlign = 'center' - fillText(ctx, '÷' + this.divideFactor, xx, yy + 5) + fillText(ctx, '÷' + this.divideFactor, xx, yy + 5, 14)This keeps the API usage consistent with other drawing helpers.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/simulator/src/metadata.ts(4 hunks)src/simulator/src/moduleSetup.js(2 hunks)src/simulator/src/modules/ClockDivider.js(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/simulator/src/modules/ClockDivider.js (3)
src/simulator/src/node.js (2)
Node(150-1034)findNode(87-89)src/simulator/src/themer/themer.ts (1)
colors(47-47)src/simulator/src/canvasApi.js (3)
correctWidth(401-403)rect2(370-386)fillText(513-522)
🔇 Additional comments (6)
src/simulator/src/moduleSetup.js (1)
10-10: ClockDivider import and registration look consistentThe new import path and moduleSet registration are aligned with existing patterns and metadata names; this will correctly expose ClockDivider to the simulator.
Also applies to: 127-127
src/simulator/src/metadata.ts (1)
41-41: Metadata wiring for ClockDivider is coherentClockDivider is added consistently to
circuitElementList,inputList,subCircuitInputList, and the "Sequential Elements" hierarchy, matching the module name and desired palette placement.Also applies to: 89-89, 106-106, 164-164
src/simulator/src/modules/ClockDivider.js (4)
17-37: Constructor and node setup follow existing CircuitElement patternsThe constructor wiring (direction RIGHT, bitWidth 1, rectangle geometry, input
clockand outputoutput) andmutableProperties.divideFactorconfiguration look consistent with other modules and should integrate cleanly with the property panel.
47-61: customSave shape matches standard module persistencePersisting only
divideFactorinconstructorParamatersand serializing theclock/outputnodes viafindNodealigns with how other stateful elements are saved and reconstructed. No issues here.
63-83: Edge detection and division logic are sound for the intended use
- Rising‑edge detection via
this.clock.value !== this.prevClockState && this.clock.value === 1avoids double‑counting during high/low phases.- Counter wrap at
this.divideFactorand the subsequent high/low computation give a divided clock with period scaled bydivideFactor. Even factors yield a clean 50% duty cycle; odd factors produce a slightly asymmetric duty cycle but still the correct period.Given the educational/visual focus, this is a reasonable and efficient implementation.
110-112: Prototype metadata is well‑formed
tooltipText,helplink, andobjectTypeare set as expected. Once dedicated docs exist, you may want to pointhelplinkto the specific Clock Divider section, but the current placeholder is fine for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/simulator/src/modules/ClockDivider.js(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/simulator/src/modules/ClockDivider.js (3)
src/simulator/src/node.js (2)
Node(150-1034)findNode(87-89)src/simulator/src/themer/themer.ts (1)
colors(47-47)src/simulator/src/canvasApi.js (3)
correctWidth(401-403)rect2(370-386)fillText(513-522)
🔇 Additional comments (2)
src/simulator/src/modules/ClockDivider.js (2)
72-88: Clock division logic looks correct.The rising-edge detection, counter wrap, and 50% duty-cycle output calculation correctly implement frequency division. Output toggles at divideFactor/2 boundaries producing the expected f/N waveform.
115-117: Metadata looks good; consider updatinghelplinkwhen documentation is available.The tooltip and objectType are appropriate. The placeholder help link is fine for now but should eventually point to component-specific documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/simulator/src/modules/ClockDivider.js (1)
108-111: Redundant font assignment on line 108.Since
fillTexton line 111 receives the font size (14) as its fourth argument and setsctx.fontinternally, line 108 has no effect. Consider removing it for clarity.- ctx.font = '14px Raleway' ctx.fillStyle = colors['input_text'] ctx.textAlign = 'center' fillText(ctx, '÷' + this.divideFactor, xx, yy + 5, 14)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/simulator/src/modules/ClockDivider.js(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-24T05:25:39.773Z
Learnt from: 092vk
Repo: CircuitVerse/cv-frontend-vue PR: 644
File: src/simulator/src/utils.ts:54-61
Timestamp: 2025-08-24T05:25:39.773Z
Learning: In CircuitVerse simulator, clearing the clock interval (simulationArea.ClockInterval) during pause can cause unexpected results. The safer approach is to let the clock continue ticking and use flag checks in clockTick() to prevent simulation updates.
Applied to files:
src/simulator/src/modules/ClockDivider.js
🔇 Additional comments (6)
src/simulator/src/modules/ClockDivider.js (6)
1-5: Imports are correctly utilized.All imported modules and functions are used appropriately throughout the class.
17-37: Constructor implementation looks good.The initialization properly sets up dimensions, nodes, and mutable properties. The divide factor bounds (min: 2, max: 100) are correctly configured per past review feedback.
39-50: Setter implementation is robust.The clamping logic (2–100), NaN check, and counter reset on change are all correctly implemented.
57-66: Serialization format is correct.The spelling fix (
constructorParameters) has been applied, and the save structure follows the expected pattern.
72-88: Core clock division logic is correct.The rising-edge detection, counter wrap, and 50% duty-cycle output computation are all properly implemented. The output is only pushed to the simulation queue when it changes, which is efficient.
Minor note: The modulo on line 82 is technically redundant since
counteris already bounded by lines 78–80, but it doesn't affect correctness.
115-117: Prototype metadata is properly defined.The
objectTypematches the class name, and the tooltip is descriptive. The help link is a placeholder pointing to the docs root—consider updating it to a specific page (e.g.,/sequential-elements/clock-divider) once documentation is available.

Hello!
I added a new Clock Divider component to help with issue #587.
Why I did this: Making slow clocks for projects was hard because users had to connect many flip-flops by hand. This new component makes it much easier and faster to use.
What I added:
A new
Clock Divider
component in the "Sequential Elements" menu.
You can change the speed (divide by 2, 4, 8, etc.) in the settings.
It shows the number (like "÷2") on the component so you can see the speed.
Testing: I checked that it shows up in the menu, connects to wires, and correctly slows down the clock speed when I change the settings.
Closes #587
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.