From c19a6cb848f7b0ef88504716de723a49fb7b05b5 Mon Sep 17 00:00:00 2001 From: David Montero Crespo Date: Mon, 16 Mar 2026 22:15:52 -0300 Subject: [PATCH] feat: update PWM handling in SimulatorCanvas and enhance I2C Scanner example with SSD1306 OLED integration --- frontend/src/components/simulator/SimulatorCanvas.tsx | 6 ++++-- frontend/src/data/examples.ts | 10 ++++++++-- frontend/src/simulation/AVRSimulator.ts | 4 ++-- frontend/src/store/useSimulatorStore.ts | 4 ++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/simulator/SimulatorCanvas.tsx b/frontend/src/components/simulator/SimulatorCanvas.tsx index 2111b1b..4f91c8b 100644 --- a/frontend/src/components/simulator/SimulatorCanvas.tsx +++ b/frontend/src/components/simulator/SimulatorCanvas.tsx @@ -463,10 +463,12 @@ export const SimulatorCanvas = () => { ); unsubscribers.push(unsubscribe); - // PWM subscription: update LED opacity when the pin receives a LEDC duty cycle + // PWM subscription: update LED opacity when the pin receives a LEDC duty cycle. + // duty=0 means no PWM / analogWrite(0) — clear the inline style so the + // component keeps its default visibility instead of becoming invisible. const pwmUnsub = pinManager.onPwmChange(pin, (_p, duty) => { const el = document.getElementById(component.id); - if (el) el.style.opacity = String(duty); // duty is 0.0–1.0 + if (el) el.style.opacity = duty > 0 ? String(duty) : ''; }); unsubscribers.push(pwmUnsub); }; diff --git a/frontend/src/data/examples.ts b/frontend/src/data/examples.ts index 0c205c2..aae1778 100644 --- a/frontend/src/data/examples.ts +++ b/frontend/src/data/examples.ts @@ -932,7 +932,7 @@ void loop() { { id: 'i2c-scanner', title: 'I2C Scanner (TWI)', - description: 'Scans the I2C bus and reports all devices found. Tests TWI protocol. Virtual devices at 0x48, 0x50, 0x68 should be detected.', + description: 'Scans the I2C bus and reports all devices found. SSD1306 OLED (0x3C) is wired on canvas; virtual devices at 0x48, 0x50, 0x68 also respond.', category: 'communication', difficulty: 'intermediate', code: `// I2C Bus Scanner — TWI Protocol Test @@ -999,8 +999,14 @@ void loop() { `, components: [ { type: 'wokwi-arduino-uno', id: 'arduino-uno', x: 100, y: 100, properties: {} }, + { type: 'wokwi-ssd1306', id: 'ssd1306-1', x: 420, y: 120, properties: {} }, + ], + wires: [ + { id: 'wire-sda', start: { componentId: 'arduino-uno', pinName: 'A4' }, end: { componentId: 'ssd1306-1', pinName: 'DATA' }, color: '#2196f3' }, + { id: 'wire-scl', start: { componentId: 'arduino-uno', pinName: 'A5' }, end: { componentId: 'ssd1306-1', pinName: 'CLK' }, color: '#ff9800' }, + { id: 'wire-gnd', start: { componentId: 'arduino-uno', pinName: 'GND.1' }, end: { componentId: 'ssd1306-1', pinName: 'GND' }, color: '#000000' }, + { id: 'wire-vcc', start: { componentId: 'arduino-uno', pinName: '5V' }, end: { componentId: 'ssd1306-1', pinName: 'VIN' }, color: '#ff0000' }, ], - wires: [], }, { id: 'i2c-rtc-read', diff --git a/frontend/src/simulation/AVRSimulator.ts b/frontend/src/simulation/AVRSimulator.ts index a0c8343..af7e82d 100644 --- a/frontend/src/simulation/AVRSimulator.ts +++ b/frontend/src/simulation/AVRSimulator.ts @@ -255,7 +255,7 @@ export class AVRSimulator { this.lastPortBValue = 0; this.lastPortCValue = 0; this.lastPortDValue = 0; - this.lastOcrValues = new Array(this.pwmPins.length).fill(-1); + this.lastOcrValues = new Array(this.pwmPins.length).fill(0); this.setupPinHooks(); @@ -503,7 +503,7 @@ export class AVRSimulator { this.lastPortBValue = 0; this.lastPortCValue = 0; this.lastPortDValue = 0; - this.lastOcrValues = new Array(this.pwmPins.length).fill(-1); + this.lastOcrValues = new Array(this.pwmPins.length).fill(0); this.setupPinHooks(); console.log('AVR CPU reset complete'); diff --git a/frontend/src/store/useSimulatorStore.ts b/frontend/src/store/useSimulatorStore.ts index 57bef45..d780647 100644 --- a/frontend/src/store/useSimulatorStore.ts +++ b/frontend/src/store/useSimulatorStore.ts @@ -497,10 +497,10 @@ export const useSimulatorStore = create((set, get) => { set((s) => { const boards = s.boards.map((b) => - b.id === boardId ? { ...b, running: true } : b + b.id === boardId ? { ...b, running: true, serialMonitorOpen: true } : b ); const isActive = s.activeBoardId === boardId; - return { boards, ...(isActive ? { running: true } : {}) }; + return { boards, ...(isActive ? { running: true, serialMonitorOpen: true } : {}) }; }); },