# drrcastro/esp-lego-loco architecture

> Architecture diagram of the GitHub repository drrcastro/esp-lego-loco, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/drrcastro/esp-lego-loco
- Repository: https://github.com/drrcastro/esp-lego-loco

## Overview

ESP-lego-loco is a modular firmware and control system for autonomous model trains: a Master coordinates locomotive and smart-station nodes, while a browser dashboard configures and monitors the layout. The main workflow runs from beacon and station sensing, through node telemetry and topology tracking, to dispatch controls and locomotive motion. The repository also documents a standalone browser firmware flasher. Several implementation excerpts are partial; edges are limited to README-documented relationships or visible source references and calls, with uncertain wiring omitted.

Main entry points: [`lib/ESPNowManager/ESPNowManager.cpp`](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ESPNowManager/ESPNowManager.cpp) (ESP-NOW Messaging), [`src/loco/main.cpp`](https://github.com/drrcastro/esp-lego-loco/blob/main/src/loco/main.cpp) (Locomotive Firmware), [`src/track/main.cpp`](https://github.com/drrcastro/esp-lego-loco/blob/main/src/track/main.cpp) (Station Firmware), [`src/master/TopologyManager.cpp`](https://github.com/drrcastro/esp-lego-loco/blob/main/src/master/TopologyManager.cpp) (Topology Manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_coordination["Master Coordination"]
  node_master_firmware["Master Firmware<br/>[main.cpp]"]
  node_topology["Topology Manager"]
  node_espnow["ESP-NOW Messaging<br/>[ESPNowManager.cpp]"]
  node_protocol["Message Protocol<br/>[ProtocolMessages.h]"]
  node_config[("Configuration Store<br/>[ConfigStore.cpp]")]
end

subgraph group_firmware["Train Firmware"]
  node_loco_firmware["Locomotive Firmware<br/>[main.cpp]"]
  node_station_firmware["Station Firmware<br/>[main.cpp]"]
end

subgraph group_hardware["Device Services"]
  node_motor["Motor Control"]
  node_lighting["Lighting System<br/>[LightingSystem.cpp]"]
  node_ir["IR Telemetry<br/>[IRTelemetry.cpp]"]
  node_track_manager["Track Manager<br/>[TrackManager.cpp]"]
  node_station_display["Station Display<br/>[StationDisplay.cpp]"]
end

subgraph group_control["Web Control"]
  node_web_server["Web Server API<br/>[LocoWebServer.cpp]"]
  node_dashboard["Dispatch Dashboard<br/>[app.js]"]
  node_web_assets["Web UI Assets<br/>[index.html]"]
end

subgraph group_install["Browser Flasher"]
  node_flasher_ui["Web Flasher<br/>[flasher.js]"]
  node_firmware_images["Merged Firmware Images"]
end

node_operator(("Layout Operator"))
node_browser(("Operator Browser"))
node_esp_board["ESP Boards"]

node_operator -->|"operates"| node_browser
node_browser -->|"loads"| node_dashboard
node_web_assets -->|"served by"| node_web_server
node_dashboard -->|"controls and monitors"| node_web_server
node_master_firmware -->|"hosts"| node_web_server
node_master_firmware -->|"coordinates topology"| node_topology
node_topology -->|"loads and saves"| node_config
node_topology -->|"broadcasts telemetry"| node_web_server
node_topology -->|"sends commands"| node_espnow
node_loco_firmware -->|"initializes"| node_motor
node_loco_firmware -->|"sets lighting"| node_lighting
node_loco_firmware -->|"sends telemetry"| node_espnow
node_station_firmware -->|"controls switch and dwell"| node_track_manager
node_station_firmware -->|"updates display"| node_station_display
node_station_firmware -->|"sends telemetry"| node_espnow
node_loco_firmware -.->|"uses beacon sensing"| node_ir
node_station_firmware -.->|"uses beam sensing"| node_ir
node_espnow -->|"encodes messages"| node_protocol
node_master_firmware -.->|"uses node messaging"| node_espnow
node_browser -->|"opens"| node_flasher_ui
node_flasher_ui -->|"flashes image"| node_firmware_images
node_flasher_ui -->|"programs over USB"| node_esp_board

click node_loco_firmware "https://github.com/drrcastro/esp-lego-loco/blob/main/src/loco/main.cpp"
click node_station_firmware "https://github.com/drrcastro/esp-lego-loco/blob/main/src/track/main.cpp"
click node_master_firmware "https://github.com/drrcastro/esp-lego-loco/blob/main/src/master/main.cpp"
click node_topology "https://github.com/drrcastro/esp-lego-loco/blob/main/src/master/TopologyManager.cpp"
click node_espnow "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ESPNowManager/ESPNowManager.cpp"
click node_protocol "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ESPNowManager/ProtocolMessages.h"
click node_config "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ConfigStore/ConfigStore.cpp"
click node_motor "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/MotorController/MotorController.cpp"
click node_lighting "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/LightingSystem/LightingSystem.cpp"
click node_ir "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/IRTelemetry/IRTelemetry.cpp"
click node_track_manager "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/TrackManager/TrackManager.cpp"
click node_station_display "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/StationDisplay/StationDisplay.cpp"
click node_web_server "https://github.com/drrcastro/esp-lego-loco/blob/main/lib/WebServer/LocoWebServer.cpp"
click node_dashboard "https://github.com/drrcastro/esp-lego-loco/blob/main/data/app.js"
click node_web_assets "https://github.com/drrcastro/esp-lego-loco/blob/main/data/index.html"
click node_flasher_ui "https://github.com/drrcastro/esp-lego-loco/blob/main/tools/web-flasher/flasher.js"
click node_firmware_images "https://github.com/drrcastro/esp-lego-loco/tree/main/tools/web-flasher/binaries"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_master_firmware,node_topology,node_espnow,node_protocol,node_config,node_browser toneBlue
class node_loco_firmware,node_station_firmware toneAmber
class node_motor,node_lighting,node_ir,node_track_manager,node_station_display toneMint
class node_web_server,node_dashboard,node_web_assets toneRose
class node_flasher_ui,node_firmware_images,node_operator,node_esp_board toneIndigo
```

## Components

### Master Coordination

- [Master Firmware](https://github.com/drrcastro/esp-lego-loco/blob/main/src/master/main.cpp): `src/master/main.cpp`
- [Topology Manager](https://github.com/drrcastro/esp-lego-loco/blob/main/src/master/TopologyManager.cpp): `src/master/TopologyManager.cpp`
- [ESP-NOW Messaging](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ESPNowManager/ESPNowManager.cpp): `lib/ESPNowManager/ESPNowManager.cpp`
- [Message Protocol](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ESPNowManager/ProtocolMessages.h): `lib/ESPNowManager/ProtocolMessages.h`
- [Configuration Store](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/ConfigStore/ConfigStore.cpp): `lib/ConfigStore/ConfigStore.cpp`

### Train Firmware

- [Locomotive Firmware](https://github.com/drrcastro/esp-lego-loco/blob/main/src/loco/main.cpp): `src/loco/main.cpp`
- [Station Firmware](https://github.com/drrcastro/esp-lego-loco/blob/main/src/track/main.cpp): `src/track/main.cpp`

### Device Services

- [Motor Control](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/MotorController/MotorController.cpp): `lib/MotorController/MotorController.cpp`
- [Lighting System](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/LightingSystem/LightingSystem.cpp): `lib/LightingSystem/LightingSystem.cpp`
- [IR Telemetry](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/IRTelemetry/IRTelemetry.cpp): `lib/IRTelemetry/IRTelemetry.cpp`
- [Track Manager](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/TrackManager/TrackManager.cpp): `lib/TrackManager/TrackManager.cpp`
- [Station Display](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/StationDisplay/StationDisplay.cpp): `lib/StationDisplay/StationDisplay.cpp`

### Web Control

- [Web Server API](https://github.com/drrcastro/esp-lego-loco/blob/main/lib/WebServer/LocoWebServer.cpp): `lib/WebServer/LocoWebServer.cpp`
- [Dispatch Dashboard](https://github.com/drrcastro/esp-lego-loco/blob/main/data/app.js): `data/app.js`
- [Web UI Assets](https://github.com/drrcastro/esp-lego-loco/blob/main/data/index.html): `data/index.html`

### Browser Flasher

- [Web Flasher](https://github.com/drrcastro/esp-lego-loco/blob/main/tools/web-flasher/flasher.js): `tools/web-flasher/flasher.js`
- [Merged Firmware Images](https://github.com/drrcastro/esp-lego-loco/tree/main/tools/web-flasher/binaries): `tools/web-flasher/binaries`

### Other components

- **Layout Operator**
- **Operator Browser**
- **ESP Boards**

## Connections

- Layout Operator → Operator Browser: operates (evidence: `README.md`)
- Operator Browser → Dispatch Dashboard: loads (evidence: `README.md`)
- Web UI Assets → Web Server API: served by (evidence: `README.md`)
- Dispatch Dashboard → Web Server API: controls and monitors (evidence: `README.md`)
- Master Firmware → Web Server API: hosts (evidence: `README.md`)
- Master Firmware → Topology Manager: coordinates topology (evidence: `README.md`)
- Topology Manager → Configuration Store: loads and saves (evidence: `src/master/TopologyManager.cpp`)
- Topology Manager → Web Server API: broadcasts telemetry (evidence: `src/master/TopologyManager.cpp`)
- Topology Manager → ESP-NOW Messaging: sends commands (evidence: `src/master/TopologyManager.cpp`)
- Locomotive Firmware → Motor Control: initializes (evidence: `src/loco/main.cpp`)
- Locomotive Firmware → Lighting System: sets lighting (evidence: `src/loco/main.cpp`)
- Locomotive Firmware → ESP-NOW Messaging: sends telemetry (evidence: `src/loco/main.cpp`)
- Station Firmware → Track Manager: controls switch and dwell (evidence: `src/track/main.cpp`)
- Station Firmware → Station Display: updates display (evidence: `src/track/main.cpp`)
- Station Firmware → ESP-NOW Messaging: sends telemetry (evidence: `src/track/main.cpp`)
- Locomotive Firmware → IR Telemetry: uses beacon sensing
- Station Firmware → IR Telemetry: uses beam sensing
- ESP-NOW Messaging → Message Protocol: encodes messages
- Master Firmware → ESP-NOW Messaging: uses node messaging
- Operator Browser → Web Flasher: opens (evidence: `README.md`)
- Web Flasher → Merged Firmware Images: flashes image (evidence: `README.md`)
- Web Flasher → ESP Boards: programs over USB (evidence: `README.md`)
