feat(sim): Phase 5 — migrate led-bar-graph handler to PinResolver

Same backwards-compatible pattern as LED and 7-segment migrations.
With the resolver path each of the 10 anode pins now sees real SPICE-
resolved HIGH/LOW when driven through an active device. Legacy
pinManager.onPinChange path is kept as the fallback.

Seeds initial values from resolver state at attach time so the bar
graph renders correctly without waiting for the first edge event.

Phase 5 progress: 3 of ~12 handlers migrated (LED, 7-segment,
led-bar-graph). Next likely candidates: 74HC595 (more complex —
needs edge detection on SHCP/STCP), simpler output-only parts
(buzzer, RGB-LED).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
davidmonterocrespo24 2026-05-15 17:40:31 +02:00
parent 73478b7433
commit 29bb8af6f6
1 changed files with 21 additions and 4 deletions

View File

@ -293,17 +293,33 @@ PartSimulationRegistry.register('led', {
* Wokwi pin names: A1-A10
*/
PartSimulationRegistry.register('led-bar-graph', {
attachEvents: (element, avrSimulator, getArduinoPinHelper) => {
attachEvents: (element, avrSimulator, getArduinoPinHelper, _componentId, getPinResolver) => {
const pinManager = (avrSimulator as any).pinManager;
if (!pinManager) return () => {};
// Phase 5 migration: prefer the resolver so each anode pin works
// through SPICE-resolved thresholds when fed from an active device.
const useResolver = typeof getPinResolver === 'function';
const values = new Array(10).fill(0);
const unsubscribers: (() => void)[] = [];
for (let i = 1; i <= 10; i++) {
const pin = getArduinoPinHelper(`A${i}`);
if (pin !== null) {
const idx = i - 1;
const idx = i - 1;
const pinName = `A${i}`;
if (useResolver) {
const resolver = getPinResolver!(pinName);
if (!resolver) continue;
values[idx] = resolver.getCurrentState() === 'HIGH' ? 1 : 0;
unsubscribers.push(
resolver.onChange((state) => {
values[idx] = state === 'HIGH' ? 1 : 0;
(element as any).values = [...values];
}),
);
} else {
const pin = getArduinoPinHelper(pinName);
if (pin === null) continue;
unsubscribers.push(
pinManager.onPinChange(pin, (_p: number, state: boolean) => {
values[idx] = state ? 1 : 0;
@ -312,6 +328,7 @@ PartSimulationRegistry.register('led-bar-graph', {
);
}
}
(element as any).values = [...values];
return () => unsubscribers.forEach((u) => u());
},