Link protocol v2, ESP-NOW leg, gauge tunables + tuning screen; remove Home screen
Protocol v2 (shared header, byte-identical with the 1.3 repo): - typed DATA/CONFIG frames: 0xAA | ver | type | len | payload | XOR | 0x55 - gear split into lever (P/R/N/D/S/M) + target/engaged nibbles, engine rpm, alert bitmap decided here from tunable thresholds, link_config_t tunables - docs/UART_PROTOCOL.md rewritten for v2 Controller A modules: - tunables: gauge calibration + alert thresholds in NVS, 'cfg' serial console, bench override (never persisted) - espnow_link: every frame also broadcast on ESP-NOW ch 1; radio stays in STA (WiFiManager no longer powers it off) - uart_manager: DATA at 5 Hz + CONFIG on change/5 s on both transports - can_driver: publishes gear pieces and rpm, evaluates alerts - tuning_screen: hand-coded Gauge Tuning page (swipe right from Driveline), -/+ rows, enable switches, override sliders, Driveline/Defaults buttons UI: SquareLine Home screen (3 meters + battery) and meters_manager removed; Driveline is the boot screen; Settings back-gesture calls app_show_driveline(). tools/fix_penv_deps.ps1: repairs the shared PlatformIO penv after a 1.3 build. Bench-verified with Controller B: both transports alive, dedupe, override rpm 2000 -> 133.3 Hz tach output. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
* Tunables Module Implementation
|
||||
*/
|
||||
|
||||
#include "tunables.h"
|
||||
#include "serial_lock.h"
|
||||
#include <Preferences.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
namespace Tunables {
|
||||
|
||||
// ============================================================================
|
||||
// PRIVATE MODULE STATE
|
||||
// ============================================================================
|
||||
|
||||
static const char* NVS_NAMESPACE = "tunables";
|
||||
static const char* NVS_KEY_BLOB = "cfg";
|
||||
|
||||
static link_config_t g_cfg;
|
||||
static SemaphoreHandle_t g_mutex = NULL;
|
||||
|
||||
static char g_line[96];
|
||||
static size_t g_line_len = 0;
|
||||
|
||||
// ============================================================================
|
||||
// PRIVATE HELPERS
|
||||
// ============================================================================
|
||||
|
||||
static void lock() { if (g_mutex) xSemaphoreTake(g_mutex, portMAX_DELAY); }
|
||||
static void unlock() { if (g_mutex) xSemaphoreGive(g_mutex); }
|
||||
|
||||
static void save() {
|
||||
// Never persist a bench override: a reboot must come up with real data.
|
||||
link_config_t c = g_cfg;
|
||||
c.override_mode = 0;
|
||||
c.override_rpm = 0;
|
||||
c.override_duty = 0;
|
||||
Preferences prefs;
|
||||
prefs.begin(NVS_NAMESPACE, false);
|
||||
prefs.putBytes(NVS_KEY_BLOB, &c, sizeof(c));
|
||||
prefs.end();
|
||||
}
|
||||
|
||||
static bool load() {
|
||||
Preferences prefs;
|
||||
prefs.begin(NVS_NAMESPACE, true);
|
||||
size_t n = prefs.getBytesLength(NVS_KEY_BLOB);
|
||||
bool ok = false;
|
||||
if (n == sizeof(link_config_t)) {
|
||||
prefs.getBytes(NVS_KEY_BLOB, &g_cfg, sizeof(g_cfg));
|
||||
ok = true;
|
||||
}
|
||||
prefs.end();
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void print(const char* fmt, ...) {
|
||||
char buf[200];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
SerialLock::lock();
|
||||
Serial.println(buf);
|
||||
SerialLock::unlock();
|
||||
}
|
||||
|
||||
static void show() {
|
||||
link_config_t c = get();
|
||||
print("[cfg] version %u", c.cfg_version);
|
||||
print("[cfg] tach: ppr=%u trim=%d%% min_hz=%u max_hz=%u enable=%d",
|
||||
c.tach_ppr, c.tach_trim_pct, c.tach_min_hz, c.tach_max_hz,
|
||||
(c.out_enable & OUT_ENABLE_TACH) ? 1 : 0);
|
||||
print("[cfg] temp: pwm_hz=%u slope=%u.%02u duty/C offset=%d min_c=%d duty_max=%u enable=%d",
|
||||
c.temp_pwm_hz, c.temp_slope_x100 / 100, c.temp_slope_x100 % 100,
|
||||
c.temp_offset, c.temp_min_c, c.temp_duty_max,
|
||||
(c.out_enable & OUT_ENABLE_TEMP) ? 1 : 0);
|
||||
print("[cfg] override: mode=%u rpm=%u duty=%u", c.override_mode, c.override_rpm, c.override_duty);
|
||||
print("[cfg] alerts: coolant_hot=%dC gearbox_hot=%dC battery_low=%u.%02uV",
|
||||
c.coolant_hot_c, c.gearbox_hot_c, c.battery_low_x100 / 100, c.battery_low_x100 % 100);
|
||||
}
|
||||
|
||||
static void help() {
|
||||
print("cfg show print current tunables");
|
||||
print("cfg set <name> <value> names: tach_ppr tach_trim tach_min_hz tach_max_hz");
|
||||
print(" temp_hz temp_slope(x100) temp_offset temp_min temp_duty_max");
|
||||
print(" tach_en temp_en coolant_hot gearbox_hot batt_low(x100)");
|
||||
print("cfg override off | rpm <n> | duty <n> | both <rpm> <duty>");
|
||||
print("cfg reset back to defaults");
|
||||
}
|
||||
|
||||
static bool setField(const char* name, long v) {
|
||||
lock();
|
||||
bool ok = true;
|
||||
if (!strcmp(name, "tach_ppr")) g_cfg.tach_ppr = (uint8_t)constrain(v, 1, 32);
|
||||
else if (!strcmp(name, "tach_trim")) g_cfg.tach_trim_pct = (int8_t)constrain(v, -50, 50);
|
||||
else if (!strcmp(name, "tach_min_hz")) g_cfg.tach_min_hz = (uint16_t)constrain(v, 0, 5000);
|
||||
else if (!strcmp(name, "tach_max_hz")) g_cfg.tach_max_hz = (uint16_t)constrain(v, 0, 5000);
|
||||
else if (!strcmp(name, "temp_hz")) g_cfg.temp_pwm_hz = (uint16_t)constrain(v, 20, 20000);
|
||||
else if (!strcmp(name, "temp_slope")) g_cfg.temp_slope_x100 = (uint16_t)constrain(v, 0, 5000);
|
||||
else if (!strcmp(name, "temp_offset")) g_cfg.temp_offset = (int16_t)constrain(v, -1000, 1000);
|
||||
else if (!strcmp(name, "temp_min")) g_cfg.temp_min_c = (int16_t)constrain(v, -50, 200);
|
||||
else if (!strcmp(name, "temp_duty_max")) g_cfg.temp_duty_max = (uint8_t)constrain(v, 0, 255);
|
||||
else if (!strcmp(name, "tach_en")) g_cfg.out_enable = v ? (g_cfg.out_enable | OUT_ENABLE_TACH) : (g_cfg.out_enable & ~OUT_ENABLE_TACH);
|
||||
else if (!strcmp(name, "temp_en")) g_cfg.out_enable = v ? (g_cfg.out_enable | OUT_ENABLE_TEMP) : (g_cfg.out_enable & ~OUT_ENABLE_TEMP);
|
||||
else if (!strcmp(name, "coolant_hot")) g_cfg.coolant_hot_c = (int16_t)constrain(v, 0, 200);
|
||||
else if (!strcmp(name, "gearbox_hot")) g_cfg.gearbox_hot_c = (int16_t)constrain(v, 0, 200);
|
||||
else if (!strcmp(name, "batt_low")) g_cfg.battery_low_x100 = (uint16_t)constrain(v, 0, 3000);
|
||||
else ok = false;
|
||||
if (ok) {
|
||||
g_cfg.cfg_version++;
|
||||
if (g_cfg.cfg_version == 0) g_cfg.cfg_version = 1;
|
||||
save();
|
||||
}
|
||||
unlock();
|
||||
return ok;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PUBLIC API
|
||||
// ============================================================================
|
||||
|
||||
void init() {
|
||||
if (g_mutex == NULL) g_mutex = xSemaphoreCreateMutex();
|
||||
lock();
|
||||
if (!load()) {
|
||||
link_config_defaults(&g_cfg);
|
||||
save();
|
||||
unlock();
|
||||
print("[Tunables] No saved config (or size changed) - defaults written to NVS");
|
||||
} else {
|
||||
unlock();
|
||||
print("[Tunables] Loaded config version %u from NVS", g_cfg.cfg_version);
|
||||
}
|
||||
show();
|
||||
print("[Tunables] Type 'cfg help' on this console to tune the Patrol gauges");
|
||||
}
|
||||
|
||||
link_config_t get() {
|
||||
link_config_t c;
|
||||
lock();
|
||||
c = g_cfg;
|
||||
unlock();
|
||||
return c;
|
||||
}
|
||||
|
||||
uint8_t version() {
|
||||
lock();
|
||||
uint8_t v = g_cfg.cfg_version;
|
||||
unlock();
|
||||
return v;
|
||||
}
|
||||
|
||||
void set(const link_config_t& cfg) {
|
||||
lock();
|
||||
uint8_t v = g_cfg.cfg_version + 1;
|
||||
g_cfg = cfg;
|
||||
g_cfg.cfg_version = v ? v : 1;
|
||||
save();
|
||||
unlock();
|
||||
}
|
||||
|
||||
void resetDefaults() {
|
||||
lock();
|
||||
uint8_t v = g_cfg.cfg_version + 1;
|
||||
link_config_defaults(&g_cfg);
|
||||
g_cfg.cfg_version = v ? v : 1;
|
||||
save();
|
||||
unlock();
|
||||
}
|
||||
|
||||
void setOverride(uint8_t mode, uint16_t rpm, uint8_t duty) {
|
||||
lock();
|
||||
g_cfg.override_mode = mode & (OVERRIDE_TACH | OVERRIDE_TEMP);
|
||||
g_cfg.override_rpm = rpm;
|
||||
g_cfg.override_duty = duty;
|
||||
g_cfg.cfg_version++;
|
||||
if (g_cfg.cfg_version == 0) g_cfg.cfg_version = 1;
|
||||
unlock();
|
||||
print("[cfg] override mode=%u rpm=%u duty=%u (not saved)", mode, rpm, duty);
|
||||
}
|
||||
|
||||
bool handleCommand(const char* line) {
|
||||
char buf[96];
|
||||
strncpy(buf, line, sizeof(buf) - 1);
|
||||
buf[sizeof(buf) - 1] = 0;
|
||||
|
||||
char* argv[5] = {0};
|
||||
int argc = 0;
|
||||
for (char* tok = strtok(buf, " \t"); tok && argc < 5; tok = strtok(NULL, " \t")) {
|
||||
argv[argc++] = tok;
|
||||
}
|
||||
if (argc == 0) return false;
|
||||
if (strcmp(argv[0], "cfg") != 0 && strcmp(argv[0], "help") != 0) return false;
|
||||
|
||||
if (argc == 1 || !strcmp(argv[1], "help") || !strcmp(argv[0], "help")) { help(); return true; }
|
||||
if (!strcmp(argv[1], "show")) { show(); return true; }
|
||||
if (!strcmp(argv[1], "reset")) { resetDefaults(); print("[cfg] reset to defaults"); show(); return true; }
|
||||
|
||||
if (!strcmp(argv[1], "set") && argc == 4) {
|
||||
long v = strtol(argv[3], NULL, 0);
|
||||
if (setField(argv[2], v)) {
|
||||
print("[cfg] %s = %ld (version %u, saved)", argv[2], v, version());
|
||||
} else {
|
||||
print("[cfg] unknown field '%s'", argv[2]);
|
||||
help();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "override") && argc >= 3) {
|
||||
lock();
|
||||
if (!strcmp(argv[2], "off")) {
|
||||
g_cfg.override_mode = 0;
|
||||
} else if (!strcmp(argv[2], "rpm") && argc == 4) {
|
||||
g_cfg.override_mode = OVERRIDE_TACH;
|
||||
g_cfg.override_rpm = (uint16_t)constrain(strtol(argv[3], NULL, 0), 0, 10000);
|
||||
} else if (!strcmp(argv[2], "duty") && argc == 4) {
|
||||
g_cfg.override_mode = OVERRIDE_TEMP;
|
||||
g_cfg.override_duty = (uint8_t)constrain(strtol(argv[3], NULL, 0), 0, 255);
|
||||
} else if (!strcmp(argv[2], "both") && argc == 5) {
|
||||
g_cfg.override_mode = OVERRIDE_TACH | OVERRIDE_TEMP;
|
||||
g_cfg.override_rpm = (uint16_t)constrain(strtol(argv[3], NULL, 0), 0, 10000);
|
||||
g_cfg.override_duty = (uint8_t)constrain(strtol(argv[4], NULL, 0), 0, 255);
|
||||
} else {
|
||||
unlock();
|
||||
help();
|
||||
return true;
|
||||
}
|
||||
g_cfg.cfg_version++;
|
||||
if (g_cfg.cfg_version == 0) g_cfg.cfg_version = 1;
|
||||
// Overrides are a bench tool: pushed to B immediately but NOT persisted,
|
||||
// so a reboot never leaves the gauges pinned to a test value.
|
||||
unlock();
|
||||
print("[cfg] override mode=%u rpm=%u duty=%u (not saved)", g_cfg.override_mode, g_cfg.override_rpm, g_cfg.override_duty);
|
||||
return true;
|
||||
}
|
||||
|
||||
help();
|
||||
return true;
|
||||
}
|
||||
|
||||
void pollSerial() {
|
||||
while (Serial.available()) {
|
||||
char c = (char)Serial.read();
|
||||
if (c == '\r' || c == '\n') {
|
||||
if (g_line_len > 0) {
|
||||
g_line[g_line_len] = 0;
|
||||
if (!handleCommand(g_line)) {
|
||||
print("[console] unknown command '%s' (try 'cfg help')", g_line);
|
||||
}
|
||||
g_line_len = 0;
|
||||
}
|
||||
} else if (g_line_len < sizeof(g_line) - 1) {
|
||||
g_line[g_line_len++] = c;
|
||||
} else {
|
||||
g_line_len = 0; // overflow: drop the line
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Tunables
|
||||
Reference in New Issue
Block a user