# starfactorypixel/mainecu architecture

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

- Interactive diagram: https://gitdiagram.com/starfactorypixel/mainecu
- Repository: https://github.com/starfactorypixel/mainecu

## Overview

This repository is firmware for a vehicle ECU board (the README identifies hardware revision 2.1). Its central workflow initializes the ECU, handles L2/L3 requests and vehicle-state data, and runs scripts that can translate state changes into CAN transmissions. The code also includes script storage/transfer, device emulation, authentication, and hardware-facing modules. The available excerpts show setup and some request handling; several runtime connections are therefore omitted rather than inferred.

Main entry points: [`src/MainECU.cpp`](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/src/MainECU.cpp) (ECU firmware), [`lib/CANScripts/CANScripts.h`](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/CANScripts.h) (CAN script dispatch), [`lib/L3/L3Wrapper.h`](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L3/L3Wrapper.h) (L3 wrapper), [`include/ScriptLogic.h`](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/ScriptLogic.h) (Script engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_boot["ECU startup"]
  node_ecu["ECU firmware<br/>[MainECU.cpp]"]
  node_about["Device identity<br/>[About.h]"]
  node_config["Configuration<br/>[Config.h]"]
  node_security["Authentication<br/>[Security.h]"]
end

subgraph group_protocol["Vehicle protocols"]
  node_can_core["CAN core<br/>[CANCore.h]"]
  node_can_driver["ESP32 CAN driver<br/>[ESP32SJA1000.cpp]"]
  node_l2["L2 wrapper<br/>[L2Wrapper.h]"]
  node_l3["L3 wrapper<br/>[L3Wrapper.h]"]
  node_l3_drivers["L3 transports<br/>[L3Driver.h]"]
end

subgraph group_logic["State and scripts"]
  node_state[("State database<br/>[StateDB.h]")]
  node_script_logic["Script engine<br/>[ScriptLogic.h]"]
  node_custom_opcodes["Custom opcodes"]
  node_can_scripts["CAN script dispatch<br/>[CANScripts.h]"]
  node_vehicle_scripts["Vehicle behaviors<br/>[ScriptsCore.h]"]
  node_light_scripts["Lighting behaviors<br/>[ScriptsLight.h]"]
end

subgraph group_hardware["Hardware services"]
  node_spi["SPI and flash<br/>[SPICore.h]"]
  node_leds["LED control<br/>[Leds.h]"]
  node_analog["Analog inputs<br/>[Analog.h]"]
end

subgraph group_support["Emulation"]
  node_virtual_device["Virtual device<br/>[VirtualDevice.h]"]
  node_virtual_interface["Device interface"]
end

node_can_bus(("Vehicle CAN bus"))
node_ecu_client(("ECU client"))

node_ecu -->|"initializes"| node_about
node_ecu -->|"initializes"| node_leds
node_ecu -->|"initializes"| node_config
node_ecu -->|"initializes"| node_security
node_ecu -->|"initializes"| node_can_core
node_ecu -->|"initializes"| node_spi
node_ecu -->|"initializes"| node_analog
node_ecu -->|"initializes"| node_script_logic
node_ecu -->|"reads and writes"| node_state
node_ecu -->|"sends packets"| node_l3
node_ecu_client -.->|"requests services"| node_ecu
node_can_core -.->|"uses driver"| node_can_driver
node_can_bus -.->|"exchanges frames"| node_can_driver
node_ecu -->|"handles L2 packets"| node_l2
node_ecu -->|"handles L3 requests"| node_l3
node_l3 -.->|"selects transport"| node_l3_drivers
node_script_logic -->|"registers opcodes"| node_custom_opcodes
node_can_scripts -->|"dispatches behavior"| node_vehicle_scripts
node_can_scripts -->|"dispatches behavior"| node_light_scripts
node_can_scripts -->|"sends CAN packets"| node_l2
node_ecu -->|"transfers scripts"| node_script_logic
node_virtual_device -->|"implements"| node_virtual_interface

click node_ecu "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/src/MainECU.cpp"
click node_about "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/About.h"
click node_config "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Config.h"
click node_security "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Security.h"
click node_can_core "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/CANCore.h"
click node_can_driver "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/arduino-CAN/src/ESP32SJA1000.cpp"
click node_l2 "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L2/L2Wrapper.h"
click node_l3 "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L3/L3Wrapper.h"
click node_l3_drivers "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L3/L3Driver.h"
click node_state "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/StateDB/StateDB.h"
click node_script_logic "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/ScriptLogic.h"
click node_custom_opcodes "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/ScriptLogicCustom.h"
click node_can_scripts "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/CANScripts.h"
click node_vehicle_scripts "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/ScriptsCore.h"
click node_light_scripts "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/ScriptsLight.h"
click node_spi "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/SPICore.h"
click node_leds "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Leds.h"
click node_analog "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Analog.h"
click node_virtual_device "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/Emulator/VirtualDevice.h"
click node_virtual_interface "https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/Emulator/VirtualDeviceInterface.h"

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_ecu,node_about,node_config,node_security,node_ecu_client toneBlue
class node_can_core,node_can_driver,node_l2,node_l3,node_l3_drivers toneAmber
class node_state,node_script_logic,node_custom_opcodes,node_can_scripts,node_vehicle_scripts,node_light_scripts toneMint
class node_spi,node_leds,node_analog toneRose
class node_virtual_device,node_virtual_interface,node_can_bus toneIndigo
```

## Components

### ECU startup

- [ECU firmware](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/src/MainECU.cpp): `src/MainECU.cpp`
- [Device identity](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/About.h): `include/About.h`
- [Configuration](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Config.h): `include/Config.h`
- [Authentication](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Security.h): `include/Security.h`

### Vehicle protocols

- [CAN core](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/CANCore.h): `include/CANCore.h`
- [ESP32 CAN driver](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/arduino-CAN/src/ESP32SJA1000.cpp): `lib/arduino-CAN/src/ESP32SJA1000.cpp`
- [L2 wrapper](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L2/L2Wrapper.h): `lib/L2/L2Wrapper.h`
- [L3 wrapper](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L3/L3Wrapper.h): `lib/L3/L3Wrapper.h`
- [L3 transports](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/L3/L3Driver.h): `lib/L3/L3Driver.h`

### State and scripts

- [State database](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/StateDB/StateDB.h): `lib/StateDB/StateDB.h`
- [Script engine](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/ScriptLogic.h): `include/ScriptLogic.h`
- [Custom opcodes](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/ScriptLogicCustom.h): `include/ScriptLogicCustom.h`
- [CAN script dispatch](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/CANScripts.h): `lib/CANScripts/CANScripts.h`
- [Vehicle behaviors](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/ScriptsCore.h): `lib/CANScripts/ScriptsCore.h`
- [Lighting behaviors](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/CANScripts/ScriptsLight.h): `lib/CANScripts/ScriptsLight.h`

### Hardware services

- [SPI and flash](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/SPICore.h): `include/SPICore.h`
- [LED control](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Leds.h): `include/Leds.h`
- [Analog inputs](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/include/Analog.h): `include/Analog.h`

### Emulation

- [Virtual device](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/Emulator/VirtualDevice.h): `lib/Emulator/VirtualDevice.h`
- [Device interface](https://github.com/starfactorypixel/mainecu/blob/board_v2.1/lib/Emulator/VirtualDeviceInterface.h): `lib/Emulator/VirtualDeviceInterface.h`

### Other components

- **Vehicle CAN bus**
- **ECU client**

## Connections

- ECU firmware → Device identity: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → LED control: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → Configuration: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → Authentication: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → CAN core: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → SPI and flash: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → Analog inputs: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → Script engine: initializes (evidence: `src/MainECU.cpp`)
- ECU firmware → State database: reads and writes (evidence: `src/MainECU.cpp`)
- ECU firmware → L3 wrapper: sends packets (evidence: `src/MainECU.cpp`)
- ECU client → ECU firmware: requests services
- CAN core → ESP32 CAN driver: uses driver
- Vehicle CAN bus → ESP32 CAN driver: exchanges frames
- ECU firmware → L2 wrapper: handles L2 packets (evidence: `src/MainECU.cpp`)
- ECU firmware → L3 wrapper: handles L3 requests (evidence: `src/MainECU.cpp`)
- L3 wrapper → L3 transports: selects transport
- Script engine → Custom opcodes: registers opcodes (evidence: `include/ScriptLogic.h`)
- CAN script dispatch → Vehicle behaviors: dispatches behavior (evidence: `lib/CANScripts/CANScripts.h`)
- CAN script dispatch → Lighting behaviors: dispatches behavior (evidence: `lib/CANScripts/CANScripts.h`)
- CAN script dispatch → L2 wrapper: sends CAN packets (evidence: `lib/CANScripts/CANScripts.h`)
- ECU firmware → Script engine: transfers scripts (evidence: `include/ScriptLogic.h`)
- Virtual device → Device interface: implements (evidence: `lib/Emulator/VirtualDevice.h`)
