Diff locks: per-board polarity ('Inverted' switch in the Settings row, 'difflock invert on|off', NVS difflock_inv) - in the car the lock wires carry 12 V while unlocked
Firmware / build (push) Successful in 7m6s

This commit is contained in:
2026-09-29 15:18:23 +02:00
parent 0254bf19e9
commit b431cdfa16
10 changed files with 88 additions and 18 deletions
+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
+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
+20 -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,16 @@ 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);
lv_obj_set_flex_align(diff_right, LV_FLEX_ALIGN_END, 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);