velxio/docs
davidmonterocrespo24 0f05544ca8 feat(esp32): SignalRouter — model the GPIO Matrix as first-class
Replaces the per-peripheral ad-hoc `_ledc_gpio_map` cache with a
proper signal-routing abstraction that mirrors the ESP32 SoC's
IO_MUX + GPIO Matrix exactly. Same idea as real silicon: signal
sources (LEDC channels, RMT, MCPWM, ...) → 40-entry routing table
→ GPIO pins.

Motivation (from user bug report in
velxio.dev/project/5218f9e3-136d-43b3-bba1-6cebde21e1a4): two
ESP32 servos on a solar-tracker visibly oscillated between two
positions instead of moving smoothly when the user changed LDR
sliders. Commit 77bf897 patched it (per-channel gpio memo +
broadcast guard) but the user requested a proper hardware-fidel
architecture, not patches.

Backend:
* `app/services/signal_router.py` — SignalRouter class. Forward
  index (gpio → signal_id) + reverse index (signal_id → set of
  gpios). `replace_snapshot()` returns the diff for the polling-
  fallback path; future C plugin hook becomes a push without
  touching this code.
* `app/services/esp32_signals.py` — Signal id constants from
  ESP32 TRM (LEDC HS 72-79, LS 80-87) + `ledc_signal_for_channel()`
  helper.
* `app/services/esp32_worker.py` — `_ledc_gpio_map` is gone;
  `_refresh_ledc_gpio_map` replaced by `_refresh_signal_routing`
  which emits `gpio_routing {gpio, signal_id}` events on diff.
  The 0x5000 LEDC callback and the LEDC poll thread now emit
  `ledc_duty {channel, duty_pct}` (canonical, no gpio) alongside
  the legacy `ledc_update {channel, duty, gpio}` for back-compat
  during rollout.

Frontend:
* `simulation/SignalRouter.ts` — 1-to-1 TS mirror of the Python
  class. Same forward + reverse index; same `pinsForSignal` /
  `updateRouting` / `clearRouting` API.
* `simulation/esp32-signals.ts` — Signal id constants, mirror
  of the Python module.
* `simulation/Esp32Bridge.ts` — new `onLedcDuty`, `onGpioRouting`,
  `onGpioRoutingClear` callbacks; handlers for the new event types.
* `store/useSimulatorStore.ts` — `makeLedcDutyHandler` looks up
  pins via `router.pinsForSignal(ledcSignalForChannel(channel))`
  and dispatches per pin. `makeGpioRoutingHandler` /
  `makeGpioRoutingClearHandler` keep the mirror in sync. Per-board
  `signalRouterMap` parallels `pinManagerMap` in lifecycle.
  `makeLedcUpdateHandler` (and its memo workaround from 77bf897)
  stays wired for back-compat during rollout; removed in a
  follow-up commit once prod is verified stable on the new path.

Tests:
* `test/backend/unit/test_signal_router.py` (20 tests) covers
  update/clear semantics, idempotency, multi-pin routing,
  snapshot diff, channel↔signal-id helpers, and the multi-servo
  regression scenario.
* `frontend/src/__tests__/SignalRouter.test.ts` (17 tests) is the
  mirror — same scenarios on the TS side.
* `frontend/src/__tests__/esp32-multi-servo-gpio-matrix.test.ts`
  (6 tests) drives the end-to-end SignalRouter handler pipeline,
  asserts that two servos on GPIO 13/12 via LEDC channels 0/1
  move independently (no mirroring), that re-routing carries
  cleanly, and — critically — that `PinManager.broadcastPwm` is
  never called.

Totals: +700 LOC, 1876 frontend tests pass (was 1853), 278 backend
unit tests pass (was 259).

Docs: ESP32_EMULATION.md §9.2 rewritten with the new architecture
diagram + a runbook for adding future peripherals through the
SignalRouter.

The C plugin hook in qemu-lcgamboa that would push gpio_out_sel
writes synchronously (eliminating the polling race window entirely)
is the next step — kept as a follow-up because the polling-fallback
path here already resolves the routing before each duty event
fires, so the bug is fixed end-to-end. The plugin work removes the
race condition fundamentally.
2026-05-17 05:00:53 +02:00
..
examples Translate Spanish docs to English and add SEO-optimised URL routing for documentation 2026-03-11 18:46:42 +00:00
img/boards Refactor code structure for improved readability and maintainability 2026-03-16 14:28:16 -03:00
wiki refactor: rename deploy/ → docker/ (container build, not host deploy) 2026-05-04 00:05:55 -03:00
ARCHITECTURE.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
BOOT_IMAGES.md feat(sim): boot_images module + Pi 3 emulation restored 2026-05-16 05:41:46 +02:00
BUILD-QEMU.md docs: BUILD-QEMU.md + 'Build QEMU from source' docs section 2026-05-14 05:44:10 +02:00
CUSTOM_CHIPS.md Add ESP32 chip demos and comprehensive tests for I2C, SPI, and UART interactions 2026-04-28 19:24:39 -03:00
ESP32C3_WIFI_BLUETOOTH.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
ESP32_EMULATION.md feat(esp32): SignalRouter — model the GPIO Matrix as first-class 2026-05-17 05:00:53 +02:00
ESP32_WIFI_BLUETOOTH.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
MCP.md
MICROPYTHON_IMPLEMENTATION.md feat: add comprehensive MicroPython implementation documentation for RP2040, ESP32, ESP32-S3, and ESP32-C3 boards 2026-03-29 23:50:55 -03:00
PICO_W_WIFI_EMULATION.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
RASPBERRYPI3_EMULATION.md docs(pi3): refresh RASPBERRYPI3_EMULATION.md for the boot_images flow 2026-05-16 07:07:41 +02:00
RISCV_EMULATION.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
RP2040_EMULATION.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
SETUP_COMPLETE.md refactor: rename wokwi-libs/ → third-party/ 2026-05-03 00:58:57 -03:00
THIRD_PARTY.md feat(components): swap BMP280 + ATtiny85 to fritzing art 2026-05-08 23:06:44 -03:00
analytics.md feat: add GA4 key events tracking (run_simulation, open_example, create_project, compile_code, visit_github) 2026-03-12 22:02:24 +00:00
components.md
emulator.md
getting-started.md Refactor code structure for improved readability and maintainability 2026-03-16 14:22:23 -03:00
img1.png
img2.png
img3.png
img4.png
img5.png Refactor code structure for improved readability and maintainability 2026-03-16 18:31:46 -03:00
img6.png Refactor code structure for improved readability and maintainability 2026-03-23 23:24:14 -03:00
intro.md Add ESP32 chip demos and comprehensive tests for I2C, SPI, and UART interactions 2026-04-28 19:24:39 -03:00
roadmap.md