From 29bb8af6f678d79d2275518e73afa71970bbe57e Mon Sep 17 00:00:00 2001 From: davidmonterocrespo24 Date: Fri, 15 May 2026 17:40:31 +0200 Subject: [PATCH] =?UTF-8?q?feat(sim):=20Phase=205=20=E2=80=94=20migrate=20?= =?UTF-8?q?led-bar-graph=20handler=20to=20PinResolver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- frontend/src/simulation/parts/BasicParts.ts | 25 +++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/frontend/src/simulation/parts/BasicParts.ts b/frontend/src/simulation/parts/BasicParts.ts index 348206d4..3307ba5b 100644 --- a/frontend/src/simulation/parts/BasicParts.ts +++ b/frontend/src/simulation/parts/BasicParts.ts @@ -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()); },