diff --git a/include/drivers/can_driver.h b/include/drivers/can_driver.h index fc39b6b..1474b12 100644 --- a/include/drivers/can_driver.h +++ b/include/drivers/can_driver.h @@ -90,6 +90,13 @@ typedef struct { bool ignition_on; // 0x3B4 byte 2 low nibble. This bus's only // ignition/terminal signal — CAS (0x130) is not // present in the swap. + uint8_t lever; // 0x1D2 byte 0 as gear_position_t: the selected + // detent (P/R/N/D). GEAR_UNKNOWN before first frame. + uint8_t engaged_gear; // 0x1D2 byte 1: gear the box is actually in, 1..6; + // 0 = none (P/R/N, and R which is unnumbered here). + // Populated in plain D too, not just S/M. + uint8_t gear_mode; // 0x1D2 byte 4 raw: GEAR_MODE_DRIVE_AUTO / _SPORT + // / _MANUAL. Only meaningful when lever == D. bool gear_mismatch; // lever (0x1D2) vs gearbox (0x0BA) disagree, // sustained past the debounce window } driveline_t; diff --git a/src/drivers/can_driver.cpp b/src/drivers/can_driver.cpp index 0527fba..ff34f3b 100644 --- a/src/drivers/can_driver.cpp +++ b/src/drivers/can_driver.cpp @@ -163,10 +163,18 @@ static bool parseCANMessage(const twai_message_t& message) { g_automotive_data.valid_flags |= VALID_FLAG_GEAR_POS; parsed = true; } - // Keep the bare lever (not the resolved display value) for the - // cross-check below: 0x0BA reports a gear, never a mode. + // Break the frame into its pieces for the driveline screen, which + // composes its own richer label (e.g. "D3"). Keep the bare lever for + // the cross-check below: 0x0BA reports a gear, never a mode. if (dlc > CAN_BYTE_GEAR_LEVER) { g_last_lever = can_to_gear_lever(data[CAN_BYTE_GEAR_LEVER]); + g_driveline.lever = (uint8_t)g_last_lever; + } + if (dlc > CAN_BYTE_GEAR_ENGAGED) { + g_driveline.engaged_gear = can_to_engaged_gear(data[CAN_BYTE_GEAR_ENGAGED]); + } + if (dlc > CAN_BYTE_GEAR_MODE) { + g_driveline.gear_mode = data[CAN_BYTE_GEAR_MODE]; } break; } @@ -422,6 +430,9 @@ static void canTask(void* parameter) { memset(&g_can_stats, 0, sizeof(g_can_stats)); // Drop stale driveline/gear state from a previous session memset(&g_driveline, 0, sizeof(g_driveline)); + // GEAR_PARK is 0, so a zeroed lever would read as "P" + // before the first 0x1D2 frame — force UNKNOWN instead. + g_driveline.lever = (uint8_t)GEAR_UNKNOWN; g_last_lever = GEAR_UNKNOWN; g_mismatch_since = 0; ESP_LOGI("CANTask", "CAN reception active"); @@ -575,6 +586,7 @@ driveline_t getDriveline() { // Never hand back a torn read; an all-zero snapshot is honest about // having no fresh data (engine_run_state 0 == ENGINE_RUN_OFF). memset(&snapshot, 0, sizeof(snapshot)); + snapshot.lever = (uint8_t)GEAR_UNKNOWN; // 0 would mean GEAR_PARK } return snapshot; } diff --git a/src/modules/driveline_screen.cpp b/src/modules/driveline_screen.cpp index 4ce9319..0cf5c6c 100644 --- a/src/modules/driveline_screen.cpp +++ b/src/modules/driveline_screen.cpp @@ -80,6 +80,37 @@ static void setChip(lv_obj_t* chip, const char* text, lv_color_t colour) { lv_obj_set_style_text_color(chip, colour, 0); } +/** + * @brief Compose the hero gear label from the 0x1D2 pieces. + * + * P/R/N are shown as the bare letter. In the Drive detent we prefix the engaged + * gear number with the mode: D3 (auto), S3 (sport gate), M3 (manual). If the box + * hasn't reported a number yet (or is in R, which is unnumbered here) the mode + * letter stands alone. + * + * @param dl Driveline snapshot + * @param out Output buffer (>= 4 bytes) + */ +static void composeGear(const driveline_t& dl, char* out, size_t n) { + switch ((gear_position_t)dl.lever) { + case GEAR_PARK: snprintf(out, n, "P"); return; + case GEAR_REVERSE: snprintf(out, n, "R"); return; + case GEAR_NEUTRAL: snprintf(out, n, "N"); return; + case GEAR_DRIVE: break; // fall through to compose D/S/M + number + default: snprintf(out, n, "--"); return; + } + + char mode = 'D'; + if (dl.gear_mode == GEAR_MODE_SPORT) mode = 'S'; + if (dl.gear_mode == GEAR_MODE_MANUAL) mode = 'M'; + + if (dl.engaged_gear >= 1 && dl.engaged_gear <= 6) { + snprintf(out, n, "%c%u", mode, dl.engaged_gear); + } else { + snprintf(out, n, "%c", mode); + } +} + /** * @brief Create a title + value stack. Returns the value label. */ @@ -284,11 +315,13 @@ void tick() { } char buf[24]; + (void)gear; // gear now comes from the driveline pieces, not the UART enum lvgl_port_lock(-1); - if (!can_up) { - // Be explicit that there is no data, rather than showing a stale frame - // that looks live. + // Blank every readout when there's nothing live to show: CAN off, or the key + // is off. The user asked for a full reset on ignition-off so the screen goes + // quiet with the car rather than freezing the last frame. + if (!can_up || !dl.ignition_on) { lv_label_set_text(g_gear_lbl, "--"); lv_label_set_text(g_coolant_lbl, "--"); lv_label_set_text(g_gearbox_lbl, "--"); @@ -297,18 +330,26 @@ void tick() { lv_label_set_text(g_pedal_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); + lv_obj_add_flag(g_mismatch_lbl, LV_OBJ_FLAG_HIDDEN); + // Leave the ignition chip telling the truth; mute the rest, since their + // values are meaningless with the key out (or absent with CAN off). + if (!can_up) { + setChip(g_ign_chip, "IGN --", COL_MUTED); + setChip(g_cool_chip, "CAN OFF", COL_WARN); + } else { + setChip(g_ign_chip, "IGN OFF", COL_MUTED); + setChip(g_cool_chip, "COOLING --", 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)); + // ---- gear (composed: P/R/N, or D/S/M + engaged number) --------------- + if ((gear_position_t)dl.lever != GEAR_UNKNOWN) { + composeGear(dl, buf, sizeof(buf)); + lv_label_set_text(g_gear_lbl, buf); } else { lv_label_set_text(g_gear_lbl, "--"); }