Diff locks on the Driveline: chassis top view after the user's sketch (steered front wheels, prop shaft, rear axle); locked axle amber with an X on its diff, front-only red, caption
Firmware / build (push) Successful in 6m59s

This commit is contained in:
2026-09-29 10:30:46 +02:00
parent 31ca44187e
commit 01f7501ae7
3 changed files with 153 additions and 39 deletions
+150 -36
View File
@@ -5,6 +5,7 @@
#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"
@@ -50,9 +51,8 @@ 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: DIFF LOCK [REAR] [FRONT]
static lv_obj_t* g_rear_pill = NULL;
static lv_obj_t* g_front_pill = 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
@@ -115,36 +115,137 @@ static void setChip(lv_obj_t* chip, const char* text, lv_color_t colour) {
}
}
/** A diff-lock pill: text, fill and text colour, each written only on change. */
static void setPill(lv_obj_t* pill, const char* text, lv_color_t bg, lv_color_t fg) {
setText(pill, text);
if (lv_color_to32(lv_obj_get_style_bg_color(pill, 0)) != lv_color_to32(bg)) {
lv_obj_set_style_bg_color(pill, bg, 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);
}
setColour(pill, fg);
}
/**
* @brief Diff locks, from the isolated inputs (not CAN - shown with CAN off too).
* Locked = amber tell-tale. The front can only lock after the rear, so
* FRONT without REAR is a wiring/switch fault and shows red.
* 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();
if (lv_obj_has_flag(g_diff_box, LV_OBJ_FLAG_HIDDEN) == on) {
if (on) lv_obj_clear_flag(g_diff_box, LV_OBJ_FLAG_HIDDEN);
else lv_obj_add_flag(g_diff_box, LV_OBJ_FLAG_HIDDEN);
}
setHidden(g_diff_box, !on);
if (!on) return;
if (!DiffLocks::isValid()) {
setPill(g_rear_pill, "REAR ?", COL_PANEL, COL_MUTED);
setPill(g_front_pill, "FRONT ?", COL_PANEL, COL_MUTED);
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();
setPill(g_rear_pill, "REAR", rear ? COL_AMBER : COL_PANEL, rear ? COL_BG : COL_MUTED);
if (front && !rear) setPill(g_front_pill, "FRONT", COL_ALERT, COL_TEXT);
else setPill(g_front_pill, "FRONT", front ? COL_AMBER : COL_PANEL, front ? COL_BG : COL_MUTED);
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); }
}
/**
@@ -248,27 +349,40 @@ static void build() {
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)
// ---- 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, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_flex_flow(g_diff_box, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(g_diff_box, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_column(g_diff_box, 8, 0);
lv_obj_align(g_diff_box, LV_ALIGN_TOP_RIGHT, -24, 60);
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);
lv_obj_t* diff_title = lv_label_create(g_diff_box);
lv_label_set_text(diff_title, "DIFF LOCK");
lv_obj_set_style_text_font(diff_title, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(diff_title, COL_MUTED, 0);
g_rear_pill = makeChip(g_diff_box, "REAR");
g_front_pill = makeChip(g_diff_box, "FRONT");
for (lv_obj_t* p : {g_rear_pill, g_front_pill}) {
lv_obj_set_style_text_font(p, &lv_font_montserrat_20, 0); // a tell-tale, bigger than the status chips
lv_obj_set_style_pad_hor(p, 14, 0);
}
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);