feat: update PWM handling in SimulatorCanvas and enhance I2C Scanner example with SSD1306 OLED integration

pull/47/head
David Montero Crespo 2026-03-16 22:15:52 -03:00
parent d35ad2d2f7
commit c19a6cb848
4 changed files with 16 additions and 8 deletions

View File

@ -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.01.0
if (el) el.style.opacity = duty > 0 ? String(duty) : '';
});
unsubscribers.push(pwmUnsub);
};

View File

@ -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',

View File

@ -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');

View File

@ -497,10 +497,10 @@ export const useSimulatorStore = create<SimulatorState>((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 } : {}) };
});
},