/** * automotive_data.h * * Shared data structure and protocol definitions for automotive monitoring system * ESP32-S3-LCD-5B (Controller A) -> ESP32-S3-LCD-1.3 (Controller B) communication * * This header defines the data structure for 6 automotive sensor values and the * simple packet framing protocol for UART transmission at 115200 baud. */ #ifndef _AUTOMOTIVE_DATA_H #define _AUTOMOTIVE_DATA_H #include // ============================================================================ // UART PROTOCOL CONFIGURATION // ============================================================================ // Communication parameters (hardware-independent) #define UART_BAUD_RATE 115200 // Reliable automotive-grade baud rate #define UART_UPDATE_RATE_MS 200 // 5Hz transmission rate // NOTE: UART pin assignments (TX/RX) are controller-specific and defined in: // - Controller A (5B): uart_manager.cpp // - Controller B (1.3): uart_receiver.cpp // Packet framing bytes #define PACKET_START_BYTE 0xAA // Start of packet marker #define PACKET_END_BYTE 0x55 // End of packet marker #define PACKET_VERSION 0x01 // Protocol version for future compatibility // Packet structure offsets #define PACKET_MIN_SIZE 8 // Start(1) + Version(1) + Length(1) + Data(min 1) + Checksum(1) + End(1) #define PACKET_MAX_SIZE 64 // Maximum packet size to prevent buffer overruns // ============================================================================ // AUTOMOTIVE DATA STRUCTURE // ============================================================================ // Gear position enumeration (matches UART protocol specification) typedef enum { GEAR_PARK = 0, // Park - transmission locked GEAR_REVERSE = 1, // Reverse gear GEAR_NEUTRAL = 2, // Neutral - no gear engaged GEAR_DRIVE = 3, // Drive - automatic mode GEAR_SPORT = 4, // Sport mode GEAR_MANUAL = 5, // Manual/Sequential mode GEAR_MANUAL_1 = 6, // Manual 1st gear GEAR_MANUAL_2 = 7, // Manual 2nd gear GEAR_MANUAL_3 = 8, // Manual 3rd gear GEAR_MANUAL_4 = 9, // Manual 4th gear GEAR_MANUAL_5 = 10, // Manual 5th gear GEAR_MANUAL_6 = 11, // Manual 6th gear GEAR_MANUAL_7 = 12, // Manual 7th gear GEAR_MANUAL_8 = 13, // Manual 8th gear GEAR_ERROR = 14 // Transmission fault/unknown } gear_position_t; // Main automotive data structure (packed for efficient transmission) typedef struct __attribute__((packed)) { // Identification and versioning uint8_t packet_id; // Incremental packet ID for debugging uint32_t timestamp_ms; // Milliseconds since boot // Sensor data (6 monitored values) uint8_t gear_position; // Current gear (see gear_position_t enum) int16_t gearbox_oil_temp; // Gearbox oil temperature in 0.1°C (-3276.8 to +3276.7°C) int16_t engine_oil_temp; // Engine oil temperature in 0.1°C int16_t coolant_temp; // Engine coolant temperature in 0.1°C uint16_t battery_voltage; // Battery voltage in 0.01V (0 to 655.35V) uint16_t fault_codes; // Bitmap of active fault codes (16 possible codes) // Data validity flags (1 bit per sensor) uint8_t valid_flags; // Bit 0: gear, Bit 1: gearbox temp, etc. // Debug mode flag (for Controller B to enable/disable monitors) uint8_t debug_enabled; // 0 = debug off, 1 = debug on } automotive_data_t; // Size of the data structure #define AUTOMOTIVE_DATA_SIZE sizeof(automotive_data_t) // Valid flags bit positions #define VALID_FLAG_GEAR_POS (1 << 0) #define VALID_FLAG_GEARBOX_TEMP (1 << 1) #define VALID_FLAG_ENGINE_TEMP (1 << 2) #define VALID_FLAG_COOLANT_TEMP (1 << 3) #define VALID_FLAG_BATTERY_VOLT (1 << 4) #define VALID_FLAG_FAULT_CODES (1 << 5) // ============================================================================ // CAN BUS ID MAPPINGS (Placeholder - User will provide real IDs) // ============================================================================ // Standard OBD-II PIDs (example placeholders) // User needs to replace these with actual CAN message IDs for their vehicle #define CAN_ID_GEAR_POSITION 0x0C0 // Placeholder - vehicle specific #define CAN_ID_GEARBOX_TEMP 0x5C // Placeholder - typically proprietary #define CAN_ID_ENGINE_OIL_TEMP 0x5C // Placeholder - not standard OBD-II #define CAN_ID_COOLANT_TEMP 0x05 // Standard OBD-II PID (if using OBD-II) #define CAN_ID_BATTERY_VOLTAGE 0x42 // Standard OBD-II PID for system voltage #define CAN_ID_FAULT_CODES 0x01 // Standard OBD-II PID for DTCs // Extended CAN IDs (29-bit) if needed #define USE_EXTENDED_CAN_IDS false // Set to true for 29-bit CAN IDs // ============================================================================ // PACKET STRUCTURE FOR UART TRANSMISSION // ============================================================================ typedef struct __attribute__((packed)) { uint8_t start_byte; // PACKET_START_BYTE (0xAA) uint8_t version; // Protocol version uint8_t length; // Length of data payload automotive_data_t data; // Actual sensor data uint8_t checksum; // XOR checksum of all bytes except start/end uint8_t end_byte; // PACKET_END_BYTE (0x55) } uart_packet_t; // ============================================================================ // HELPER FUNCTIONS // ============================================================================ /** * Calculate XOR checksum for data buffer * @param data Pointer to data buffer * @param length Length of data to checksum * @return XOR checksum value */ static inline uint8_t calculate_checksum(const uint8_t* data, size_t length) { uint8_t checksum = 0; for (size_t i = 0; i < length; i++) { checksum ^= data[i]; } return checksum; } /** * Validate packet checksum * @param packet Pointer to complete packet * @return true if checksum is valid, false otherwise */ static inline bool validate_packet(const uart_packet_t* packet) { if (packet->start_byte != PACKET_START_BYTE || packet->end_byte != PACKET_END_BYTE) { return false; } // Calculate checksum over version, length, and data size_t checksum_size = 2 + packet->length; // version(1) + length(1) + data(length) uint8_t calculated = calculate_checksum((uint8_t*)&packet->version, checksum_size); return (calculated == packet->checksum); } /** * Convert temperature from CAN format to internal format * @param can_value Raw CAN bus value * @param offset Temperature offset (usually -40 for automotive) * @return Temperature in 0.1°C units */ static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset = -40) { return (int16_t)((can_value + offset) * 10); // Convert to 0.1°C units } /** * Convert voltage from CAN format to internal format * @param can_value Raw CAN bus value (typically in 0.1V) * @return Voltage in 0.01V units */ static inline uint16_t can_to_voltage(uint16_t can_value) { return can_value * 10; // Convert 0.1V to 0.01V units } /** * Get human-readable gear position string * @param gear Gear position enum value * @return String representation of gear */ static inline const char* gear_to_string(gear_position_t gear) { switch (gear) { case GEAR_PARK: return "P"; case GEAR_REVERSE: return "R"; case GEAR_NEUTRAL: return "N"; case GEAR_DRIVE: return "D"; case GEAR_SPORT: return "S"; case GEAR_MANUAL: return "M"; case GEAR_MANUAL_1: return "1"; case GEAR_MANUAL_2: return "2"; case GEAR_MANUAL_3: return "3"; case GEAR_MANUAL_4: return "4"; case GEAR_MANUAL_5: return "5"; case GEAR_MANUAL_6: return "6"; case GEAR_MANUAL_7: return "7"; case GEAR_MANUAL_8: return "8"; case GEAR_ERROR: return "?"; default: return "?"; } } /** * Format temperature for display * @param temp_x10 Temperature in 0.1°C units * @param buffer Output buffer (minimum 10 characters) * @return Pointer to buffer */ static inline char* format_temperature(int16_t temp_x10, char* buffer) { sprintf(buffer, "%d.%dC", temp_x10 / 10, abs(temp_x10 % 10)); return buffer; } /** * Format voltage for display * @param voltage_x100 Voltage in 0.01V units * @param buffer Output buffer (minimum 10 characters) * @return Pointer to buffer */ static inline char* format_voltage(uint16_t voltage_x100, char* buffer) { sprintf(buffer, "%d.%02dV", voltage_x100 / 100, voltage_x100 % 100); return buffer; } // ============================================================================ // DEFAULT/TEST VALUES // ============================================================================ // Default values for testing (before CAN data is available) #define DEFAULT_GEAR_POS GEAR_PARK #define DEFAULT_GEARBOX_TEMP 850 // 85.0°C #define DEFAULT_ENGINE_OIL_TEMP 920 // 92.0°C #define DEFAULT_COOLANT_TEMP 890 // 89.0°C #define DEFAULT_BATTERY_VOLTAGE 1245 // 12.45V #define DEFAULT_FAULT_CODES 0x0000 // No faults #endif // _AUTOMOTIVE_DATA_H