Link protocol v2, ESP-NOW leg, gauge tunables + tuning screen; remove Home screen

Protocol v2 (shared header, byte-identical with the 1.3 repo):
- typed DATA/CONFIG frames: 0xAA | ver | type | len | payload | XOR | 0x55
- gear split into lever (P/R/N/D/S/M) + target/engaged nibbles, engine rpm,
  alert bitmap decided here from tunable thresholds, link_config_t tunables
- docs/UART_PROTOCOL.md rewritten for v2

Controller A modules:
- tunables: gauge calibration + alert thresholds in NVS, 'cfg' serial console,
  bench override (never persisted)
- espnow_link: every frame also broadcast on ESP-NOW ch 1; radio stays in STA
  (WiFiManager no longer powers it off)
- uart_manager: DATA at 5 Hz + CONFIG on change/5 s on both transports
- can_driver: publishes gear pieces and rpm, evaluates alerts
- tuning_screen: hand-coded Gauge Tuning page (swipe right from Driveline),
  -/+ rows, enable switches, override sliders, Driveline/Defaults buttons

UI: SquareLine Home screen (3 meters + battery) and meters_manager removed;
Driveline is the boot screen; Settings back-gesture calls app_show_driveline().

tools/fix_penv_deps.ps1: repairs the shared PlatformIO penv after a 1.3 build.
Bench-verified with Controller B: both transports alive, dedupe, override
rpm 2000 -> 133.3 Hz tach output.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-09-21 19:12:31 +02:00
parent a8f68ebc27
commit f60bdac268
33 changed files with 1743 additions and 2266 deletions
+30 -13
View File
@@ -75,10 +75,14 @@ src/
main.cpp # Main application logic only
modules/
wifi_manager.cpp # WiFi scanning and connection
uart_manager.cpp # UART transmission (5Hz)
uart_manager.cpp # Link transmitter: v2 DATA/CONFIG frames on UART1 + ESP-NOW
tunables.cpp # Gauge calibration + alert thresholds (NVS, 'cfg' serial console)
espnow_link.cpp # ESP-NOW broadcast leg of the link
tuning_screen.cpp # Hand-coded LVGL Gauge Tuning screen (swipe right from Driveline)
settings_manager.cpp # NVS persistent storage
ui_state_manager.cpp # UI synchronization logic
display_init.cpp # Display initialization
display_init.cpp # Display initialization; boots into DrivelineScreen
driveline_screen.cpp # Hand-coded LVGL boot screen (gear, temps, rpm, status)
drivers/
can_driver.cpp # CAN Bus driver (TWAI)
@@ -103,9 +107,11 @@ include/
- Thread-safe UI updates via `lvgl_port_lock()`/`unlock()`
**include/protocol/automotive_data.h**
- Defines `automotive_data_t` structure (6 sensor values: gear position, 3 temperatures, battery voltage, fault codes)
- UART packet framing protocol: `uart_packet_t` with start/end markers (0xAA/0x55), version byte, XOR checksum
- Helper functions: `calculate_checksum()`, `validate_packet()`, temperature/voltage formatters
- **Shared with Controller B, must stay byte-identical in both repos.** Protocol v2 (2026-09-21).
- `automotive_data_t` (DATA payload): gear lever + target/engaged nibbles, rpm, 3 temperatures, battery, alert bitmap, valid flags
- `link_config_t` (CONFIG payload): Patrol gauge calibration + alert thresholds, pushed A → B
- Frame: `0xAA | version | type | length | payload | XOR | 0x55`; helpers `frame_build()` / `frame_parse()`
- Spec: `docs/UART_PROTOCOL.md`
- **BMW CAN Bus ID mappings**: 11-bit PT-CAN IDs verified live on the swap bus (evidence: `wiki/pages/can-id-candidates.md`)
- UART config: 115200 baud on GPIO 44 (TX) / 43 (RX, unused) — leaves the board via the SP3485 RS485 transceiver
@@ -127,8 +133,8 @@ include/
- Memory settings, theme configuration, widget enables
**include/ui/** directory (SquareLine Studio exports)
- `ui_Home_Screen.c/h`: Main display screen
- `ui_Settings_Screen.c/h`: WiFi, UART, CAN Bus, Debug toggle switches and containers
- `ui_Settings_Screen.c/h`: WiFi, UART, CAN Bus, Debug toggle switches and containers. Its right-swipe calls `app_show_driveline()` (C entry point in `driveline_screen.cpp`)
- The SquareLine **Home screen was removed on 2026-09-21** (its meters module too). `ui_init()` loads an empty placeholder; `display_init.cpp` then shows the Driveline screen. If SquareLine re-exports a Home screen, delete it from the SquareLine project — do not wire it back in
- External UI references: `uic_Toggle_Wifi_Switch`, `uic_Toggle_UART_Switch`, `uic_Toggle_CAN_Bus_Switch`, `uic_Toggle_Debug_Switch`, `uic_Wifi_Container`, `uic_Settings_Loading_Container`, `uic_Device_State_Container`
- Event handlers: `onWifiToggle()`, `onUARTToggle()`, `onCANBusToggle()`, `onDebugToggle()`
- **Important**: SquareLine Studio exports to `include/ui/` directory to avoid overwriting custom code in `src/`
@@ -165,20 +171,20 @@ Settings persist across power cycles via ESP32 NVS (Non-Volatile Storage):
### UART Architecture
**Transmission Protocol**:
- Packet structure: `uart_packet_t` = Start(0xAA) + Version + Length + `automotive_data_t` + Checksum(XOR) + End(0x55)
- Transmission rate: 5Hz (every 200ms) when enabled
**Transmission Protocol** (v2, see `docs/UART_PROTOCOL.md`):
- DATA frame (27 bytes) every 200 ms; CONFIG frame (33 bytes) on start, on every tunables change, and every 5 s
- Every frame goes out on UART1 **and** ESP-NOW broadcast (`EspNowLink`); B dedupes on `packet_id`
- Hardware: Serial1 TX GPIO 44 / RX GPIO 43 (RS485 terminal block on board)
**Task Flow**:
- `uartTask()` runs on Core 0
- Events via `g_uart_event_queue`: `UART_EVENT_START`, `UART_EVENT_STOP`, `UART_EVENT_SEND_DATA`
- Default data: `initializeDefaultData()` seeds the structure until CAN updates it; the dummy generator was removed
- Actual transmission: `sendUARTPacket()` builds packet, calculates checksum, sends via `Serial1.write()`
- `sendDataFrame()` / `sendConfigFrame()` build frames with `frame_build()` and call `sendFrame()` (UART1 + ESP-NOW)
- Mutex protection: `g_data_mutex` guards `g_automotive_data` structure
**CAN Bus Integration**:
- CAN driver reads BMW-specific messages and updates `g_automotive_data`
- CAN driver reads BMW-specific messages and updates `g_automotive_data`; `publishGear()` fills lever + nibbles, `updateAlerts()` evaluates the alert bits against `Tunables` thresholds
- UART auto-starts/stops with the CAN task (`linkToCANState()`); no dummy simulation remains
- Shared data structure (`g_automotive_data`) is mutex-protected for thread safety
@@ -213,6 +219,14 @@ Settings persist across power cycles via ESP32 NVS (Non-Volatile Storage):
**See Also**: `wiki/pages/can-id-candidates.md` for the verified CAN ID map and evidence, `wiki/pages/can-bus-integration.md` for integration notes
### Tunables (gauge calibration, alert thresholds)
`src/modules/tunables.cpp` owns `link_config_t`: tach pulses/rev + trim + clamps, coolant PWM frequency + duty law + min temp, per-output enables, a bench override (force rpm / duty), and the alert thresholds. Stored as an NVS blob (namespace `tunables`). Edited over the USB serial console: `cfg show`, `cfg set <name> <value>`, `cfg override rpm <n> | duty <n> | both <rpm> <duty> | off`, `cfg reset`. Every change bumps `cfg_version`, which makes the UART task push a CONFIG frame. Overrides are pushed but never saved. The **Gauge Tuning screen** (`src/modules/tuning_screen.cpp`, hand-coded like the driveline screen so SquareLine exports cannot clobber it; Home → swipe right → Driveline → swipe right → Tuning) edits the same values with −/+ buttons, enable switches, and an override panel with RPM and duty sliders. Overrides are never written to NVS (`save()` strips them).
### ESP-NOW link
`src/modules/espnow_link.cpp` broadcasts every frame on Wi-Fi channel `ESPNOW_CHANNEL` (1). The radio stays in STA mode permanently — `WiFiManager`'s "off" only stops scanning/association (it no longer calls `WiFi.mode(WIFI_OFF)`). If Wi-Fi joins an AP the channel follows the AP; B hops to find it.
### UI State Synchronization
**Problem**: SquareLine Studio screens get destroyed/recreated when swiping between screens, losing runtime state.
@@ -223,6 +237,8 @@ Settings persist across power cycles via ESP32 NVS (Non-Volatile Storage):
- Syncs toggle switches with actual service states: `g_wifi_state`, `g_uart_state`, `g_debug_enabled`
- Shows loading spinner during sync for smooth UX
**Screens**: Driveline (boot, hand-coded) ← swipe left → Settings (SquareLine); Driveline → swipe right → Gauge Tuning (hand-coded).
**Debug Mode**:
- Toggle in Settings screen shows/hides `uic_Device_State_Container`
- Updates FPS (`uic_FPS_Values_LBL`) and CPU usage (`uic_CPU_Values_LBL`) every second
@@ -328,7 +344,7 @@ xTaskCreatePinnedToCore(
### GPIO Assignments
- **CAN Bus**: GPIO 15 (TX), GPIO 16 (RX) - 500 Kbps, TWAI_MODE_NORMAL, onboard TJA1051 on the terminal block
- **UART1**: GPIO 44 (TX), GPIO 43 (RX, unused) - reaches the terminal block only as the RS485 A/B pair (SP3485), 115200 baud
- **UART1**: GPIO 44 (TX), GPIO 43 (RX, unused) - reaches the terminal block only as the RS485 A/B pair (SP3485), 115200 baud. Bench: `485_A` straight into B's IO1 + GND works (verified 2026-09-21)
- **USB Serial**: UART0 (default Serial) - 115200 baud for debug output
### Display Panel
@@ -348,6 +364,7 @@ xTaskCreatePinnedToCore(
### Build Errors
- **`Failed to install Python dependencies into penv (exit code: 2)`** at the start of `pio run`: the sibling ESP32-S3-LCD-1.3 project (official espressif32@6 platform) installs newer `urllib3`/`esptool` into the shared PlatformIO penv, and this project's pioarduino platform needs `urllib3<2`; its install step hides the error. Run `. ools ix_penv_deps.ps1`, then build again. Never build the two projects at the same time (shared penv).
- **Missing libraries**: Run `pio lib update` then `pio run --target clean`
- **Partition table not found**: Ensure `partition_table.csv` exists in project root
- **Out of memory**: Check PSRAM enabled (`board_build.psram_type = opi` in platformio.ini)