From 0b31c6f5f7cc44544d5e2226eb0eca0731526257 Mon Sep 17 00:00:00 2001 From: Julian Cuni Date: Fri, 17 Jul 2026 16:23:10 +0200 Subject: [PATCH] Add Driveline screen; stop displaying values that were never measured New hand-coded LVGL screen (src/modules/driveline_screen.*) showing only signals verified on this bus: gear as the hero, coolant and gearbox oil temp flanking it, an engine RPM bar, and turbine / output-shaft speeds, plus ignition, engine-run, converter-clutch, gearbox-cooling and gear-mismatch status. Reached by swiping right from Home. It lives in src/ and hooks its gesture onto the Home screen at RUNTIME rather than editing ui_Home_Screen.c, because SquareLine Studio overwrites include/ui/ on every export - that is how the CAN toggle's event handler went missing. LVGL dispatches to every registered callback, so the runtime hook coexists with the generated left-swipe handler instead of replacing it. Same reasoning the existing meters_manager already follows. Output shaft is labelled "raw" and shown as counts: the x0.125 scaling is still a hypothesis, so inventing a unit here would launder it into fact. Also fixes a defect found while wiring this up: main.cpp updated the Home screen's meters and battery label unconditionally, but those fields are pre-seeded with DEFAULT_* constants. With no CAN data the display showed a confident 92.0degC engine oil temp and 12.45V battery that had never been measured. Two of them have no source on this bus at all - engine oil temp has no sensor (0x1D0 byte 1 is permanently 0xFF), and 0x3B4 publishes the battery's not-available marker on nearly every frame. Now gated on valid_flags, so an unmeasured value reads "--.--" instead of a plausible lie. Same class of bug in the driver: 0x3B4's U_BT was accepted even when it read 0xFFF, which would have put a confident 61V on screen. Now rejected, leaving the valid flag clear. Decodes ignition state from 0x3B4 byte 2 (user-verified: 0x00 = on, 0x09 = off, unmoved by the engine starting). Both the E65 repo (ST_ENGINE_RUN) and the E60 VFL sheet (engine flag, 0x09 = running) are wrong about this field on our bus - inverted and mislabelled. Noted at the definition so it doesn't get "corrected" back. It also restores ignition sensing, which we thought was lost with CAS/0x130 absent from the swap. Co-Authored-By: Claude Opus 4.8 --- include/drivers/can_driver.h | 3 + include/protocol/automotive_data.h | 34 ++- src/drivers/can_driver.cpp | 19 +- src/main.cpp | 36 ++- src/modules/driveline_screen.cpp | 365 +++++++++++++++++++++++++++++ src/modules/driveline_screen.h | 49 ++++ 6 files changed, 493 insertions(+), 13 deletions(-) create mode 100644 src/modules/driveline_screen.cpp create mode 100644 src/modules/driveline_screen.h diff --git a/include/drivers/can_driver.h b/include/drivers/can_driver.h index ab1ffa2..97ba57c 100644 --- a/include/drivers/can_driver.h +++ b/include/drivers/can_driver.h @@ -86,6 +86,9 @@ typedef struct { uint8_t converter_clutch; // 0x0BA ST_CCLT (CCLT_* constants). When CLOSED, // turbine_rpm == engine_rpm by design — read this // before interpreting the two as a slip ratio. + bool ignition_on; // 0x3B4 byte 2 low nibble. This bus's only + // ignition/terminal signal — CAS (0x130) is not + // present in the swap. bool gear_mismatch; // lever (0x1D2) vs gearbox (0x0BA) disagree, // sustained past the debounce window } driveline_t; diff --git a/include/protocol/automotive_data.h b/include/protocol/automotive_data.h index 7dc15ff..c1556c5 100644 --- a/include/protocol/automotive_data.h +++ b/include/protocol/automotive_data.h @@ -211,6 +211,15 @@ typedef struct __attribute__((packed)) { #define CAN_ID_BATTERY_VOLTAGE 0x3B4 #define CAN_BYTE_BATTERY_VOLT_L 0 // U_BT: 12-bit little-endian, V = raw * 0.015 #define CAN_BYTE_BATTERY_VOLT_H 1 // low nibble only (high nibble = ST_BTSW) +#define BATTERY_RAW_INVALID 0xFFF // U_BT's not-available marker + +// Byte 2 low nibble. The E65 repo calls this ST_ENGINE_RUN and the E60 VFL sheet +// calls it an engine-running flag with 0x09=running — BOTH ARE WRONG FOR US. +// Verified on this bus 2026-07-17: it tracks IGNITION, inverted, and does not +// move when the engine starts. Use 0x1D0 ST_ENGINE_RUN for engine state. +#define CAN_BYTE_IGNITION 2 +#define IGNITION_ON 0x00 +#define IGNITION_OFF 0x09 // All IDs are 11-bit standard frames — the extended-ID assumption came from the // bogus 0x1001/0x101F placeholders and is dead. @@ -275,17 +284,34 @@ static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset) { return (int16_t)(((int16_t)can_value - offset) * 10); } +/** + * Extract the raw 12-bit U_BT field from a 0x3B4 frame. + * The high nibble of byte 1 is ST_BTSW, NOT voltage — mask it off or you'll read + * ~4x too high. + * @param d Payload of a 0x3B4 frame (needs DLC >= 2) + * @return Raw 12-bit value; BATTERY_RAW_INVALID (0xFFF) means "not available" + */ +static inline uint16_t can_battery_raw(const uint8_t* d) { + return (uint16_t)(((d[CAN_BYTE_BATTERY_VOLT_H] & 0x0F) << 8) | d[CAN_BYTE_BATTERY_VOLT_L]); +} + /** * Decode battery voltage from a 0x3B4 frame. * U_BT is 12-bit little-endian, factor 0.015 V — i.e. 0.01 V units = raw * 1.5. - * (Verified: raw 0x3D9 = 985 -> 14.775 V.) The high nibble of byte 1 is ST_BTSW, - * NOT voltage — mask it off or you'll read ~4x too high. + * (Source example: raw 0x3D9 = 985 -> 14.775 V.) + * + * CAVEAT: on this bus the field reads BATTERY_RAW_INVALID almost always — the + * Power Module appears not to be retained in the swap. Check can_battery_raw() + * against BATTERY_RAW_INVALID before calling this; 0xFFF would otherwise decode + * to a confident-looking 61 V. The factor itself is also unconfirmed against a + * meter (0.015 here vs ~0.0147 implied by the E60 VFL sheet). + * See wiki/pages/can-id-candidates.md. + * * @param d Payload of a 0x3B4 frame (needs DLC >= 2) * @return Voltage in 0.01V units */ static inline uint16_t can_to_voltage(const uint8_t* d) { - uint16_t raw12 = (uint16_t)(((d[CAN_BYTE_BATTERY_VOLT_H] & 0x0F) << 8) | d[CAN_BYTE_BATTERY_VOLT_L]); - return (uint16_t)((raw12 * 3) / 2); // raw * 0.015 V -> 0.01 V units + return (uint16_t)((can_battery_raw(d) * 3) / 2); // raw * 0.015 V -> 0.01 V units } /** diff --git a/src/drivers/can_driver.cpp b/src/drivers/can_driver.cpp index 151d453..dc741df 100644 --- a/src/drivers/can_driver.cpp +++ b/src/drivers/can_driver.cpp @@ -212,10 +212,23 @@ static bool parseCANMessage(const twai_message_t& message) { break; case CAN_ID_BATTERY_VOLTAGE: - // 0x3B4: U_BT, 12-bit little-endian x 0.015 V. + // 0x3B4: U_BT (12-bit LE x 0.015 V) + ignition state. if (dlc > CAN_BYTE_BATTERY_VOLT_H) { - g_automotive_data.battery_voltage = can_to_voltage(data); - g_automotive_data.valid_flags |= VALID_FLAG_BATTERY_VOLT; + // The field reads 0xFFF (not-available) on nearly every frame of + // this bus. Publishing that would put a confident 61 V on the + // display, so only accept real readings and leave the valid flag + // clear otherwise. + if (can_battery_raw(data) != BATTERY_RAW_INVALID) { + g_automotive_data.battery_voltage = can_to_voltage(data); + g_automotive_data.valid_flags |= VALID_FLAG_BATTERY_VOLT; + } + parsed = true; + } + if (dlc > CAN_BYTE_IGNITION) { + // Byte 2's low nibble tracks ignition (inverted), NOT engine run + // — verified on this bus against both published claims. + g_driveline.ignition_on = + ((data[CAN_BYTE_IGNITION] & 0x0F) == IGNITION_ON); parsed = true; } break; diff --git a/src/main.cpp b/src/main.cpp index 6cb2814..a31935e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "modules/settings_manager.h" // Settings manager module #include "modules/ui_state_manager.h" // UI state manager module #include "modules/meters_manager.h" // Meters display manager module +#include "modules/driveline_screen.h" // Hand-coded driveline screen (swipe right from Home) #include "modules/serial_lock.h" // Global serial write mutex // ============================================================================ @@ -255,6 +256,12 @@ void loop() // (This survives SquareLine Studio regeneration) initializeHomeScreenMeters(); + // Bolt the right-swipe -> Driveline screen onto Home at runtime. Same reason + // as the meters above: doing it here rather than in ui_Home_Screen.c means a + // SquareLine re-export can't silently remove it. + DrivelineScreen::attachHomeGesture(); + DrivelineScreen::tick(); + // Update meter values from CAN data every 200ms (5Hz) if (millis() - last_meter_update >= 200) { last_meter_update = millis(); @@ -265,21 +272,38 @@ void loop() int16_t engine_oil_temp = UARTManager::g_automotive_data.engine_oil_temp; int16_t gearbox_oil_temp = UARTManager::g_automotive_data.gearbox_oil_temp; uint16_t battery_voltage = UARTManager::g_automotive_data.battery_voltage; + uint8_t valid = UARTManager::g_automotive_data.valid_flags; xSemaphoreGive(UARTManager::g_data_mutex); // Update meters and labels (LVGL port lock required for thread safety) lvgl_port_lock(-1); - // Update temperature meters - meters_manager_update_coolant_temp(coolant_temp); - meters_manager_update_engine_oil_temp(engine_oil_temp); - meters_manager_update_gearbox_oil_temp(gearbox_oil_temp); + // Only show a value once its signal has actually been received. These + // fields are pre-seeded with DEFAULT_* constants, so updating them + // unconditionally displayed a plausible 92.0degC engine oil temp and a + // 12.45V battery that were never measured — inventions, not readings. + // Two of these have no source on this bus at all: engine oil temp has + // no sensor (0x1D0 byte 1 is permanently 0xFF) and 0x3B4 publishes the + // battery's not-available marker on nearly every frame. + if (valid & VALID_FLAG_COOLANT_TEMP) { + meters_manager_update_coolant_temp(coolant_temp); + } + if (valid & VALID_FLAG_ENGINE_TEMP) { + meters_manager_update_engine_oil_temp(engine_oil_temp); + } + if (valid & VALID_FLAG_GEARBOX_TEMP) { + meters_manager_update_gearbox_oil_temp(gearbox_oil_temp); + } // Update battery voltage label (format: "12.45V") if (uic_Batery_Values_LBL != NULL) { char battery_buf[16]; - snprintf(battery_buf, sizeof(battery_buf), "%d.%02d", - battery_voltage / 100, battery_voltage % 100); + if (valid & VALID_FLAG_BATTERY_VOLT) { + snprintf(battery_buf, sizeof(battery_buf), "%d.%02d", + battery_voltage / 100, battery_voltage % 100); + } else { + snprintf(battery_buf, sizeof(battery_buf), "--.--"); + } lv_label_set_text(uic_Batery_Values_LBL, battery_buf); } diff --git a/src/modules/driveline_screen.cpp b/src/modules/driveline_screen.cpp new file mode 100644 index 0000000..ffab9fe --- /dev/null +++ b/src/modules/driveline_screen.cpp @@ -0,0 +1,365 @@ +/* + * Driveline Screen Module Implementation + */ + +#include "driveline_screen.h" +#include "uart_manager.h" +#include "display/lvgl_v8_port.h" +#include "drivers/can_driver.h" +#include "esp_log.h" + +extern "C" { +#include "ui.h" +} + +namespace DrivelineScreen { + +// ============================================================================ +// LAYOUT / THEME CONSTANTS +// ============================================================================ + +#define COL_BG lv_color_hex(0x101418) +#define COL_PANEL lv_color_hex(0x1B2128) +#define COL_TEXT lv_color_hex(0xE8EDF2) +#define COL_MUTED lv_color_hex(0x6E7B88) +#define COL_ACCENT lv_color_hex(0x3DA9FC) +#define COL_OK lv_color_hex(0x3DD68C) +#define COL_WARN lv_color_hex(0xE5484D) + +#define RPM_MAX 5000 // M57N redline is ~4500; a little headroom + +// ============================================================================ +// MODULE STATE +// ============================================================================ + +static lv_obj_t* g_screen = NULL; +static lv_obj_t* g_gear_lbl = NULL; +static lv_obj_t* g_gear_ring = NULL; +static lv_obj_t* g_coolant_lbl = NULL; +static lv_obj_t* g_gearbox_lbl = NULL; +static lv_obj_t* g_rpm_bar = NULL; +static lv_obj_t* g_rpm_lbl = NULL; +static lv_obj_t* g_turbine_lbl = NULL; +static lv_obj_t* g_shaft_lbl = NULL; +static lv_obj_t* g_ign_chip = NULL; +static lv_obj_t* g_eng_chip = NULL; +static lv_obj_t* g_clutch_chip = NULL; +static lv_obj_t* g_cool_chip = NULL; +static lv_obj_t* g_mismatch_lbl = NULL; + +// Home screen instance we've already hooked the right-swipe onto. +static lv_obj_t* g_hooked_home = NULL; + +// ============================================================================ +// PRIVATE HELPERS +// ============================================================================ + +/** + * @brief Create a small status chip (rounded pill with text). + */ +static lv_obj_t* makeChip(lv_obj_t* parent, const char* text) { + lv_obj_t* chip = lv_label_create(parent); + lv_label_set_text(chip, text); + lv_obj_set_style_text_font(chip, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(chip, COL_MUTED, 0); + lv_obj_set_style_bg_color(chip, COL_PANEL, 0); + lv_obj_set_style_bg_opa(chip, LV_OPA_COVER, 0); + lv_obj_set_style_radius(chip, 10, 0); + lv_obj_set_style_pad_hor(chip, 10, 0); + lv_obj_set_style_pad_ver(chip, 5, 0); + return chip; +} + +/** + * @brief Set a chip's text and colour in one go. + */ +static void setChip(lv_obj_t* chip, const char* text, lv_color_t colour) { + if (chip == NULL) return; + lv_label_set_text(chip, text); + lv_obj_set_style_text_color(chip, colour, 0); +} + +/** + * @brief Create a title + value stack. Returns the value label. + */ +static lv_obj_t* makeStat(lv_obj_t* parent, const char* title, + const lv_font_t* value_font, + lv_align_t align, lv_coord_t x, lv_coord_t y) { + lv_obj_t* box = lv_obj_create(parent); + lv_obj_remove_style_all(box); + lv_obj_set_size(box, LV_SIZE_CONTENT, LV_SIZE_CONTENT); + lv_obj_set_flex_flow(box, LV_FLEX_FLOW_COLUMN); + lv_obj_set_flex_align(box, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); + lv_obj_align(box, align, x, y); + lv_obj_clear_flag(box, LV_OBJ_FLAG_SCROLLABLE); + + lv_obj_t* t = lv_label_create(box); + lv_label_set_text(t, title); + lv_obj_set_style_text_font(t, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(t, COL_MUTED, 0); + lv_obj_set_style_pad_bottom(t, 4, 0); + + lv_obj_t* v = lv_label_create(box); + lv_label_set_text(v, "--"); + lv_obj_set_style_text_font(v, value_font, 0); + lv_obj_set_style_text_color(v, COL_TEXT, 0); + return v; +} + +/** + * @brief Gesture handler on OUR screen: swipe right/left goes back to Home. + */ +static void onScreenGesture(lv_event_t* e) { + if (lv_event_get_code(e) != LV_EVENT_GESTURE) return; + lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act()); + if (dir == LV_DIR_LEFT || dir == LV_DIR_RIGHT) { + lv_indev_wait_release(lv_indev_get_act()); + _ui_screen_change(&ui_Home_Screen, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, + &ui_Home_Screen_screen_init); + } +} + +/** + * @brief Gesture handler we bolt onto the Home screen at runtime. + * + * The generated ui_event_Home_Screen() already claims LEFT (-> Settings). LVGL + * dispatches to every registered callback, so this one coexists with it rather + * than replacing it — which is the whole point: we never edit the generated file. + */ +static void onHomeGesture(lv_event_t* e) { + if (lv_event_get_code(e) != LV_EVENT_GESTURE) return; + if (lv_indev_get_gesture_dir(lv_indev_get_act()) != LV_DIR_RIGHT) return; + lv_indev_wait_release(lv_indev_get_act()); + show(); +} + +/** + * @brief Build the screen. Idempotent — returns immediately if already built. + */ +static void build() { + if (g_screen != NULL) return; + + g_screen = lv_obj_create(NULL); + lv_obj_set_style_bg_color(g_screen, COL_BG, 0); + lv_obj_clear_flag(g_screen, LV_OBJ_FLAG_SCROLLABLE); + lv_obj_add_event_cb(g_screen, onScreenGesture, LV_EVENT_GESTURE, NULL); + + // ---- top status bar -------------------------------------------------- + lv_obj_t* bar = lv_obj_create(g_screen); + lv_obj_remove_style_all(bar); + lv_obj_set_size(bar, LV_SIZE_CONTENT, LV_SIZE_CONTENT); + lv_obj_set_flex_flow(bar, LV_FLEX_FLOW_ROW); + lv_obj_set_style_pad_column(bar, 8, 0); + lv_obj_align(bar, LV_ALIGN_TOP_LEFT, 24, 18); + lv_obj_clear_flag(bar, LV_OBJ_FLAG_SCROLLABLE); + + g_ign_chip = makeChip(bar, "IGN --"); + g_eng_chip = makeChip(bar, "ENG --"); + g_clutch_chip = makeChip(bar, "CLUTCH --"); + g_cool_chip = makeChip(bar, "COOLING --"); + + g_mismatch_lbl = lv_label_create(g_screen); + lv_label_set_text(g_mismatch_lbl, LV_SYMBOL_WARNING " GEAR MISMATCH"); + lv_obj_set_style_text_font(g_mismatch_lbl, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(g_mismatch_lbl, COL_WARN, 0); + lv_obj_align(g_mismatch_lbl, LV_ALIGN_TOP_RIGHT, -24, 24); + lv_obj_add_flag(g_mismatch_lbl, LV_OBJ_FLAG_HIDDEN); + + // ---- gear hero ------------------------------------------------------- + g_gear_ring = lv_obj_create(g_screen); + lv_obj_set_size(g_gear_ring, 220, 220); + lv_obj_align(g_gear_ring, LV_ALIGN_CENTER, 0, -40); + lv_obj_set_style_radius(g_gear_ring, LV_RADIUS_CIRCLE, 0); + lv_obj_set_style_bg_color(g_gear_ring, COL_PANEL, 0); + lv_obj_set_style_border_color(g_gear_ring, COL_ACCENT, 0); + lv_obj_set_style_border_width(g_gear_ring, 3, 0); + lv_obj_clear_flag(g_gear_ring, LV_OBJ_FLAG_SCROLLABLE); + + g_gear_lbl = lv_label_create(g_gear_ring); + lv_label_set_text(g_gear_lbl, "--"); + lv_obj_set_style_text_font(g_gear_lbl, &lv_font_montserrat_48, 0); + lv_obj_set_style_text_color(g_gear_lbl, COL_TEXT, 0); + lv_obj_center(g_gear_lbl); + + // ---- flanking temperatures ------------------------------------------ + g_coolant_lbl = makeStat(g_screen, "COOLANT", &lv_font_montserrat_40, + LV_ALIGN_LEFT_MID, 110, -40); + g_gearbox_lbl = makeStat(g_screen, "GEARBOX OIL", &lv_font_montserrat_40, + LV_ALIGN_RIGHT_MID, -110, -40); + + // ---- RPM bar --------------------------------------------------------- + lv_obj_t* rpm_title = lv_label_create(g_screen); + lv_label_set_text(rpm_title, "RPM"); + lv_obj_set_style_text_font(rpm_title, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(rpm_title, COL_MUTED, 0); + lv_obj_align(rpm_title, LV_ALIGN_CENTER, -330, 108); + + g_rpm_bar = lv_bar_create(g_screen); + lv_obj_set_size(g_rpm_bar, 560, 16); + lv_obj_align(g_rpm_bar, LV_ALIGN_CENTER, -30, 108); + lv_bar_set_range(g_rpm_bar, 0, RPM_MAX); + lv_bar_set_value(g_rpm_bar, 0, LV_ANIM_OFF); + lv_obj_set_style_bg_color(g_rpm_bar, COL_PANEL, 0); + lv_obj_set_style_bg_color(g_rpm_bar, COL_ACCENT, LV_PART_INDICATOR); + lv_obj_set_style_radius(g_rpm_bar, 8, 0); + lv_obj_set_style_radius(g_rpm_bar, 8, LV_PART_INDICATOR); + + g_rpm_lbl = lv_label_create(g_screen); + lv_label_set_text(g_rpm_lbl, "----"); + lv_obj_set_style_text_font(g_rpm_lbl, &lv_font_montserrat_30, 0); + lv_obj_set_style_text_color(g_rpm_lbl, COL_TEXT, 0); + lv_obj_align(g_rpm_lbl, LV_ALIGN_CENTER, 330, 108); + + // ---- driveline speeds ------------------------------------------------ + g_turbine_lbl = makeStat(g_screen, "TURBINE (rpm)", &lv_font_montserrat_30, + LV_ALIGN_BOTTOM_MID, -180, -34); + // "raw" is not decoration: the output-shaft scaling is still unverified, so + // we deliberately show counts rather than invent a unit. + g_shaft_lbl = makeStat(g_screen, "OUTPUT SHAFT (raw)", &lv_font_montserrat_30, + LV_ALIGN_BOTTOM_MID, 180, -34); + + lv_obj_t* hint = lv_label_create(g_screen); + lv_label_set_text(hint, LV_SYMBOL_LEFT " swipe back to Home"); + lv_obj_set_style_text_font(hint, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(hint, COL_MUTED, 0); + lv_obj_align(hint, LV_ALIGN_BOTTOM_LEFT, 24, -14); + + ESP_LOGI("Driveline", "Driveline screen built"); +} + +// ============================================================================ +// PUBLIC API +// ============================================================================ + +void show() { + build(); + lv_scr_load_anim(g_screen, LV_SCR_LOAD_ANIM_FADE_ON, 100, 0, false); +} + +void attachHomeGesture() { + if (ui_Home_Screen == NULL) { + g_hooked_home = NULL; // screen gone; re-hook when it returns + return; + } + if (g_hooked_home == ui_Home_Screen) return; + + lvgl_port_lock(-1); + lv_obj_add_event_cb(ui_Home_Screen, onHomeGesture, LV_EVENT_GESTURE, NULL); + lvgl_port_unlock(); + + g_hooked_home = ui_Home_Screen; + ESP_LOGI("Driveline", "Right-swipe hooked onto Home screen"); +} + +void tick() { + // Nothing to do unless we're built AND actually the screen being shown. + if (g_screen == NULL || lv_scr_act() != g_screen) return; + + // loop() spins fast and Core 1 is already ~80% busy rendering. Refreshing at + // 10Hz is well past what the eye resolves and keeps us off the LVGL lock. + static uint32_t last_refresh = 0; + if (millis() - last_refresh < 100) return; + last_refresh = millis(); + + bool can_up = CANDriver::isActive(); + driveline_t dl = CANDriver::getDriveline(); + + // Snapshot the shared struct under its own mutex, briefly. + uint8_t gear = GEAR_UNKNOWN; + int16_t coolant = 0, gearbox = 0; + uint8_t flags = 0; + if (xSemaphoreTake(UARTManager::g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) { + gear = UARTManager::g_automotive_data.gear_position; + coolant = UARTManager::g_automotive_data.coolant_temp; + gearbox = UARTManager::g_automotive_data.gearbox_oil_temp; + flags = UARTManager::g_automotive_data.valid_flags; + xSemaphoreGive(UARTManager::g_data_mutex); + } else { + return; // couldn't read; leave the last frame on screen + } + + char buf[24]; + lvgl_port_lock(-1); + + if (!can_up) { + // Be explicit that there is no data, rather than showing a stale frame + // that looks live. + lv_label_set_text(g_gear_lbl, "--"); + lv_label_set_text(g_coolant_lbl, "--"); + lv_label_set_text(g_gearbox_lbl, "--"); + lv_label_set_text(g_rpm_lbl, "----"); + lv_label_set_text(g_turbine_lbl, "--"); + lv_label_set_text(g_shaft_lbl, "--"); + lv_bar_set_value(g_rpm_bar, 0, LV_ANIM_OFF); + setChip(g_ign_chip, "IGN --", COL_MUTED); + setChip(g_eng_chip, "ENG --", COL_MUTED); + setChip(g_clutch_chip, "CLUTCH --", COL_MUTED); + setChip(g_cool_chip, "CAN OFF", COL_WARN); + lv_obj_add_flag(g_mismatch_lbl, LV_OBJ_FLAG_HIDDEN); + lvgl_port_unlock(); + return; + } + + // ---- gear ------------------------------------------------------------ + if (flags & VALID_FLAG_GEAR_POS) { + lv_label_set_text(g_gear_lbl, gear_to_string((gear_position_t)gear)); + } else { + lv_label_set_text(g_gear_lbl, "--"); + } + + // ---- temperatures (only when the signal is real) --------------------- + if (flags & VALID_FLAG_COOLANT_TEMP) { + snprintf(buf, sizeof(buf), "%d.%d°C", coolant / 10, abs(coolant % 10)); + lv_label_set_text(g_coolant_lbl, buf); + } + if (flags & VALID_FLAG_GEARBOX_TEMP) { + snprintf(buf, sizeof(buf), "%d.%d°C", gearbox / 10, abs(gearbox % 10)); + lv_label_set_text(g_gearbox_lbl, buf); + } + + // ---- speeds ---------------------------------------------------------- + snprintf(buf, sizeof(buf), "%u", dl.engine_rpm); + lv_label_set_text(g_rpm_lbl, buf); + lv_bar_set_value(g_rpm_bar, dl.engine_rpm > RPM_MAX ? RPM_MAX : dl.engine_rpm, LV_ANIM_OFF); + + snprintf(buf, sizeof(buf), "%u", dl.turbine_rpm); + lv_label_set_text(g_turbine_lbl, buf); + + snprintf(buf, sizeof(buf), "%u", dl.output_shaft_raw); + lv_label_set_text(g_shaft_lbl, buf); + + // ---- status chips ---------------------------------------------------- + setChip(g_ign_chip, dl.ignition_on ? "IGN ON" : "IGN OFF", + dl.ignition_on ? COL_OK : COL_MUTED); + + switch (dl.engine_run_state) { + case ENGINE_RUN_RUNNING: setChip(g_eng_chip, "ENG RUN", COL_OK); break; + case ENGINE_RUN_STARTING: setChip(g_eng_chip, "ENG START", COL_ACCENT); break; + case ENGINE_RUN_OFF: setChip(g_eng_chip, "ENG OFF", COL_MUTED); break; + default: setChip(g_eng_chip, "ENG ?", COL_MUTED); break; + } + + switch (dl.converter_clutch) { + case CCLT_DISENGAGED: setChip(g_clutch_chip, "CLUTCH OPEN", COL_MUTED); break; + case CCLT_CONTROLLED: setChip(g_clutch_chip, "CLUTCH SLIP", COL_ACCENT); break; + case CCLT_CLOSED: setChip(g_clutch_chip, "CLUTCH LOCK", COL_OK); break; + default: setChip(g_clutch_chip, "CLUTCH ?", COL_MUTED); break; + } + + switch (dl.gearbox_cooling) { + case GEARBOX_COOL_NONE: setChip(g_cool_chip, "COOLING OK", COL_MUTED); break; + case GEARBOX_COOL_STAGE1: setChip(g_cool_chip, "COOLING 1", COL_ACCENT); break; + case GEARBOX_COOL_STAGE2: setChip(g_cool_chip, "COOLING 2", COL_WARN); break; + default: setChip(g_cool_chip, "COOLING ?", COL_MUTED); break; + } + + if (dl.gear_mismatch) { + lv_obj_clear_flag(g_mismatch_lbl, LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_add_flag(g_mismatch_lbl, LV_OBJ_FLAG_HIDDEN); + } + + lvgl_port_unlock(); +} + +} // namespace DrivelineScreen diff --git a/src/modules/driveline_screen.h b/src/modules/driveline_screen.h new file mode 100644 index 0000000..5335d6c --- /dev/null +++ b/src/modules/driveline_screen.h @@ -0,0 +1,49 @@ +/* + * Driveline Screen Module + * + * A hand-coded LVGL screen showing the CAN signals we have actually verified on + * the swap's bus: gear (hero), coolant, gearbox oil temp, engine RPM, turbine + * speed and output shaft, plus ignition / engine / clutch / mismatch status. + * + * WHY THIS IS NOT A SQUARELINE SCREEN + * SquareLine Studio owns include/ui/ and overwrites it on every export. Editing + * generated files loses the edit — that is exactly how the CAN toggle's event + * handler went missing. This screen lives entirely in src/, which SquareLine + * never touches, so it cannot be clobbered. It is reached by a right-swipe + * gesture attached to the Home screen AT RUNTIME (see attachHomeGesture) rather + * than by editing ui_Home_Screen.c, for the same reason. + * + * Signals deliberately NOT shown: engine oil temp (no sensor on this M57N — + * 0x1D0 byte 1 is permanently 0xFF) and battery voltage (0x3B4 publishes the + * invalid marker almost always). See wiki/pages/can-id-candidates.md. + */ + +#pragma once + +#include +#include + +namespace DrivelineScreen { + +/** + * @brief Build the screen (idempotent) and load it. + * @note Caller must hold the LVGL lock. + */ +void show(); + +/** + * @brief Refresh all values from the CAN driver / shared automotive data. + * Cheap no-op unless the screen exists and is currently on display. + * Call from loop(); takes its own locks. + */ +void tick(); + +/** + * @brief Attach a right-swipe handler to the Home screen so this screen is + * reachable, without touching the SquareLine-generated Home screen code. + * Safe to call every loop — re-attaches only when the Home screen + * instance changes. Takes its own LVGL lock. + */ +void attachHomeGesture(); + +} // namespace DrivelineScreen