184 lines
6.7 KiB
C++
184 lines
6.7 KiB
C++
/**
|
|
* ESP32-S3-LCD-1.3 - Inst. Cluster
|
|
*
|
|
* Receives the v2 link from Main CTRL on UART1 and ESP-NOW, shows the data
|
|
* on the 1.3" round display, and drives the Nissan Patrol cluster's tachometer
|
|
* and coolant gauge from it (GaugeOutputs, IO7 / IO8).
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <lvgl.h>
|
|
#include "modules/display_init.h"
|
|
#include "modules/uart_receiver.h"
|
|
#include "modules/espnow_receiver.h"
|
|
#include "modules/gauge_outputs.h"
|
|
#include "modules/debug_monitor.h"
|
|
#include "modules/gauge_screen.h"
|
|
#include "modules/link_peer.h"
|
|
#include "modules/console.h"
|
|
#include "modules/ota_update.h"
|
|
#include "config/ota_config.h"
|
|
|
|
// FreeRTOS task handles
|
|
TaskHandle_t lvglTaskHandle = NULL;
|
|
|
|
// Link is considered lost after this long without an applied DATA frame
|
|
#define LINK_TIMEOUT_MS 1000
|
|
|
|
/**
|
|
* LVGL update task (runs on Core 1)
|
|
*/
|
|
void lvglTask(void *parameter) {
|
|
Serial.println("[LVGL Task] Started on Core 1");
|
|
|
|
while (true) {
|
|
DisplayInit::update();
|
|
vTaskDelay(pdMS_TO_TICKS(5));
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
Serial.println("\n[BOOT] ESP32-S3-LCD-1.3 starting...");
|
|
|
|
unsigned long start_wait = millis();
|
|
while (!Serial && (millis() - start_wait < 3000)) {
|
|
delay(100);
|
|
}
|
|
delay(500);
|
|
|
|
Serial.println("[BOOT] Serial communication ready!");
|
|
Serial.println("\n\n=================================");
|
|
Serial.println("ESP32-S3-LCD-1.3 - Inst. Cluster");
|
|
Serial.printf("Firmware %s | Link protocol v%u | UART + ESP-NOW | gauge outputs\n", FW_VERSION, PACKET_VERSION);
|
|
Serial.println("=================================\n");
|
|
|
|
Serial.printf("CPU Frequency: %d MHz\n", getCpuFrequencyMhz());
|
|
Serial.printf("Free Heap: %d bytes\n", ESP.getFreeHeap());
|
|
Serial.printf("PSRAM Size: %d bytes\n", ESP.getPsramSize());
|
|
Serial.printf("PSRAM Free: %d bytes\n", ESP.getFreePsram());
|
|
|
|
if (!DisplayInit::init()) {
|
|
Serial.println("[ERROR] Display initialization failed!");
|
|
while (1) {
|
|
delay(1000);
|
|
}
|
|
}
|
|
|
|
GaugeScreen::init(); // hand-coded screen, built once, loaded
|
|
|
|
xTaskCreatePinnedToCore(lvglTask, "LVGL_Task", 8192, NULL, 2, &lvglTaskHandle, 1);
|
|
|
|
Serial.println("\n[Debug] Initializing debug monitor control...");
|
|
DebugMonitor::init();
|
|
|
|
// Gauge outputs before the receivers, so the first CONFIG frame has
|
|
// somewhere to land and the pins are already driven low.
|
|
Serial.println("\n[Gauges] Initializing Patrol gauge outputs...");
|
|
GaugeOutputs::init();
|
|
|
|
// Pairing key + Wi-Fi credentials from NVS, before any frame can arrive
|
|
LinkPeer::init();
|
|
Ota::init(); // version + slot; hooks A's OTA commands
|
|
Console::addHandler(LinkPeer::handleCommand);
|
|
Console::addHandler(Ota::handleCommand);
|
|
|
|
Serial.println("\n[UART] Initializing link receiver...");
|
|
if (!UARTReceiver::init()) {
|
|
Serial.println("[ERROR] Link receiver initialization failed!");
|
|
while (1) {
|
|
delay(1000);
|
|
}
|
|
}
|
|
Serial.println("[UART] Link receiver initialized");
|
|
|
|
Serial.println("\n[EspNow] Initializing ESP-NOW receiver...");
|
|
if (!EspNowReceiver::init()) {
|
|
Serial.println("[WARN] ESP-NOW receiver failed to start - UART only");
|
|
}
|
|
|
|
Serial.println("\n[Setup] Complete! Starting main loop...\n");
|
|
}
|
|
|
|
void loop() {
|
|
static uint32_t last_stats_print = 0;
|
|
static uint32_t last_connection_check = 0;
|
|
static uint32_t last_ui_update = 0;
|
|
static uint32_t last_cfg_seq = 0;
|
|
static bool was_connected = false;
|
|
|
|
// Wireless leg housekeeping (channel hopping while silent)
|
|
EspNowReceiver::tick();
|
|
// STATUS to A, PAIR/SECURE frames from A, serial console
|
|
LinkPeer::tick();
|
|
Ota::tick(); // runs a queued check/install (blocks the loop while it does)
|
|
Console::tick();
|
|
|
|
// Apply a new CONFIG from A. Overrides are a bench tool: applied but not
|
|
// persisted, so a reboot never leaves the gauges pinned to a test value.
|
|
uint32_t cfg_seq = UARTReceiver::getConfigSeq();
|
|
if (cfg_seq != last_cfg_seq) {
|
|
last_cfg_seq = cfg_seq;
|
|
link_config_t cfg = UARTReceiver::getConfig();
|
|
GaugeOutputs::applyConfig(cfg, cfg.override_mode == 0);
|
|
}
|
|
|
|
// Statistics every 5 seconds
|
|
if (millis() - last_stats_print > 5000) {
|
|
last_stats_print = millis();
|
|
|
|
UARTReceiver::uart_rx_stats_t s = UARTReceiver::getStats();
|
|
Serial.printf("[Stats] Uptime: %lu s | Free Heap: %d bytes\n",
|
|
millis() / 1000, ESP.getFreeHeap());
|
|
Serial.printf("[Link] applied=%lu uart=%lu espnow=%lu dup=%lu cfg=%lu | chk=%lu frame=%lu ver=%lu | uart %s, espnow %s (ch %u)\n",
|
|
(unsigned long)s.packets_received, (unsigned long)s.packets_uart,
|
|
(unsigned long)s.packets_espnow, (unsigned long)s.duplicates,
|
|
(unsigned long)s.config_received,
|
|
(unsigned long)s.checksum_errors, (unsigned long)s.framing_errors,
|
|
(unsigned long)s.version_errors,
|
|
UARTReceiver::isSourceAlive(LINK_SOURCE_UART, LINK_TIMEOUT_MS) ? "alive" : "silent",
|
|
UARTReceiver::isSourceAlive(LINK_SOURCE_ESPNOW, LINK_TIMEOUT_MS) ? "alive" : "silent",
|
|
EspNowReceiver::currentChannel());
|
|
if (s.packets_received > 0) {
|
|
Serial.printf("[Link] Last packet: %lu ms ago\n", (unsigned long)UARTReceiver::getTimeSinceLastPacket());
|
|
}
|
|
GaugeOutputs::printStatus();
|
|
}
|
|
|
|
// Connection state changes
|
|
if (millis() - last_connection_check > 1000) {
|
|
last_connection_check = millis();
|
|
bool is_connected = UARTReceiver::isConnected(LINK_TIMEOUT_MS);
|
|
if (is_connected && !was_connected) {
|
|
Serial.println("[Link] Connection ESTABLISHED with Main CTRL");
|
|
} else if (!is_connected && was_connected) {
|
|
Serial.println("[Link] Connection LOST - no data from Main CTRL");
|
|
}
|
|
was_connected = is_connected;
|
|
}
|
|
|
|
// Tach slew: glide the output frequency between the 5 Hz data points so the
|
|
// pulse-counting cluster tach sweeps instead of stair-stepping.
|
|
static uint32_t last_tach_tick = 0;
|
|
if (millis() - last_tach_tick >= 20) {
|
|
last_tach_tick = millis();
|
|
GaugeOutputs::tick();
|
|
}
|
|
|
|
// UI + gauge outputs at the packet rate (5 Hz)
|
|
if (millis() - last_ui_update > 200) {
|
|
last_ui_update = millis();
|
|
|
|
automotive_data_t data = UARTReceiver::getLatestData();
|
|
bool is_connected = UARTReceiver::isConnected(LINK_TIMEOUT_MS);
|
|
|
|
GaugeOutputs::update(data, is_connected);
|
|
|
|
GaugeScreen::update(data, is_connected);
|
|
DebugMonitor::tick(); // apply A's debug flag under the LVGL lock
|
|
}
|
|
|
|
delay(10);
|
|
}
|