3 Commits

11 changed files with 161 additions and 88 deletions
+2 -2
View File
@@ -144,7 +144,7 @@ include/
Settings persist across power cycles via ESP32 NVS (Non-Volatile Storage):
- Namespace: "settings"
- Keys: `wifi_en`, `uart_en`, `can_en`, `debug_en`, `difflock_en` (boolean values)
- Keys: `wifi_en`, `uart_en`, `can_en`, `debug_en`, `difflock_en`, `difflock_inv` (boolean values)
- Functions: `saveWiFiState()`, `loadWiFiState()`, `saveUARTState()`, `loadUARTState()`, `saveCANState()`, `loadCANState()`, `saveDebugState()`, `loadDebugState()`
- Namespace "wifi": `ssid`, `pass` - the one remembered network (`WiFiManager`); the password is never printed
@@ -234,7 +234,7 @@ Settings persist across power cycles via ESP32 NVS (Non-Volatile Storage):
### Diff-lock inputs (`difflock` console)
`src/modules/diff_locks.cpp` reads the Patrol's two diff-lock signals (+12 V = locked) on the isolated inputs: DI0 = rear, DI1 = front, `DI_COM` to vehicle ground. The PC814 outputs reach the ESP32 only through the CH422G (DI0 = EXIO0, DI1 = EXIO5, active LOW), and the CH422G's IO0-7 are one bank, all in or all out - the same bank drives touch reset (EXIO1), backlight (EXIO2) and LCD reset (EXIO3). Each poll (4 Hz, under the LVGL lock so no touch read shares the bus) writes WR_SET input (0x24 <- 0x00), reads 0x26, and writes output back (0x01), restore retried; the chip keeps its output latch. Raw I2C on host 0 (legacy driver, as the panel library), never the library's `enableAllIO_Input()` (it sleeps 2 ms with the bank floating). A state counts after two equal reads. Behind the Settings → System switch, default OFF, so a board without the wiring never flips the bank. The Driveline shows a top view of the chassis in the top-right corner, drawn from the user's sketch with `lv_line` objects and two border boxes, geometry in sketch pixels scaled by `DL_S`. A locked axle turns amber, with its wheels and an X on its diff. The front axle turns red if it reads locked without the rear. A caption below reads DIFFS OPEN, REAR LOCKED, REAR + FRONT or FRONT ONLY. The whole drawing is hidden when the switch is off; the System row's sub-line shows the live state for testing. Wiring and evidence: wiki `diff-lock-inputs`.
`src/modules/diff_locks.cpp` reads the Patrol's two diff-lock signals (+12 V = locked) on the isolated inputs: DI0 = rear, DI1 = front, `DI_COM` to vehicle ground. The PC814 outputs reach the ESP32 only through the CH422G (DI0 = EXIO0, DI1 = EXIO5, active LOW), and the CH422G's IO0-7 are one bank, all in or all out - the same bank drives touch reset (EXIO1), backlight (EXIO2) and LCD reset (EXIO3). Each poll (4 Hz, under the LVGL lock so no touch read shares the bus) writes WR_SET input (0x24 <- 0x00), reads 0x26, and writes output back (0x01), restore retried; the chip keeps its output latch. Raw I2C on host 0 (legacy driver, as the panel library), never the library's `enableAllIO_Input()` (it sleeps 2 ms with the bank floating). A state counts after two equal reads. Polarity is a per-board setting (`difflock_inv`, the "Inverted" switch in the same Settings row, `difflock invert on|off`): in the car the lock wires carry 12 V while UNLOCKED, so the car board needs it on, the bench (12 V = locked) needs it off. Behind the Settings → System switch, default OFF, so a board without the wiring never flips the bank. The Driveline shows a top view of the chassis in the top-right corner, drawn from the user's sketch with `lv_line` objects and two border boxes, geometry in sketch pixels scaled by `DL_S`. All four tyres are the same square-cornered rectangle, and the wheelbase is the sketch's shortened by 20 %. A locked axle turns amber: axle, tyres filled solid, and an X on its diff. A filled tyre is one `lv_line` as wide as the tyre with square ends, because LVGL 8 cannot rotate a box. The front axle turns red if it reads locked without the rear. There is no text caption (user). The whole drawing is hidden when the switch is off; the System row's sub-line shows the live state for testing. Wiring and evidence: wiki `diff-lock-inputs`.
### Screens
+2 -1
View File
@@ -136,8 +136,9 @@ diag status
```
difflock status rear / front LOCKED or open, raw CH422G IO byte, read counters
difflock on | off same as the switch (saved)
difflock invert on | off polarity, same as the "Inverted" switch (saved): on = NO current means locked
```
Wiring: lock wire (+12 V when locked) → DI0 / DI1, `DI_COM` → vehicle ground. Details: wiki `diff-lock-inputs`.
Wiring: lock wire → DI0 / DI1, `DI_COM` → vehicle ground. If the screen shows locked when unlocked (the Patrol's ground-switched lamp wires do this), turn **Inverted** on. Details: wiki `diff-lock-inputs`.
### Diagnostics
```
+1 -1
View File
@@ -109,7 +109,7 @@ void setup()
bool saved_uart_state = SettingsManager::loadUARTState();
bool saved_can_state = SettingsManager::loadCANState();
bool saved_debug_state = SettingsManager::loadDebugState();
DiffLocks::init(SettingsManager::loadDiffLockState()); // CH422G bus is up since DisplayInit
DiffLocks::init(SettingsManager::loadDiffLockState(), SettingsManager::loadDiffLockInvert()); // CH422G bus is up since DisplayInit
ESP_LOGI("MAIN", "Saved states loaded (UI will be updated when Settings screen opens)");
// Initialize UI State Manager
+21 -7
View File
@@ -32,6 +32,7 @@ static const uint32_t POLL_MS = 250;
static const uint8_t FAULT_AFTER = 3; // failed reads in a row
static bool g_enabled = false;
static bool g_inverted = false;
static bool g_rear = false, g_front = false; // debounced
static bool g_have_state = false;
static uint8_t g_last_raw = 0xFF;
@@ -72,9 +73,11 @@ static bool readBank(uint8_t* out) {
return r == ESP_OK;
}
void init(bool enabled) {
void init(bool enabled, bool inverted) {
g_enabled = enabled;
print("[difflock] %s (DI0 = rear, DI1 = front)", enabled ? "ON" : "OFF - Settings > System to enable");
g_inverted = inverted;
print("[difflock] %s (DI0 = rear, DI1 = front, %s)", enabled ? "ON" : "OFF - Settings > System to enable",
inverted ? "INVERTED: no current = locked" : "current = locked");
}
void setEnabled(bool enabled) {
@@ -86,6 +89,14 @@ void setEnabled(bool enabled) {
SettingsManager::saveDiffLockState(enabled);
}
void setInverted(bool inverted) {
if (inverted == g_inverted) return;
g_inverted = inverted;
g_have_state = g_have_raw = false; // re-evaluate from the next two reads
SettingsManager::saveDiffLockInvert(inverted);
}
bool isInverted() { return g_inverted; }
bool isEnabled() { return g_enabled; }
bool isValid() { return g_enabled && g_have_state && g_fail_run < FAULT_AFTER; }
bool rearLocked() { return isValid() && g_rear; }
@@ -112,8 +123,9 @@ void tick() {
// Accept a state only when two reads in a row agree (<= 0.5 s latency).
uint8_t bits = raw & (BIT_REAR | BIT_FRONT);
if (g_have_raw && bits == (g_last_raw & (BIT_REAR | BIT_FRONT))) {
bool rear = !(bits & BIT_REAR); // opto conducting = pin low = locked
bool front = !(bits & BIT_FRONT);
// opto conducting = pin low; that is "locked" unless the polarity is inverted
bool rear = !(bits & BIT_REAR) != g_inverted;
bool front = !(bits & BIT_FRONT) != g_inverted;
if (!g_have_state || rear != g_rear || front != g_front) {
g_rear = rear;
g_front = front;
@@ -133,16 +145,18 @@ bool handleCommand(const char* line) {
if (!strcmp(a, "on")) { setEnabled(true); print("[difflock] ON (saved)"); return true; }
if (!strcmp(a, "off")) { setEnabled(false); print("[difflock] OFF (saved)"); return true; }
if (!strcmp(a, "invert on")) { setInverted(true); print("[difflock] INVERTED: no current = locked (saved)"); return true; }
if (!strcmp(a, "invert off")) { setInverted(false); print("[difflock] normal: current (+12 V) = locked (saved)"); return true; }
if (!*a || !strcmp(a, "status")) {
if (!g_enabled) { print("[difflock] OFF - inputs not read ('difflock on' or Settings > System)"); return true; }
print("[difflock] rear %s, front %s | raw IO0-7 0x%02X | %lu reads, %lu failed%s",
print("[difflock] rear %s, front %s | %s | raw IO0-7 0x%02X | %lu reads, %lu failed%s",
!isValid() ? "?" : g_rear ? "LOCKED" : "open",
!isValid() ? "?" : g_front ? "LOCKED" : "open",
g_last_raw, (unsigned long)g_reads, (unsigned long)g_fails,
g_inverted ? "inverted" : "normal", g_last_raw, (unsigned long)g_reads, (unsigned long)g_fails,
g_fail_run >= FAULT_AFTER ? " | READ FAULT" : "");
return true;
}
print("[difflock] usage: difflock status | on | off (DI0 = rear, DI1 = front, 12 V = locked)");
print("[difflock] usage: difflock status | on | off | invert on|off (DI0 = rear, DI1 = front)");
return true;
}
+11 -2
View File
@@ -18,12 +18,21 @@
namespace DiffLocks {
/** Call once after DisplayInit (the CH422G's I2C bus is installed by the panel library). */
void init(bool enabled);
void init(bool enabled, bool inverted);
/** Settings switch / `difflock on|off`. Saved to NVS (SettingsManager). */
void setEnabled(bool enabled);
bool isEnabled();
/**
* Polarity. Normal: current through the input (+12 V on DINx) = locked.
* Inverted: NO current = locked - the Patrol's ground-switched lock lamps give
* 12 V through the lamp while unlocked and 0 V once the diff switch closes
* (seen in the car, 2026-09-29). Saved to NVS.
*/
void setInverted(bool inverted);
bool isInverted();
/** Poll the inputs (4 Hz, debounced over two reads). Call from loop(). */
void tick();
@@ -32,7 +41,7 @@ bool isValid();
bool rearLocked();
bool frontLocked();
/** `difflock status | on | off`. Returns true if the line was ours. */
/** `difflock status | on | off | invert on|off`. Returns true if the line was ours. */
bool handleCommand(const char* line);
} // namespace DiffLocks
+69 -69
View File
@@ -52,7 +52,6 @@ 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
@@ -116,27 +115,40 @@ static void setChip(lv_obj_t* chip, const char* text, lv_color_t colour) {
}
// ---- 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.
// A top view of the chassis, after the user's sketch (front up): steered front
// tyres on the front axle, prop shaft, rear axle on straight tyres. All four
// tyres are the same square-cornered 24 x 76 rectangle. A locked axle lights up:
// axle, tyres filled, and an X on its diff. Geometry is in sketch pixels, scaled
// by DL_S. Wheelbase 151 = the sketch's 189 shortened by 20 % (user, 2026-09-29).
static const float DL_S = 0.46f; // sketch -> screen
static const lv_coord_t DL_BOX_W = 150; // wide enough for "REAR + FRONT"
static const float DL_S = 0.46f; // sketch -> screen
static const lv_coord_t DL_BOX_W = 110; // hugs the drawing (dl(DL_W) = 98)
static const float DL_W = 212; // sketch width the drawing is centred in
static const float DL_SHAFT_X = 106;
static const float DL_FRONT_Y = 45;
static const float DL_REAR_Y = DL_FRONT_Y + 151;
static const float DL_TRACK_L = 45, DL_TRACK_R = 167; // tyre centres, symmetric about the shaft
static const float DL_TYRE_W = 24, DL_TYRE_L = 76;
static const float DL_STEER = 25; // front tyres, degrees, top leaning right
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.x = (DL_BOX_W - dl(DL_W)) / 2 + dl(x);
pt.y = dl(y) + 2;
return pt;
}
struct TyreView {
lv_obj_t* outline;
lv_obj_t* fill; // shown only while the axle is locked
};
struct AxleView {
lv_obj_t* axle;
lv_obj_t* wheel[2];
TyreView tyre[2];
lv_obj_t* cross[2]; // the X on the diff, shown only while locked
};
static AxleView g_front_axle, g_rear_axle;
@@ -144,45 +156,43 @@ 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_tyre[2][2][5]; // [front, rear][left, right] closed outline
static lv_point_t g_pts_fill[2][2][2]; // [front, rear][left, right] centre line of the fill
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) {
static lv_obj_t* makeLine(lv_obj_t* parent, lv_point_t* pts, uint16_t n, lv_coord_t width, bool rounded) {
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_rounded(l, rounded, 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;
/**
* A tyre centred on (cx, cy), `steer` degrees from straight (top leaning right).
* The fill is ONE line as wide as the tyre with square ends, which is exactly
* the filled rectangle, rotated or not (LVGL 8 cannot rotate a plain box).
*/
static TyreView makeTyre(lv_obj_t* parent, lv_point_t* outline, lv_point_t* fill,
float cx, float cy, float steer) {
const float a = DL_TYRE_W / 2, b = DL_TYRE_L / 2, t = steer * (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);
outline[i] = dlPoint(cx + k[i][0] * ux + k[i][1] * vx, cy + k[i][0] * uy + k[i][1] * vy);
}
out[4] = out[0];
}
outline[4] = outline[0];
fill[0] = dlPoint(cx + b * ux, cy + b * uy);
fill[1] = dlPoint(cx - b * ux, cy - b * uy);
/** 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;
TyreView v;
v.fill = makeLine(parent, fill, 2, dl(DL_TYRE_W), false);
lv_obj_add_flag(v.fill, LV_OBJ_FLAG_HIDDEN);
v.outline = makeLine(parent, outline, 5, 2, true); // 2 px rounded joins read as square corners
return v;
}
/** The X on a diff at (cx, cy); idx 0 = front, 1 = rear. */
@@ -191,19 +201,15 @@ static void diffCross(lv_obj_t* parent, AxleView& a, int idx, float cx, float cy
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);
a.cross[k] = makeLine(parent, g_pts_cross[idx][k], 2, 4, true);
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. */
/** Line colour, 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);
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);
}
}
@@ -217,7 +223,9 @@ 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.tyre[i].outline, c);
setPartColour(a.tyre[i].fill, c);
setHidden(a.tyre[i].fill, !locked);
setPartColour(a.cross[i], c);
setHidden(a.cross[i], !locked);
}
@@ -235,17 +243,11 @@ static void refreshDiffLocks() {
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); }
setAxle(g_front_axle, front, rear ? COL_AMBER : COL_ALERT); // no caption: the drawing says it all (user)
}
/**
@@ -353,36 +355,34 @@ static void build() {
// 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_set_size(g_diff_box, DL_BOX_W, dl(DL_REAR_Y + DL_TYRE_L / 2) + 6); // drawing only, no caption
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_shaft[0] = dlPoint(DL_SHAFT_X, DL_FRONT_Y);
g_pts_shaft[1] = dlPoint(DL_SHAFT_X, DL_REAR_Y);
makeLine(g_diff_box, g_pts_shaft, 2, 3, true);
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);
// axles run between the tyres' inner edges (a steered tyre's edge sits a little further out)
const float half = DL_TYRE_W / 2;
const float half_steered = half / cosf(DL_STEER * (float)M_PI / 180.0f);
g_pts_axle[0][0] = dlPoint(DL_TRACK_L + half_steered, DL_FRONT_Y);
g_pts_axle[0][1] = dlPoint(DL_TRACK_R - half_steered, DL_FRONT_Y);
g_pts_axle[1][0] = dlPoint(DL_TRACK_L + half, DL_REAR_Y);
g_pts_axle[1][1] = dlPoint(DL_TRACK_R - half, DL_REAR_Y);
g_front_axle.axle = makeLine(g_diff_box, g_pts_axle[0], 2, 3, true);
g_rear_axle.axle = makeLine(g_diff_box, g_pts_axle[1], 2, 3, true);
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);
g_front_axle.tyre[0] = makeTyre(g_diff_box, g_pts_tyre[0][0], g_pts_fill[0][0], DL_TRACK_L, DL_FRONT_Y, DL_STEER);
g_front_axle.tyre[1] = makeTyre(g_diff_box, g_pts_tyre[0][1], g_pts_fill[0][1], DL_TRACK_R, DL_FRONT_Y, DL_STEER);
g_rear_axle.tyre[0] = makeTyre(g_diff_box, g_pts_tyre[1][0], g_pts_fill[1][0], DL_TRACK_L, DL_REAR_Y, 0);
g_rear_axle.tyre[1] = makeTyre(g_diff_box, g_pts_tyre[1][1], g_pts_fill[1][1], DL_TRACK_R, DL_REAR_Y, 0);
diffCross(g_diff_box, g_front_axle, 0, 106, 45);
diffCross(g_diff_box, g_rear_axle, 1, 106, 234);
diffCross(g_diff_box, g_front_axle, 0, DL_SHAFT_X, DL_FRONT_Y);
diffCross(g_diff_box, g_rear_axle, 1, DL_SHAFT_X, DL_REAR_Y);
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);
+15
View File
@@ -20,6 +20,7 @@ static const char* NVS_KEY_UART_ENABLED = "uart_en";
static const char* NVS_KEY_DEBUG_ENABLED = "debug_en";
static const char* NVS_KEY_CAN_ENABLED = "can_en";
static const char* NVS_KEY_DIFFLOCK_ENABLED = "difflock_en";
static const char* NVS_KEY_DIFFLOCK_INVERT = "difflock_inv";
// ============================================================================
// PUBLIC API IMPLEMENTATION
@@ -104,4 +105,18 @@ bool loadDiffLockState() {
return enabled;
}
void saveDiffLockInvert(bool inverted) {
preferences.begin(NVS_NAMESPACE, false); // read-write mode
preferences.putBool(NVS_KEY_DIFFLOCK_INVERT, inverted);
preferences.end();
Serial.printf("[SettingsManager] Saved DiffLock polarity = %s\n", inverted ? "INVERTED" : "normal");
}
bool loadDiffLockInvert() {
preferences.begin(NVS_NAMESPACE, true); // read-only mode
bool inverted = preferences.getBool(NVS_KEY_DIFFLOCK_INVERT, false); // default: current = locked
preferences.end();
return inverted;
}
} // namespace SettingsManager
+4
View File
@@ -70,4 +70,8 @@ bool loadCANState();
void saveDiffLockState(bool enabled);
bool loadDiffLockState();
/** Diff-lock polarity: true = no current means locked. Default false. */
void saveDiffLockInvert(bool inverted);
bool loadDiffLockInvert();
} // namespace SettingsManager
+23 -2
View File
@@ -84,6 +84,7 @@ static lv_obj_t* g_sys_can = NULL;
static lv_obj_t* g_sys_link = NULL;
static lv_obj_t* g_sys_dbg = NULL;
static lv_obj_t* g_sys_diff = NULL;
static lv_obj_t* g_sys_diff_inv = NULL; // polarity: no current = locked
static lv_obj_t* g_sys_diff_sub = NULL; // sub-line of the diff-lock row: live state
static lv_obj_t* g_sys_info = NULL; // values column; keys are static
@@ -776,13 +777,21 @@ static void onDiffSwitch(lv_event_t* e) {
DiffLocks::setEnabled(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED));
}
static void onDiffInvertSwitch(lv_event_t* e) {
if (g_refreshing) return;
DiffLocks::setInverted(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED));
}
static void refreshSystem(bool force) {
setChecked(g_sys_can, CANDriver::isActive());
setChecked(g_sys_link, UARTManager::getState() != UARTManager::UART_STATE_OFF);
setChecked(g_sys_dbg, UIStateManager::isDebugEnabled());
setChecked(g_sys_diff, DiffLocks::isEnabled());
setChecked(g_sys_diff_inv, DiffLocks::isInverted());
// live state in the sub-line, so the wiring can be tested from this page
if (!DiffLocks::isEnabled()) setText(g_sys_diff_sub, "DI0 rear, DI1 front - 12 V = locked");
if (!DiffLocks::isEnabled()) setText(g_sys_diff_sub, DiffLocks::isInverted()
? "DI0 rear, DI1 front - 0 V = locked"
: "DI0 rear, DI1 front - 12 V = locked");
else if (!DiffLocks::isValid()) setText(g_sys_diff_sub, "Now: reading...");
else setText(g_sys_diff_sub, DiffLocks::frontLocked()
? (DiffLocks::rearLocked() ? "Now: rear LOCKED, front LOCKED" : "Now: FRONT only - check DI0 wiring")
@@ -826,7 +835,19 @@ static void buildSystemPage(lv_obj_t* page) {
g_sys_dbg = toggle(right, onDebugSwitch, NULL);
lv_obj_t* diff_row = makeSettingRow(c, "Diff lock inputs", "DI0 rear, DI1 front - 12 V = locked", &right);
g_sys_diff_sub = lv_obj_get_child(lv_obj_get_child(diff_row, 0), 1);
g_sys_diff = toggle(right, onDiffSwitch, NULL);
// two switches on the right: polarity ("Inverted") and on/off; the page has no room for a fifth row
lv_obj_t* diff_right = box(right, LV_FLEX_FLOW_ROW);
lv_obj_set_size(diff_right, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
// START, not END: with END, LVGL 8's content-size pass measures only the last child
// (the earlier ones sit at negative x) and clips "Inverted" away - seen on the board.
// The row's SPACE_BETWEEN already pushes this box to the right edge.
lv_obj_set_flex_align(diff_right, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_column(diff_right, 12, 0);
label(diff_right, "Inverted", &lv_font_montserrat_20, UI_COL_MUTED);
g_sys_diff_inv = toggle(diff_right, onDiffInvertSwitch, NULL);
lv_obj_t* gap = box(diff_right, LV_FLEX_FLOW_ROW);
lv_obj_set_size(gap, 12, 1);
g_sys_diff = toggle(diff_right, onDiffSwitch, NULL);
// two labels side by side, same line spacing, so keys and values line up
lv_obj_t* info = card(page);
+4
View File
@@ -222,3 +222,7 @@ The "Sleep when the car is off" switch and both power managers are gone: code, S
## [2026-09-29] update | Diff-lock inputs on DI0 / DI1
The user wants the two Patrol diff locks (+12 V while locked; the front only locks after the rear) shown on the Main CTRL. From the schematic: DI0 = CH422G EXIO0, DI1 = EXIO5, active low behind PC814. The CH422G's IO0-7 are one all-in/all-out bank shared with touch reset, backlight and LCD reset, so the firmware flips the bank to input for about 0.1 ms per read, 4 Hz. It is behind a Settings switch, default OFF, until tried on the bench for flicker. New page [[diff-lock-inputs]]; the schematic source page now carries the input circuit and the EXIO net table.
## [2026-09-29] update | Diff-lock polarity is inverted in the car
In the car the Driveline showed locked while unlocked and open while locked; the bench (12 V = locked) had been right. The likely cause is the ground-switched dash lamps (unverified). Added a per-board polarity setting: the "Inverted" switch in the Settings diff-lock row and `difflock invert on|off`, NVS `difflock_inv`. [[diff-lock-inputs]] is corrected.
+9 -4
View File
@@ -6,14 +6,19 @@ updated: 2026-09-29
# Diff-lock inputs (DI0 rear, DI1 front)
The Patrol has two differential locks. Their switches put **+12 V on a wire while a diff is locked** (source: user, 2026-09-29). The front can only be locked after the rear, so the valid states are: none, rear only, rear + front (source: user). The Main CTRL shows them on the Driveline screen from its two isolated inputs.
The Patrol has two differential locks. The user first described the wires as carrying **+12 V while a diff is locked**. In the car, the screen then showed the opposite (verified by the user, 2026-09-29): the wires carry current while unlocked and none once locked.
> [!warning] Polarity in the car is inverted
> On the bench, 12 V on DI0 showed locked (verified). In the car, the same firmware showed locked while unlocked and open while locked (verified, 2026-09-29). The likely cause is the usual lamp wiring: the dash lamp is fed +12 V and the diff's position switch grounds it on lock. A wire tapped between the lamp and the switch then sits at about 12 V through the lamp filament while unlocked, and at 0 V once the switch closes `(unverified: wiring not traced)`. Fix: the per-board **Inverted** switch (Settings → System, diff-lock row) or `difflock invert on` `(repo)`. With it on, a broken wire reads as *locked*, which shows up as amber, not as a silent "open".
The front can only be locked after the rear, so the valid states are: none, rear only, rear + front (source: user). The Main CTRL shows them on the Driveline screen from its two isolated inputs.
## Wiring
| Terminal (16-way block) | Connect to |
|---|---|
| `DI0` | rear diff-lock wire (+12 V = locked) |
| `DI1` | front diff-lock wire (+12 V = locked) |
| `DI0` | rear diff-lock wire (in the car: 12 V = UNlocked, so turn Inverted on) |
| `DI1` | front diff-lock wire (same) |
| `DI_COM` | vehicle ground (chassis, same ground as the board's `GND`) |
This is Waveshare's "PNP wet input / high-level trigger" wiring: `DI_COM` to the negative pole, 5–36 V DC on the input (source: [[esp32-s3-touch-lcd-5b-waveshare-wiki]]). Tap the wires in parallel with whatever the Patrol already drives with them (dash lamp); each input draws only ~3 mA.
@@ -32,7 +37,7 @@ The CH422G's IO0–7 are **one bank, all inputs or all outputs**, and the same b
> [!warning] Not yet verified on this board
> Whether the 0.1 ms float disturbs the panel (flicker) or touch is **unknown** until tried. That is why the feature sits behind **Settings → System → Diff lock inputs**, default OFF. A board without the wiring never flips the bank. If the panel flickers with the switch on, turn it off. The fallback is to move the inputs to spare GPIOs on the Inst. Cluster.
A state counts after two equal reads, so it can lag the real switch by up to 0.5 s. The Driveline draws a top view of the chassis, after the user's sketch. A locked axle turns amber: axle, wheels, and an X on the diff. Front locked without rear turns the front axle red, as a wiring or switch fault (verified on the bench by the user, 2026-09-29, with the earlier pill display: DI1 alone showed red, both inputs showed amber).
A state counts after two equal reads, so it can lag the real switch by up to 0.5 s. The Driveline draws a top view of the chassis, after the user's sketch. A locked axle turns amber: axle, tyres filled, and an X on the diff. There is no text; the drawing alone carries the state (user, 2026-09-29). Front locked without rear turns the front axle red, as a wiring or switch fault (verified on the bench by the user, 2026-09-29, with the earlier pill display: DI1 alone showed red, both inputs showed amber).
## Test on the bench