This commit is contained in:
2026-09-21 10:28:35 +02:00
commit b6fb01ba73
54 changed files with 33982 additions and 0 deletions
+301
View File
@@ -0,0 +1,301 @@
/**
* ESP32-S3-LCD-1.3 - Controller B
* Phase 2: UART Reception & Data Display
*
* This firmware receives automotive data packets from Controller A via UART
* and displays real-time sensor data on the 1.3" round display (240x240 ST7789VW).
*/
#include <Arduino.h>
#include <lvgl.h>
#include "modules/display_init.h"
#include "modules/uart_receiver.h"
#include "modules/debug_monitor.h"
#include "ui/ui.h"
#include "ui/screens/ui_Home_Screen.h"
// FreeRTOS task handles
TaskHandle_t lvglTaskHandle = NULL;
// Warning thresholds
#define COOLANT_TEMP_WARNING_C 110 // °C (1100 in 0.1°C units)
#define ENGINE_OIL_TEMP_WARNING_C 130 // °C (1300 in 0.1°C units)
#define GEARBOX_OIL_TEMP_WARNING_C 130 // °C (1300 in 0.1°C units)
#define BATTERY_VOLTAGE_WARNING_V 11.0f // V (1100 in 0.01V units)
/**
* Helper function to update sensor icon position, visibility, and color
*
* @param icon_normal Normal color icon object
* @param icon_red Red/warning color icon object
* @param label_ref Label to position relative to
* @param position_below If true, position icon below label; if false, position above
* @param show If true, show appropriate icon; if false, hide both icons
* @param use_red If true and show=true, show red icon; if false, show normal icon
*/
void update_sensor_icon(lv_obj_t* icon_normal, lv_obj_t* icon_red,
lv_obj_t* label_ref, bool position_below,
bool show, bool use_red) {
if (icon_normal == NULL || icon_red == NULL || label_ref == NULL) {
return; // Safety check
}
if (show) {
// Determine which icon to show
lv_obj_t* icon_to_show = use_red ? icon_red : icon_normal;
lv_obj_t* icon_to_hide = use_red ? icon_normal : icon_red;
// Position the icon relative to the label
if (position_below) {
lv_obj_align_to(icon_to_show, label_ref, LV_ALIGN_OUT_BOTTOM_MID, 0, 2); // 2px gap below
} else {
lv_obj_align_to(icon_to_show, label_ref, LV_ALIGN_OUT_TOP_MID, 0, -2); // 2px gap above
}
// Show appropriate icon, hide the other
lv_obj_clear_flag(icon_to_show, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(icon_to_hide, LV_OBJ_FLAG_HIDDEN);
} else {
// Hide both icons when no data
lv_obj_add_flag(icon_normal, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(icon_red, LV_OBJ_FLAG_HIDDEN);
}
}
/**
* 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)); // ~200 FPS max, actual will be limited by LVGL
}
}
void setup() {
// Initialize serial (USB CDC on ESP32-S3)
Serial.begin(115200);
// Send immediate boot message (may be lost if monitor not connected yet)
Serial.println("\n[BOOT] ESP32-S3-LCD-1.3 starting...");
// Wait for USB CDC to be ready (important for ESP32-S3)
unsigned long start_wait = millis();
while (!Serial && (millis() - start_wait < 3000)) {
delay(100); // Wait up to 3 seconds for serial
}
delay(500); // Additional stabilization time
Serial.println("[BOOT] Serial communication ready!");
Serial.println("\n\n=================================");
Serial.println("ESP32-S3-LCD-1.3 - Controller B");
Serial.println("Phase 2: UART Reception");
Serial.println("=================================\n");
// Display hardware info
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());
// Initialize display hardware
if (!DisplayInit::init()) {
Serial.println("[ERROR] Display initialization failed!");
while (1) {
delay(1000);
}
}
// Initialize SquareLine Studio UI
Serial.println("[UI] Initializing SquareLine Studio UI...");
ui_init();
Serial.println("[UI] SquareLine Studio UI initialized");
// Create LVGL update task on Core 1
xTaskCreatePinnedToCore(
lvglTask,
"LVGL_Task",
8192, // Stack size
NULL,
2, // Priority
&lvglTaskHandle,
1 // Core 1
);
// Initialize debug monitor module
Serial.println("\n[Debug] Initializing debug monitor control...");
DebugMonitor::init();
// Initialize UART receiver (creates task on Core 0)
Serial.println("\n[UART] Initializing UART receiver...");
if (!UARTReceiver::init()) {
Serial.println("[ERROR] UART receiver initialization failed!");
while (1) {
delay(1000);
}
}
Serial.println("[UART] UART receiver initialized");
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 bool was_connected = false;
// Print UART statistics every 5 seconds
if (millis() - last_stats_print > 5000) {
last_stats_print = millis();
UARTReceiver::uart_rx_stats_t stats = UARTReceiver::getStats();
Serial.printf("[Stats] Uptime: %lu s | Free Heap: %d bytes\n",
millis() / 1000,
ESP.getFreeHeap());
Serial.printf("[UART Stats] Packets: %lu | Checksum Errors: %lu | Framing Errors: %lu | Bytes RX: %lu\n",
stats.packets_received,
stats.checksum_errors,
stats.framing_errors,
stats.bytes_received);
if (stats.packets_received > 0) {
uint32_t time_since = UARTReceiver::getTimeSinceLastPacket();
Serial.printf("[UART Stats] Last packet: %lu ms ago\n", time_since);
}
}
// Check connection status every second
if (millis() - last_connection_check > 1000) {
last_connection_check = millis();
bool is_connected = UARTReceiver::isConnected(1000); // 1 second timeout
// Log connection state changes
if (is_connected && !was_connected) {
Serial.println("[UART] Connection ESTABLISHED with Controller A");
automotive_data_t data = UARTReceiver::getLatestData();
char gearbox_buf[16], engine_buf[16], voltage_buf[16];
Serial.printf("[Data] Gear:%s | Gearbox:%s | Engine:%s | Battery:%s | Debug:%s\n",
gear_to_string((gear_position_t)data.gear_position),
format_temperature(data.gearbox_oil_temp, gearbox_buf),
format_temperature(data.engine_oil_temp, engine_buf),
format_voltage(data.battery_voltage, voltage_buf),
data.debug_enabled ? "ON" : "OFF");
} else if (!is_connected && was_connected) {
Serial.println("[UART] Connection LOST - No data from Controller A");
}
was_connected = is_connected;
}
// Update UI labels every 200ms (5Hz, matching UART packet rate)
if (millis() - last_ui_update > 200) {
last_ui_update = millis();
automotive_data_t data = UARTReceiver::getLatestData();
bool is_connected = UARTReceiver::isConnected(1000);
if (is_connected) {
// Connection active - update labels with real data
// Check valid_flags per protocol requirement (UART_PROTOCOL.md lines 242, 554, 560)
char gearbox_buf[16], engine_buf[16], coolant_buf[16], voltage_buf[16];
// Update Gearbox Oil Temperature (check VALID_FLAG_GEARBOX_TEMP)
if (data.valid_flags & VALID_FLAG_GEARBOX_TEMP) {
lv_label_set_text(ui_GB_Oil_Temp_LBL, format_temperature(data.gearbox_oil_temp, gearbox_buf));
} else {
lv_label_set_text(ui_GB_Oil_Temp_LBL, "---"); // Invalid per protocol line 560
}
// Update Engine Oil Temperature (check VALID_FLAG_ENGINE_TEMP)
if (data.valid_flags & VALID_FLAG_ENGINE_TEMP) {
lv_label_set_text(ui_Engine_Oil_Temp_LBL, format_temperature(data.engine_oil_temp, engine_buf));
} else {
lv_label_set_text(ui_Engine_Oil_Temp_LBL, "---");
}
// Update Water/Coolant Temperature (check VALID_FLAG_COOLANT_TEMP)
if (data.valid_flags & VALID_FLAG_COOLANT_TEMP) {
lv_label_set_text(ui_Water_Temp_LBL, format_temperature(data.coolant_temp, coolant_buf));
} else {
lv_label_set_text(ui_Water_Temp_LBL, "---");
}
// Update Battery Voltage (check VALID_FLAG_BATTERY_VOLT)
if (data.valid_flags & VALID_FLAG_BATTERY_VOLT) {
lv_label_set_text(ui_Battery_Voltage_LBL, format_voltage(data.battery_voltage, voltage_buf));
} else {
lv_label_set_text(ui_Battery_Voltage_LBL, "---");
}
// Update Gear Position (check VALID_FLAG_GEAR_POS)
if (ui_Gear_State_LBL != NULL) {
if (data.valid_flags & VALID_FLAG_GEAR_POS) {
lv_label_set_text(ui_Gear_State_LBL, gear_to_string((gear_position_t)data.gear_position));
} else {
lv_label_set_text(ui_Gear_State_LBL, "?");
}
}
// Update sensor icons with warning logic
// Icons only shown if data is BOTH connected AND valid (per protocol)
// Temperature values are in 0.1°C units (e.g., 850 = 85.0°C)
// Voltage values are in 0.01V units (e.g., 1250 = 12.50V)
// Top Container Icons (positioned BELOW labels)
// Water/Coolant Temperature - show only if valid, warn if >= 110°C
bool coolant_valid = (data.valid_flags & VALID_FLAG_COOLANT_TEMP);
bool coolant_warning = coolant_valid && (data.coolant_temp >= (COOLANT_TEMP_WARNING_C * 10));
update_sensor_icon(ui_Engine_Water, ui_Engine_Water_Red,
ui_Water_Temp_LBL, true, coolant_valid, coolant_warning);
// Gearbox Oil Temperature - show only if valid, warn if >= 130°C
bool gearbox_valid = (data.valid_flags & VALID_FLAG_GEARBOX_TEMP);
bool gearbox_warning = gearbox_valid && (data.gearbox_oil_temp >= (GEARBOX_OIL_TEMP_WARNING_C * 10));
update_sensor_icon(ui_GearBox_Oil, ui_GearBox_Oil_Red,
ui_GB_Oil_Temp_LBL, true, gearbox_valid, gearbox_warning);
// Bottom Container Icons (positioned ABOVE labels)
// Battery Voltage - show only if valid, warn if < threshold
bool battery_valid = (data.valid_flags & VALID_FLAG_BATTERY_VOLT);
bool battery_warning = battery_valid && (data.battery_voltage < (uint16_t)(BATTERY_VOLTAGE_WARNING_V * 100));
update_sensor_icon(ui_Battery_Voltage, ui_Battery_Voltage_Red,
ui_Battery_Voltage_LBL, false, battery_valid, battery_warning);
// Engine Oil Temperature - show only if valid, warn if >= 130°C
bool engine_oil_valid = (data.valid_flags & VALID_FLAG_ENGINE_TEMP);
bool engine_oil_warning = engine_oil_valid && (data.engine_oil_temp >= (ENGINE_OIL_TEMP_WARNING_C * 10));
update_sensor_icon(ui_Engine_Oil, ui_Engine_Oil_Red,
ui_Engine_Oil_Temp_LBL, false, engine_oil_valid, engine_oil_warning);
} else {
// No connection - show "N/A" warning and hide all icons
lv_label_set_text(ui_GB_Oil_Temp_LBL, "N/A");
lv_label_set_text(ui_Engine_Oil_Temp_LBL, "N/A");
lv_label_set_text(ui_Water_Temp_LBL, "N/A");
lv_label_set_text(ui_Battery_Voltage_LBL, "N/A");
if (ui_Gear_State_LBL != NULL) {
lv_label_set_text(ui_Gear_State_LBL, "?");
}
// Hide all sensor icons when disconnected
update_sensor_icon(ui_Engine_Water, ui_Engine_Water_Red,
ui_Water_Temp_LBL, true, false, false);
update_sensor_icon(ui_GearBox_Oil, ui_GearBox_Oil_Red,
ui_GB_Oil_Temp_LBL, true, false, false);
update_sensor_icon(ui_Battery_Voltage, ui_Battery_Voltage_Red,
ui_Battery_Voltage_LBL, false, false, false);
update_sensor_icon(ui_Engine_Oil, ui_Engine_Oil_Red,
ui_Engine_Oil_Temp_LBL, false, false, false);
}
}
delay(10);
}