Files
ESP32-S3-Touch-LCD-5B/src/modules/driveline_screen.cpp
T

610 lines
26 KiB
C++

/*
* Driveline Screen Module Implementation
*/
#include "driveline_screen.h"
#include "uart_manager.h"
#include <string.h>
#include <math.h>
#include "display/lvgl_v8_port.h"
#include "drivers/can_driver.h"
#include "tuning_screen.h"
#include "settings_screen.h"
#include "diff_locks.h"
#include "esp_log.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 COL_ALERT lv_color_hex(0xFF3B30) // threshold crossed: value + title
#define COL_AMBER lv_color_hex(0xF5A524)
#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_coolant_title = NULL;
static lv_obj_t* g_gearbox_title = NULL;
static lv_obj_t* g_rpm_bar = NULL;
static lv_obj_t* g_rpm_lbl = NULL;
static lv_obj_t* g_torque_lbl = NULL;
static lv_obj_t* g_pedal_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_diff_box = NULL; // top-right: chassis top view, locked axles lit
static lv_obj_t* g_diff_caption = 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 lv_label_set_text() only if the text differs. lv_label_set_text()
* re-lays-out and invalidates the label even when the string is the
* same, and this screen refreshes at 10 Hz - with constant data that was
* ~40 kpx redrawn 10x a second for nothing, a third of core 1 and 50 ms/s
* of PSRAM writes (perf baseline 2026-09-27).
*/
static void setText(lv_obj_t* lbl, const char* text) {
if (lbl == NULL) return;
const char* cur = lv_label_get_text(lbl);
if (cur != NULL && strcmp(cur, text) == 0) return;
lv_label_set_text(lbl, text);
}
/** Text colour, written only on change (a style write invalidates regardless). */
static void setColour(lv_obj_t* o, lv_color_t colour) {
if (o == NULL) return;
if (lv_color_to32(lv_obj_get_style_text_color(o, 0)) != lv_color_to32(colour)) {
lv_obj_set_style_text_color(o, colour, 0);
}
}
/** Ring border colour, written only on change. */
static void setRing(lv_color_t colour) {
if (lv_color_to32(lv_obj_get_style_border_color(g_gear_ring, 0)) != lv_color_to32(colour)) {
lv_obj_set_style_border_color(g_gear_ring, colour, 0);
}
}
/**
* @brief Set a chip's text and colour, touching the object only on a change
* (a style write invalidates the object even for an identical value).
*/
static void setChip(lv_obj_t* chip, const char* text, lv_color_t colour) {
if (chip == NULL) return;
setText(chip, text);
if (lv_color_to32(lv_obj_get_style_text_color(chip, 0)) != lv_color_to32(colour)) {
lv_obj_set_style_text_color(chip, colour, 0);
}
}
// ---- diff-lock drawing ------------------------------------------------------
// A top view of the chassis, after the user's sketch (209 x 285 px, front up):
// steered front wheels on the front axle, prop shaft, rear axle on straight
// wheels. A locked axle lights up (axle, wheels, and an X on its diff). All
// geometry below is in sketch pixels, scaled by DL_S.
static const float DL_S = 0.46f; // sketch -> screen
static const lv_coord_t DL_BOX_W = 150; // wide enough for "REAR + FRONT"
static lv_coord_t dl(float v) { return (lv_coord_t)lroundf(v * DL_S); }
/** Sketch point -> box point (drawing centred horizontally in the box). */
static lv_point_t dlPoint(float x, float y) {
lv_point_t pt;
pt.x = (DL_BOX_W - dl(209)) / 2 + dl(x);
pt.y = dl(y) + 2;
return pt;
}
struct AxleView {
lv_obj_t* axle;
lv_obj_t* wheel[2];
lv_obj_t* cross[2]; // the X on the diff, shown only while locked
};
static AxleView g_front_axle, g_rear_axle;
// lv_line keeps a pointer to its points: they must outlive the objects.
static lv_point_t g_pts_shaft[2];
static lv_point_t g_pts_axle[2][2]; // [front, rear][end]
static lv_point_t g_pts_front_wheel[2][5]; // closed outlines of the steered wheels
static lv_point_t g_pts_cross[2][2][2]; // [front, rear][stroke][end]
static lv_obj_t* makeLine(lv_obj_t* parent, lv_point_t* pts, uint16_t n, lv_coord_t width) {
lv_obj_t* l = lv_line_create(parent);
lv_line_set_points(l, pts, n);
lv_obj_set_style_line_width(l, width, 0);
lv_obj_set_style_line_rounded(l, true, 0);
lv_obj_set_style_line_color(l, COL_MUTED, 0);
lv_obj_clear_flag(l, LV_OBJ_FLAG_CLICKABLE);
return l;
}
/** Steered wheel: a 24 x 76 outline around (cx, cy), top leaning 25 deg to the right. */
static void steeredWheel(lv_point_t* out, float cx, float cy) {
const float a = 12, b = 38, t = 25.0f * (float)M_PI / 180.0f;
const float ux = sinf(t), uy = -cosf(t); // long axis, towards the top
const float vx = cosf(t), vy = sinf(t); // short axis
const float k[4][2] = {{b, a}, {b, -a}, {-b, -a}, {-b, a}};
for (int i = 0; i < 4; i++) {
out[i] = dlPoint(cx + k[i][0] * ux + k[i][1] * vx, cy + k[i][0] * uy + k[i][1] * vy);
}
out[4] = out[0];
}
/** Straight (rear) wheel: rounded outline, sketch x/y of its top-left corner. */
static lv_obj_t* straightWheel(lv_obj_t* parent, float x, float y) {
lv_obj_t* w = lv_obj_create(parent);
lv_obj_remove_style_all(w);
lv_point_t p = dlPoint(x, y);
lv_obj_set_pos(w, p.x, p.y);
lv_obj_set_size(w, dl(24), dl(76));
lv_obj_set_style_radius(w, dl(10), 0);
lv_obj_set_style_border_width(w, 2, 0);
lv_obj_set_style_border_opa(w, LV_OPA_COVER, 0);
lv_obj_set_style_border_color(w, COL_MUTED, 0);
lv_obj_clear_flag(w, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(w, LV_OBJ_FLAG_CLICKABLE);
return w;
}
/** The X on a diff at (cx, cy); idx 0 = front, 1 = rear. */
static void diffCross(lv_obj_t* parent, AxleView& a, int idx, float cx, float cy) {
const float r = 16;
g_pts_cross[idx][0][0] = dlPoint(cx - r, cy - r); g_pts_cross[idx][0][1] = dlPoint(cx + r, cy + r);
g_pts_cross[idx][1][0] = dlPoint(cx + r, cy - r); g_pts_cross[idx][1][1] = dlPoint(cx - r, cy + r);
for (int k = 0; k < 2; k++) {
a.cross[k] = makeLine(parent, g_pts_cross[idx][k], 2, 4);
lv_obj_add_flag(a.cross[k], LV_OBJ_FLAG_HIDDEN);
}
}
/** Line colour for lines, border colour for the wheel boxes; written only on change. */
static void setPartColour(lv_obj_t* o, lv_color_t c) {
if (lv_obj_check_type(o, &lv_line_class)) {
if (lv_color_to32(lv_obj_get_style_line_color(o, 0)) != lv_color_to32(c)) {
lv_obj_set_style_line_color(o, c, 0);
}
} else if (lv_color_to32(lv_obj_get_style_border_color(o, 0)) != lv_color_to32(c)) {
lv_obj_set_style_border_color(o, c, 0);
}
}
static void setHidden(lv_obj_t* o, bool hidden) {
if (lv_obj_has_flag(o, LV_OBJ_FLAG_HIDDEN) == hidden) return;
if (hidden) lv_obj_add_flag(o, LV_OBJ_FLAG_HIDDEN);
else lv_obj_clear_flag(o, LV_OBJ_FLAG_HIDDEN);
}
static void setAxle(AxleView& a, bool locked, lv_color_t colour) {
lv_color_t c = locked ? colour : COL_MUTED;
setPartColour(a.axle, c);
for (int i = 0; i < 2; i++) {
setPartColour(a.wheel[i], c);
setPartColour(a.cross[i], c);
setHidden(a.cross[i], !locked);
}
}
/**
* @brief Diff locks, from the isolated inputs (not CAN - shown with CAN off too).
* Locked axle = amber. The front can only lock after the rear, so FRONT
* without REAR is a wiring/switch fault and shows red.
*/
static void refreshDiffLocks() {
bool on = DiffLocks::isEnabled();
setHidden(g_diff_box, !on);
if (!on) return;
if (!DiffLocks::isValid()) {
setAxle(g_front_axle, false, COL_MUTED);
setAxle(g_rear_axle, false, COL_MUTED);
setText(g_diff_caption, "DIFF LOCK ?");
setColour(g_diff_caption, COL_MUTED);
return;
}
bool rear = DiffLocks::rearLocked(), front = DiffLocks::frontLocked();
setAxle(g_rear_axle, rear, COL_AMBER);
setAxle(g_front_axle, front, rear ? COL_AMBER : COL_ALERT);
if (front && rear) { setText(g_diff_caption, "REAR + FRONT"); setColour(g_diff_caption, COL_AMBER); }
else if (rear) { setText(g_diff_caption, "REAR LOCKED"); setColour(g_diff_caption, COL_AMBER); }
else if (front) { setText(g_diff_caption, "FRONT ONLY"); setColour(g_diff_caption, COL_ALERT); }
else { setText(g_diff_caption, "DIFFS OPEN"); setColour(g_diff_caption, COL_MUTED); }
}
/**
* @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.
*/
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: swipe LEFT opens Settings, swipe RIGHT the Gauge
* Tuning screen (both hand-coded, instant switches).
*/
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) {
lv_indev_wait_release(lv_indev_get_act());
SettingsScreen::show();
} else if (dir == LV_DIR_RIGHT) {
lv_indev_wait_release(lv_indev_get_act());
TuningScreen::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);
// y 60: the top 56 px belong to the StatusOverlay (alert banner centre, Wi-Fi chip right)
lv_obj_align(bar, LV_ALIGN_TOP_LEFT, 24, 60);
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 --");
// Gear mismatch: the ring and the gear letter turn red, and the overlay banner says so.
// ---- diff locks, top-right under the overlay's Wi-Fi chip (hidden until enabled).
// Box bottom stays above the GEARBOX OIL stat (its top is at y ~227).
g_diff_box = lv_obj_create(g_screen);
lv_obj_remove_style_all(g_diff_box);
lv_obj_set_size(g_diff_box, DL_BOX_W, dl(285) + 30);
lv_obj_align(g_diff_box, LV_ALIGN_TOP_RIGHT, -16, 60);
lv_obj_clear_flag(g_diff_box, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(g_diff_box, LV_OBJ_FLAG_CLICKABLE); // swipes pass through to the screen
lv_obj_add_flag(g_diff_box, LV_OBJ_FLAG_HIDDEN);
g_pts_shaft[0] = dlPoint(106, 45);
g_pts_shaft[1] = dlPoint(106, 234);
makeLine(g_diff_box, g_pts_shaft, 2, 3);
g_pts_axle[0][0] = dlPoint(57, 45); g_pts_axle[0][1] = dlPoint(160, 45);
g_pts_axle[1][0] = dlPoint(57, 234); g_pts_axle[1][1] = dlPoint(159, 234);
g_front_axle.axle = makeLine(g_diff_box, g_pts_axle[0], 2, 3);
g_rear_axle.axle = makeLine(g_diff_box, g_pts_axle[1], 2, 3);
steeredWheel(g_pts_front_wheel[0], 45, 47);
steeredWheel(g_pts_front_wheel[1], 172, 47);
g_front_axle.wheel[0] = makeLine(g_diff_box, g_pts_front_wheel[0], 5, 2);
g_front_axle.wheel[1] = makeLine(g_diff_box, g_pts_front_wheel[1], 5, 2);
g_rear_axle.wheel[0] = straightWheel(g_diff_box, 33, 196);
g_rear_axle.wheel[1] = straightWheel(g_diff_box, 159, 196);
diffCross(g_diff_box, g_front_axle, 0, 106, 45);
diffCross(g_diff_box, g_rear_axle, 1, 106, 234);
g_diff_caption = lv_label_create(g_diff_box);
lv_label_set_text(g_diff_caption, "DIFF LOCK ?");
lv_obj_set_style_text_font(g_diff_caption, &lv_font_montserrat_20, 0);
lv_obj_set_style_text_color(g_diff_caption, COL_MUTED, 0);
lv_obj_align(g_diff_caption, LV_ALIGN_BOTTOM_MID, 0, 0);
// ---- 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);
g_coolant_title = lv_obj_get_child(lv_obj_get_parent(g_coolant_lbl), 0);
g_gearbox_title = lv_obj_get_child(lv_obj_get_parent(g_gearbox_lbl), 0);
// ---- 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);
// ---- bottom row: torque | pedal | output shaft -----------------------
// Turbine speed is intentionally NOT shown: it's a diagnostic (engagement
// proof), not a driver number. Still decoded in the driver.
g_torque_lbl = makeStat(g_screen, "TORQUE (Nm)", &lv_font_montserrat_30,
LV_ALIGN_BOTTOM_MID, -340, -34);
g_pedal_lbl = makeStat(g_screen, "PEDAL (%)", &lv_font_montserrat_30,
LV_ALIGN_BOTTOM_MID, 0, -34);
// Output shaft in rpm via a PROVISIONAL factor (one Launch point). The label
// carries "~" so the screen doesn't present an unlocked scaling as exact.
g_shaft_lbl = makeStat(g_screen, "OUTPUT (~rpm)", &lv_font_montserrat_30,
LV_ALIGN_BOTTOM_MID, 340, -34);
lv_obj_t* hint = lv_label_create(g_screen);
lv_label_set_text(hint, LV_SYMBOL_LEFT " swipe left: Settings | swipe right: Gauge tuning " LV_SYMBOL_RIGHT);
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_NONE, 0, 0, false); // instant: fades stutter at 1024x600
}
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;
uint16_t alerts = 0;
if (xSemaphoreTake(UARTManager::g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
gear = UARTManager::g_automotive_data.gear_lever;
coolant = UARTManager::g_automotive_data.coolant_temp;
gearbox = UARTManager::g_automotive_data.gearbox_oil_temp;
flags = UARTManager::g_automotive_data.valid_flags;
alerts = UARTManager::g_automotive_data.alerts;
xSemaphoreGive(UARTManager::g_data_mutex);
} else {
return; // couldn't read; leave the last frame on screen
}
char buf[24];
(void)gear; // gear now comes from the driveline pieces, not the UART enum
lvgl_port_lock(-1);
refreshDiffLocks(); // hard-wired inputs: live even with CAN or the key off
// 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. 0x3B4 sends its
// ignition nibble on-change, so this reset is effectively instant.
if (!can_up || !dl.ignition_on) {
setText(g_gear_lbl, "--");
setText(g_coolant_lbl, "--");
setText(g_gearbox_lbl, "--");
setText(g_rpm_lbl, "----");
setText(g_torque_lbl, "--");
setText(g_pedal_lbl, "--");
setText(g_shaft_lbl, "--");
lv_bar_set_value(g_rpm_bar, 0, LV_ANIM_OFF);
setRing(COL_ACCENT);
setColour(g_gear_lbl, COL_TEXT);
setColour(g_coolant_lbl, COL_TEXT); setColour(g_coolant_title, COL_MUTED);
setColour(g_gearbox_lbl, COL_TEXT); setColour(g_gearbox_title, COL_MUTED);
// 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);
lvgl_port_unlock();
return;
}
// ---- gear (composed: P/R/N, or D/S/M + engaged number) ---------------
if ((gear_position_t)dl.lever != GEAR_UNKNOWN) {
composeGear(dl, buf, sizeof(buf));
setText(g_gear_lbl, buf);
} else {
setText(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));
setText(g_coolant_lbl, buf);
}
if (flags & VALID_FLAG_GEARBOX_TEMP) {
snprintf(buf, sizeof(buf), "%d.%d°C", gearbox / 10, abs(gearbox % 10));
setText(g_gearbox_lbl, buf);
}
// ---- thresholds crossed: the value AND its title go red (amber while the
// box only asks for cooling); the overlay banner blinks on top of it all.
if (!(flags & VALID_FLAG_ALERTS)) alerts = 0;
bool cool_hot = (alerts & ALERT_COOLANT_HOT) != 0;
bool gb_hot = (alerts & ALERT_GEARBOX_HOT) != 0;
bool gb_cool = (alerts & ALERT_GEARBOX_COOLING) != 0;
setColour(g_coolant_lbl, cool_hot ? COL_ALERT : COL_TEXT);
setColour(g_coolant_title, cool_hot ? COL_ALERT : COL_MUTED);
setColour(g_gearbox_lbl, gb_hot ? COL_ALERT : gb_cool ? COL_AMBER : COL_TEXT);
setColour(g_gearbox_title, gb_hot ? COL_ALERT : gb_cool ? COL_AMBER : COL_MUTED);
// ---- speeds ----------------------------------------------------------
snprintf(buf, sizeof(buf), "%u", dl.engine_rpm);
setText(g_rpm_lbl, buf);
lv_bar_set_value(g_rpm_bar, dl.engine_rpm > RPM_MAX ? RPM_MAX : dl.engine_rpm, LV_ANIM_OFF);
// Torque is signed (negative under overrun/braking); one decimal, matching
// the 0.5 Nm resolution of the field.
snprintf(buf, sizeof(buf), "%d.%d", dl.engine_torque_x10 / 10, abs(dl.engine_torque_x10 % 10));
setText(g_torque_lbl, buf);
snprintf(buf, sizeof(buf), "%u", dl.pedal_percent);
setText(g_pedal_lbl, buf);
// Provisional rpm from the single-point Launch factor (see automotive_data.h).
snprintf(buf, sizeof(buf), "%u", can_output_shaft_rpm(dl.output_shaft_raw));
setText(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;
}
// Gear mismatch: the hero ring and the gear letter turn red.
setRing(dl.gear_mismatch ? COL_ALERT : COL_ACCENT);
setColour(g_gear_lbl, dl.gear_mismatch ? COL_ALERT : COL_TEXT);
lvgl_port_unlock();
}
} // namespace DrivelineScreen