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
+74 -8
View File
@@ -9,6 +9,7 @@
#include "modules/uart_manager.h"
#include "modules/settings_manager.h"
#include "modules/serial_lock.h"
#include "modules/tunables.h"
#include "esp_log.h"
// Import shared data from UARTManager namespace
@@ -55,6 +56,9 @@ static driveline_t g_driveline = {0};
static gear_position_t g_last_lever = GEAR_UNKNOWN;
// When the current disagreement started (0 = currently in agreement).
static uint32_t g_mismatch_since = 0;
// Gearbox target gear number from 0x0BA (1..6 forward, 0 = P/R/N/none), for the
// wire's target nibble. Lives here because 0x0BA and 0x1D2 arrive separately.
static uint8_t g_target_gear_num = 0;
// ============================================================================
// PRIVATE FUNCTIONS
@@ -87,7 +91,7 @@ static void updateGearMismatch(gear_position_t target) {
if (!disagrees) {
g_mismatch_since = 0;
g_driveline.gear_mismatch = false;
g_automotive_data.fault_codes &= ~FAULT_GEAR_MISMATCH;
g_automotive_data.alerts &= ~ALERT_GEAR_MISMATCH;
return;
}
@@ -96,11 +100,66 @@ static void updateGearMismatch(gear_position_t target) {
g_mismatch_since = now; // start the clock, don't fault yet
} else if ((now - g_mismatch_since) >= GEAR_MISMATCH_DEBOUNCE_MS) {
g_driveline.gear_mismatch = true;
g_automotive_data.fault_codes |= FAULT_GEAR_MISMATCH;
g_automotive_data.valid_flags |= VALID_FLAG_FAULT_CODES;
g_automotive_data.alerts |= ALERT_GEAR_MISMATCH;
g_automotive_data.valid_flags |= VALID_FLAG_ALERTS;
}
}
/**
* @brief Publish the wire gear fields (lever + target/engaged nibbles) from the
* driveline pieces. Caller must hold g_data_mutex.
*/
static void publishGear() {
uint8_t lever;
switch ((gear_position_t)g_driveline.lever) {
case GEAR_PARK: lever = LEVER_PARK; break;
case GEAR_REVERSE: lever = LEVER_REVERSE; break;
case GEAR_NEUTRAL: lever = LEVER_NEUTRAL; break;
case GEAR_DRIVE:
lever = (g_driveline.gear_mode == GEAR_MODE_SPORT) ? LEVER_SPORT
: (g_driveline.gear_mode == GEAR_MODE_MANUAL) ? LEVER_MANUAL
: LEVER_DRIVE;
break;
default: lever = LEVER_UNKNOWN; break;
}
g_automotive_data.gear_lever = lever;
g_automotive_data.gear_nibbles =
(uint8_t)((g_target_gear_num << 4) | (g_driveline.engaged_gear & 0x0F));
if (lever != LEVER_UNKNOWN) {
g_automotive_data.valid_flags |= VALID_FLAG_GEAR_POS;
}
}
/**
* @brief Re-evaluate the alert bitmap from the current values and A's thresholds
* (Tunables). The gear-mismatch bit is owned by updateGearMismatch() and
* preserved here. Caller must hold g_data_mutex.
*/
static void updateAlerts() {
link_config_t cfg = Tunables::get();
uint8_t valid = g_automotive_data.valid_flags;
uint16_t a = g_automotive_data.alerts & ALERT_GEAR_MISMATCH;
if ((valid & VALID_FLAG_COOLANT_TEMP) &&
g_automotive_data.coolant_temp >= (int16_t)(cfg.coolant_hot_c * 10)) {
a |= ALERT_COOLANT_HOT;
}
if ((valid & VALID_FLAG_GEARBOX_TEMP) &&
g_automotive_data.gearbox_oil_temp >= (int16_t)(cfg.gearbox_hot_c * 10)) {
a |= ALERT_GEARBOX_HOT;
}
if (g_driveline.gearbox_cooling == GEARBOX_COOL_STAGE1 ||
g_driveline.gearbox_cooling == GEARBOX_COOL_STAGE2) {
a |= ALERT_GEARBOX_COOLING;
}
if ((valid & VALID_FLAG_BATTERY_VOLT) &&
g_automotive_data.battery_voltage < cfg.battery_low_x100) {
a |= ALERT_BATTERY_LOW;
}
g_automotive_data.alerts = a;
g_automotive_data.valid_flags |= VALID_FLAG_ALERTS;
}
/**
* @brief Parse BMW-specific CAN message and update automotive data
* @param message Received TWAI message
@@ -157,10 +216,7 @@ static bool parseCANMessage(const twai_message_t& message) {
case CAN_ID_GEAR_DISPLAY: {
// 0x1D2: the EGS publishing its whole state — byte 0 is what the driver
// selected, byte 1 is what's actually engaged, byte 4 is the mode.
gear_position_t g = decode_gear(data, dlc);
if (g != GEAR_UNKNOWN) {
g_automotive_data.gear_position = (uint8_t)g;
g_automotive_data.valid_flags |= VALID_FLAG_GEAR_POS;
if (decode_gear(data, dlc) != GEAR_UNKNOWN) {
parsed = true;
}
// Break the frame into its pieces for the driveline screen, which
@@ -176,6 +232,7 @@ static bool parseCANMessage(const twai_message_t& message) {
if (dlc > CAN_BYTE_GEAR_MODE) {
g_driveline.gear_mode = data[CAN_BYTE_GEAR_MODE];
}
publishGear();
break;
}
@@ -186,6 +243,8 @@ static bool parseCANMessage(const twai_message_t& message) {
gear_position_t target =
can_to_target_gear(data[CAN_BYTE_GEARBOX_TARGET] & 0x0F);
updateGearMismatch(target);
g_target_gear_num = can_to_target_gear_num(data[CAN_BYTE_GEARBOX_TARGET] & 0x0F);
publishGear();
// ST_CCLT lives in the top two bits of the same byte.
g_driveline.converter_clutch =
(data[CAN_BYTE_CONVERTER_CLUTCH] >> 6) & 0x03;
@@ -219,6 +278,8 @@ static bool parseCANMessage(const twai_message_t& message) {
// 0x0AA: engine RPM + accelerator pedal.
if (dlc > CAN_BYTE_RPM_H) {
g_driveline.engine_rpm = can_to_rpm(data);
g_automotive_data.engine_rpm = g_driveline.engine_rpm;
g_automotive_data.valid_flags |= VALID_FLAG_RPM;
parsed = true;
}
if (dlc > CAN_BYTE_PEDAL_POS) {
@@ -254,6 +315,8 @@ static bool parseCANMessage(const twai_message_t& message) {
break;
}
updateAlerts();
xSemaphoreGive(g_data_mutex);
return parsed;
@@ -323,9 +386,11 @@ static void handleRxMessage(twai_message_t& message) {
// Per-frame decoded prose — off by default (redundant with raw capture)
if (g_verbose_decode && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
char gearbuf[8];
gear_compose(g_automotive_data.gear_lever, gear_engaged(g_automotive_data.gear_nibbles), gearbuf, sizeof(gearbuf));
ESP_LOGI("CANDrv", "✓ RX 0x%X → Gear:%s%s Coolant:%d.%d°C Gearbox:%d.%d°C Batt:%d.%02dV | RPM:%u Turb:%u Shaft:%u [%d/%d]",
message.identifier,
gear_to_string((gear_position_t)g_automotive_data.gear_position),
gearbuf,
g_driveline.gear_mismatch ? " !MISMATCH" : "",
g_automotive_data.coolant_temp / 10, abs(g_automotive_data.coolant_temp % 10),
g_automotive_data.gearbox_oil_temp / 10, abs(g_automotive_data.gearbox_oil_temp % 10),
@@ -434,6 +499,7 @@ static void canTask(void* parameter) {
// before the first 0x1D2 frame — force UNKNOWN instead.
g_driveline.lever = (uint8_t)GEAR_UNKNOWN;
g_last_lever = GEAR_UNKNOWN;
g_target_gear_num = 0;
g_mismatch_since = 0;
ESP_LOGI("CANTask", "CAN reception active");