e10e03eb90
Boot screen: land on the Driveline data screen instead of the SquareLine gauges Home screen. Done in display_init after ui_init while the backlight is still off (no flash of the wrong screen); Home stays reachable by swiping off Driveline. Engine torque added to the screen, decoded from 0x0A8 TORQ_AVL (available/ delivered torque), bits 12-23, signed x0.5 Nm. The user identified the ID and supplied the spreadsheet formula (D2*256+D1)/32, which is algebraically bits12-23 x0.5; confirmed against the repo's 0x0A8 worked example (24 Nm). Firmware masks D1's low nibble (that's the alive counter, not torque - the repo example shows ALIV=8 there) and sign-extends so overrun reads negative. 0x0AA TORQ_DVCH is the same layout but driver demand - identical at idle, diverges under load - so the delivered value is the right one to show. Output-shaft scaling revised: a direct Launch reference (output speed 128 rpm at our raw 1192) implies ~0.107, contradicting the earlier x0.125 (which would predict 149). One eyeballed cross-tool point, so nothing is adopted - the field stays raw counts - but 0.125 is downgraded in firmware and wiki, and the Launch now gives a direct rpm reference so calibration is just a multi-point sweep and a linear fit. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
507 lines
21 KiB
C
507 lines
21 KiB
C
/**
|
|
* 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 <stdint.h>
|
|
|
|
// ============================================================================
|
|
// 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
|
|
typedef enum {
|
|
GEAR_PARK = 0,
|
|
GEAR_REVERSE = 1,
|
|
GEAR_NEUTRAL = 2,
|
|
GEAR_DRIVE = 3,
|
|
GEAR_MANUAL_1 = 4,
|
|
GEAR_MANUAL_2 = 5,
|
|
GEAR_MANUAL_3 = 6,
|
|
GEAR_MANUAL_4 = 7,
|
|
GEAR_MANUAL_5 = 8,
|
|
GEAR_MANUAL_6 = 9,
|
|
GEAR_UNKNOWN = 0xFF
|
|
} 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)
|
|
|
|
// Fault code bitmap (fault_codes field). NOTE: these are OUR derived faults, not
|
|
// DDE DTCs — real fault codes are not broadcast on this bus and would require
|
|
// diagnostic request/response (see wiki/pages/can-id-candidates.md).
|
|
#define FAULT_GEAR_MISMATCH (1 << 0) // lever (0x1D2) and gearbox (0x0BA) disagree
|
|
|
|
// ============================================================================
|
|
// CAN BUS ID MAPPINGS (BMW-Specific)
|
|
// ============================================================================
|
|
|
|
// BMW E60/E65 PT-CAN, 500 kbps. Every ID below is an 11-bit STANDARD frame and
|
|
// has been seen live on the actual swap bus. Decodes cross-checked against
|
|
// HeinrichG-V12/E65_ReverseEngineering (per-ID DBC docs) AND our own captures.
|
|
// Full evidence + confidence labels: wiki/pages/can-id-candidates.md
|
|
//
|
|
// The original map (0x4F/0x55/0x201/0x1001/0x101F) was placeholder junk — those
|
|
// IDs were verified ABSENT from the bus 2026-07-15. Do not resurrect them.
|
|
|
|
// --- 0x1D0 "Engine Data" (DME/DDE, DLC 8, 100 ms) --------------------------
|
|
#define CAN_ID_ENGINE_DATA 0x1D0
|
|
#define CAN_BYTE_COOLANT_TEMP 0 // TEMP_ENG: °C = raw - 48 (verified)
|
|
#define CAN_BYTE_ENGINE_OIL_TEMP 1 // TEMP_EOI: °C = raw - 48 (reads 0xFF here — sensor not populated on this M57N)
|
|
#define CAN_BYTE_ENGINE_STATUS 2 // bits 4-5 = ST_ENGINE_RUN
|
|
#define ENGINE_TEMP_OFFSET 48 // subtract from raw
|
|
|
|
// ST_ENGINE_RUN (byte 2, bits 4-5)
|
|
#define ENGINE_RUN_OFF 0
|
|
#define ENGINE_RUN_STARTING 1
|
|
#define ENGINE_RUN_RUNNING 2
|
|
#define ENGINE_RUN_INVALID 3
|
|
|
|
// --- 0x0B5 "Torque Request EGS" (EGS, DLC 8, 10 ms) ------------------------
|
|
#define CAN_ID_TORQUE_EGS 0x0B5
|
|
#define CAN_BYTE_GEARBOX_TEMP 7 // TEMP_GRB: °C = raw - 40 (verified two ways)
|
|
#define GEARBOX_TEMP_OFFSET 40 // NOT 0, NOT +40 — see wiki; a 40 °C error either way
|
|
#define CAN_BYTE_GEARBOX_COOL 4 // bits 38-39 = ST_OTMP_GRB (cooling demand)
|
|
|
|
// ST_OTMP_GRB — the box's own overheat opinion (better than thresholding temp)
|
|
#define GEARBOX_COOL_NONE 0
|
|
#define GEARBOX_COOL_STAGE1 1
|
|
#define GEARBOX_COOL_STAGE2 2
|
|
#define GEARBOX_COOL_INVALID 3
|
|
|
|
// --- 0x1D2 "Display Gearbox Data" (EGS, DLC 6, 200 ms) ---------------------
|
|
// Our decode is AHEAD of the published source here: the E65 repo lists this ID
|
|
// with an empty signal table. Byte 1 and byte 4 are our own work.
|
|
#define CAN_ID_GEAR_DISPLAY 0x1D2
|
|
#define CAN_BYTE_GEAR_LEVER 0 // whole-byte lever code
|
|
#define CAN_BYTE_GEAR_ENGAGED 1 // (raw >> 4) - 4 = gear 1..6; 0x0F = none
|
|
#define CAN_BYTE_GEAR_MODE 4 // 0xF0 auto / 0xF1 Sport / 0xF2 Manual
|
|
|
|
// Byte 0 lever codes (verified — every code matches the documented table)
|
|
#define GEAR_CODE_PARK 0xE1
|
|
#define GEAR_CODE_REVERSE 0xD2
|
|
#define GEAR_CODE_NEUTRAL 0xB4
|
|
#define GEAR_CODE_DRIVE 0x78 // D *and* S/M — the side-gate is physically the same detent
|
|
|
|
// Byte 4 mode codes. NOTE: 0xF2 is a PERSISTENT mode, not a downshift event —
|
|
// it holds across gears 2..6. The +/- nudges have no distinct code; the first
|
|
// tap simply promotes Sport -> Manual.
|
|
#define GEAR_MODE_DRIVE_AUTO 0xF0
|
|
#define GEAR_MODE_SPORT 0xF1
|
|
#define GEAR_MODE_MANUAL 0xF2
|
|
|
|
#define GEAR_ENGAGED_NONE 0x0F // byte 1 value in P/R/N — and in R even when engaged
|
|
|
|
// --- 0x0BA gearbox state (EGS, DLC 8, 20 ms) -------------------------------
|
|
// The ONLY source of engaged reverse: 0x1D2 byte 1 stays 0x0F in R.
|
|
#define CAN_ID_GEARBOX_STATE 0x0BA
|
|
#define CAN_BYTE_GEARBOX_TARGET 0 // low nibble = ST_GR_GRB
|
|
|
|
// ST_GR_GRB (byte 0, low nibble). Every value 1/2/3/5..0xA observed live in the
|
|
// 2026-07-17 drive capture. Value 4 remains unaccounted for.
|
|
// This nibble alone is a COMPLETE gear source: P/R/N plus 1st..6th, reverse
|
|
// included. 0x1D2 is still needed for the lever and the D/S/M mode, which this
|
|
// message does not carry.
|
|
#define GRB_TARGET_NEUTRAL 1
|
|
#define GRB_TARGET_REVERSE 2
|
|
#define GRB_TARGET_PARK 3
|
|
#define GRB_TARGET_GEAR1 5 // ..0xA = 6th
|
|
#define GRB_TARGET_GEAR6 0xA
|
|
#define GRB_TARGET_INVALID 0xF
|
|
|
|
// Byte 0 bits 6-7: ST_CCLT, torque converter lockup clutch. Relevant to any
|
|
// turbine-vs-engine ratio test: a CLOSED clutch makes turbine == engine exactly,
|
|
// so ratio ~1.0 at road speed means "locked up", not "not engaged".
|
|
#define CAN_BYTE_CONVERTER_CLUTCH 0
|
|
#define CCLT_DISENGAGED 0
|
|
#define CCLT_CONTROLLED 1
|
|
#define CCLT_CLOSED 2
|
|
#define CCLT_INVALID 3
|
|
|
|
// Bytes 3-4: output shaft speed, 16-bit little-endian. UNDOCUMENTED in every
|
|
// source. Identification is verified (rises monotonically 1st->6th under drive,
|
|
// zero whenever the output is held, including engaged-in-D). This is the
|
|
// project's only road-speed signal — there is no DSC/wheel-speed module on this
|
|
// bus.
|
|
//
|
|
// SCALING IS UNSETTLED. The upshift-schedule test earlier suggested raw * 0.125
|
|
// = rpm, but a direct Launch reference reading (128 rpm at raw 1192, 2026-07-17)
|
|
// points to ~0.107 instead and contradicts 0.125. One eyeballed point isn't a
|
|
// calibration, so this stays RAW COUNTS until a multi-point Launch sweep fixes
|
|
// the factor. Do not convert to a physical unit here.
|
|
// See wiki/pages/driveline-speed-signals.md
|
|
#define CAN_BYTE_OUTPUT_SHAFT_L 3
|
|
#define CAN_BYTE_OUTPUT_SHAFT_H 4
|
|
|
|
// --- 0x1A2 turbine speed (EGS, DLC 4, 20 ms) -------------------------------
|
|
// Gearbox INPUT speed. Physically proves engagement: free-spins at ~engine
|
|
// speed in P/N, collapses to ~0 when the box engages against a held output.
|
|
#define CAN_ID_TURBINE_SPEED 0x1A2
|
|
#define CAN_BYTE_TURBINE_L 0
|
|
#define CAN_BYTE_TURBINE_H 1
|
|
|
|
// --- 0x0A8 "Torque 1" (DME/DDE, DLC 8, 10 ms) ------------------------------
|
|
// TORQ_AVL = torque the engine has AVAILABLE/is delivering. (0x0AA's TORQ_DVCH
|
|
// is driver DEMAND — same bit layout, so both read ~27 Nm at idle, but they
|
|
// diverge under load. This is the one to display as "engine torque".)
|
|
#define CAN_ID_TORQUE_1 0x0A8
|
|
#define CAN_BYTE_TORQUE_L 1 // TORQ_AVL low (byte 1) — see can_to_torque_x10
|
|
#define CAN_BYTE_TORQUE_H 2 // TORQ_AVL high (byte 2)
|
|
|
|
// --- 0x0AA "Torque 3" (DME/DDE, DLC 8, 10 ms) ------------------------------
|
|
#define CAN_ID_TORQUE_3 0x0AA
|
|
#define CAN_BYTE_PEDAL_POS 3 // ANG_ACPD: % = raw * 0.39063
|
|
#define CAN_BYTE_RPM_L 4 // RPM_ENG: 1/min = raw16 * 0.25
|
|
#define CAN_BYTE_RPM_H 5
|
|
#define CAN_BYTE_CRUISE_STATE 6 // low nibble = ST_CLCTR_V
|
|
|
|
// --- 0x3B4 "Power Management Battery Voltage" (DLC 8, 5 s) -----------------
|
|
#define CAN_ID_BATTERY_VOLTAGE 0x3B4
|
|
#define CAN_BYTE_BATTERY_VOLT_L 0 // U_BT: 12-bit little-endian, V = raw * 0.015
|
|
#define CAN_BYTE_BATTERY_VOLT_H 1 // low nibble only (high nibble = ST_BTSW)
|
|
#define BATTERY_RAW_INVALID 0xFFF // U_BT's not-available marker
|
|
|
|
// Byte 2 low nibble. The E65 repo calls this ST_ENGINE_RUN and the E60 VFL sheet
|
|
// calls it an engine-running flag with 0x09=running — BOTH ARE WRONG FOR US.
|
|
// Verified on this bus 2026-07-17: it tracks IGNITION, inverted, and does not
|
|
// move when the engine starts. Use 0x1D0 ST_ENGINE_RUN for engine state.
|
|
#define CAN_BYTE_IGNITION 2
|
|
#define IGNITION_ON 0x00
|
|
#define IGNITION_OFF 0x09
|
|
|
|
// All IDs are 11-bit standard frames — the extended-ID assumption came from the
|
|
// bogus 0x1001/0x101F placeholders and is dead.
|
|
#define USE_EXTENDED_CAN_IDS false
|
|
|
|
// ============================================================================
|
|
// 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 Value to SUBTRACT from raw (48 for 0x1D0 engine temps, 40 for 0x0B5)
|
|
* @return Temperature in 0.1°C units
|
|
*/
|
|
static inline int16_t can_to_temperature(uint8_t can_value, int16_t offset) {
|
|
return (int16_t)(((int16_t)can_value - offset) * 10);
|
|
}
|
|
|
|
/**
|
|
* Extract the raw 12-bit U_BT field from a 0x3B4 frame.
|
|
* The high nibble of byte 1 is ST_BTSW, NOT voltage — mask it off or you'll read
|
|
* ~4x too high.
|
|
* @param d Payload of a 0x3B4 frame (needs DLC >= 2)
|
|
* @return Raw 12-bit value; BATTERY_RAW_INVALID (0xFFF) means "not available"
|
|
*/
|
|
static inline uint16_t can_battery_raw(const uint8_t* d) {
|
|
return (uint16_t)(((d[CAN_BYTE_BATTERY_VOLT_H] & 0x0F) << 8) | d[CAN_BYTE_BATTERY_VOLT_L]);
|
|
}
|
|
|
|
/**
|
|
* Decode battery voltage from a 0x3B4 frame.
|
|
* U_BT is 12-bit little-endian, factor 0.015 V — i.e. 0.01 V units = raw * 1.5.
|
|
* (Source example: raw 0x3D9 = 985 -> 14.775 V.)
|
|
*
|
|
* CAVEAT: on this bus the field reads BATTERY_RAW_INVALID almost always — the
|
|
* Power Module appears not to be retained in the swap. Check can_battery_raw()
|
|
* against BATTERY_RAW_INVALID before calling this; 0xFFF would otherwise decode
|
|
* to a confident-looking 61 V. The factor itself is also unconfirmed against a
|
|
* meter (0.015 here vs ~0.0147 implied by the E60 VFL sheet).
|
|
* See wiki/pages/can-id-candidates.md.
|
|
*
|
|
* @param d Payload of a 0x3B4 frame (needs DLC >= 2)
|
|
* @return Voltage in 0.01V units
|
|
*/
|
|
static inline uint16_t can_to_voltage(const uint8_t* d) {
|
|
return (uint16_t)((can_battery_raw(d) * 3) / 2); // raw * 0.015 V -> 0.01 V units
|
|
}
|
|
|
|
/**
|
|
* Decode engine RPM from a 0x0AA frame. RPM_ENG = raw16 (Intel) * 0.25.
|
|
* @param d Payload of a 0x0AA frame (needs DLC >= 6)
|
|
*/
|
|
static inline uint16_t can_to_rpm(const uint8_t* d) {
|
|
uint16_t raw = (uint16_t)((d[CAN_BYTE_RPM_H] << 8) | d[CAN_BYTE_RPM_L]);
|
|
return (uint16_t)(raw / 4);
|
|
}
|
|
|
|
/**
|
|
* Decode available engine torque (TORQ_AVL) from a 0x0A8 frame.
|
|
*
|
|
* The field is a SIGNED 12-bit value at bits 12-23, factor 0.5 Nm. The user's
|
|
* spreadsheet formula (D2*256 + D1)/32 lands on the same number — algebraically
|
|
* (D2<<8|D1)/32 == ((D2<<4)|(D1>>4)) * 0.5 — but it sweeps D1's low nibble into
|
|
* the result, and those four bits are ALIV_TORQ_1_DME (the alive counter), not
|
|
* torque. That adds up to ~0.5 Nm of jitter. We mask it off and sign-extend so
|
|
* overrun/braking (negative torque) reads correctly. Verified against the repo's
|
|
* 0x0A8 worked example: C7 08 03 00 ... -> 24 Nm, with ALIV=8 in D1's low nibble.
|
|
*
|
|
* @param d Payload of a 0x0A8 frame (needs DLC >= 3)
|
|
* @return Torque in 0.1 Nm units, signed
|
|
*/
|
|
static inline int16_t can_to_torque_x10(const uint8_t* d) {
|
|
uint16_t raw12 = (uint16_t)((d[CAN_BYTE_TORQUE_H] << 4) | (d[CAN_BYTE_TORQUE_L] >> 4));
|
|
int16_t s = (raw12 & 0x800) ? (int16_t)(raw12 | 0xF000) // sign-extend 12->16 bit
|
|
: (int16_t)raw12;
|
|
return (int16_t)(s * 5); // * 0.5 Nm, expressed in 0.1 Nm units
|
|
}
|
|
|
|
/**
|
|
* Decode gearbox turbine (input) speed from a 0x1A2 frame.
|
|
* RPM_GRB_TURB = raw16 (Intel) * 0.125 + 6.
|
|
* @param d Payload of a 0x1A2 frame (needs DLC >= 2)
|
|
*/
|
|
static inline uint16_t can_to_turbine_rpm(const uint8_t* d) {
|
|
uint16_t raw = (uint16_t)((d[CAN_BYTE_TURBINE_H] << 8) | d[CAN_BYTE_TURBINE_L]);
|
|
return (uint16_t)(raw / 8 + 6);
|
|
}
|
|
|
|
/**
|
|
* Map a raw 0x1D2 byte-0 lever code to a gear_position_t.
|
|
* D and S/M share code 0x78 — use decode_gear() to resolve them.
|
|
* @param d0 Byte 0 of a 0x1D2 frame
|
|
* @return Lever position, or GEAR_UNKNOWN if the code isn't recognized
|
|
*/
|
|
static inline gear_position_t can_to_gear_lever(uint8_t d0) {
|
|
switch (d0) {
|
|
case GEAR_CODE_PARK: return GEAR_PARK;
|
|
case GEAR_CODE_REVERSE: return GEAR_REVERSE;
|
|
case GEAR_CODE_NEUTRAL: return GEAR_NEUTRAL;
|
|
case GEAR_CODE_DRIVE: return GEAR_DRIVE; // D or S/M — see decode_gear()
|
|
default: return GEAR_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extract the engaged gear number from 0x1D2 byte 1.
|
|
* High nibble is a +16 ladder: 0x5F=1st .. 0xAF=6th. 0x0F = no numbered gear.
|
|
* @param d1 Byte 1 of a 0x1D2 frame
|
|
* @return 1..6, or 0 if no gear is engaged / value out of range.
|
|
* Always 0 in REVERSE — reverse is unnumbered here; use 0x0BA instead.
|
|
*/
|
|
static inline uint8_t can_to_engaged_gear(uint8_t d1) {
|
|
if (d1 == GEAR_ENGAGED_NONE) return 0;
|
|
uint8_t n = (uint8_t)((d1 >> 4) - 4);
|
|
return (n >= 1 && n <= 6) ? n : 0;
|
|
}
|
|
|
|
/**
|
|
* Resolve the full 0x1D2 frame into a single displayable gear_position_t.
|
|
*
|
|
* P/R/N come straight from the lever. In the D detent, byte 4 splits plain Drive
|
|
* from the Sport/Manual side-gate; when in S/M we surface the engaged gear number
|
|
* (GEAR_MANUAL_1..6) because that's what a driver in the side-gate wants to read.
|
|
*
|
|
* @param d Payload of a 0x1D2 frame
|
|
* @param dlc Frame DLC (must be >= 5 to reach the mode byte)
|
|
* @return Gear to display, or GEAR_UNKNOWN if the lever code isn't recognized
|
|
*/
|
|
static inline gear_position_t decode_gear(const uint8_t* d, uint8_t dlc) {
|
|
if (dlc <= CAN_BYTE_GEAR_LEVER) return GEAR_UNKNOWN;
|
|
|
|
gear_position_t lever = can_to_gear_lever(d[CAN_BYTE_GEAR_LEVER]);
|
|
if (lever != GEAR_DRIVE) return lever; // P/R/N (or UNKNOWN) need no further work
|
|
|
|
// In the D detent. Without the mode byte we can't tell D from S/M — report D.
|
|
if (dlc <= CAN_BYTE_GEAR_MODE) return GEAR_DRIVE;
|
|
if (d[CAN_BYTE_GEAR_MODE] == GEAR_MODE_DRIVE_AUTO) return GEAR_DRIVE;
|
|
|
|
// Sport or Manual: show the engaged gear if the box has told us one.
|
|
if (dlc > CAN_BYTE_GEAR_ENGAGED) {
|
|
uint8_t n = can_to_engaged_gear(d[CAN_BYTE_GEAR_ENGAGED]);
|
|
if (n >= 1 && n <= 6) return (gear_position_t)(GEAR_MANUAL_1 + (n - 1));
|
|
}
|
|
return GEAR_DRIVE;
|
|
}
|
|
|
|
/**
|
|
* Map a 0x0BA ST_GR_GRB nibble (the gearbox's own target gear) to a
|
|
* gear_position_t, for cross-checking against the lever.
|
|
* @param nibble Low nibble of 0x0BA byte 0
|
|
* @return Target gear, or GEAR_UNKNOWN if invalid/unrecognized
|
|
*/
|
|
static inline gear_position_t can_to_target_gear(uint8_t nibble) {
|
|
switch (nibble) {
|
|
case GRB_TARGET_PARK: return GEAR_PARK;
|
|
case GRB_TARGET_REVERSE: return GEAR_REVERSE;
|
|
case GRB_TARGET_NEUTRAL: return GEAR_NEUTRAL;
|
|
default: break;
|
|
}
|
|
// 5..0xA = forward gears 1..6 (same ladder as 0x1D2 byte 1)
|
|
if (nibble >= GRB_TARGET_GEAR1 && nibble <= GRB_TARGET_GEAR6) return GEAR_DRIVE;
|
|
return GEAR_UNKNOWN;
|
|
}
|
|
|
|
/**
|
|
* Extract the engaged gear number from a 0x0BA ST_GR_GRB nibble.
|
|
* Unlike 0x1D2 byte 1, this ladder is the whole picture — but it numbers only
|
|
* the forward gears, so P/R/N still return 0.
|
|
* @param nibble Low nibble of 0x0BA byte 0
|
|
* @return 1..6, or 0 if not in a numbered forward gear
|
|
*/
|
|
static inline uint8_t can_to_target_gear_num(uint8_t nibble) {
|
|
if (nibble < GRB_TARGET_GEAR1 || nibble > GRB_TARGET_GEAR6) return 0;
|
|
return (uint8_t)(nibble - GRB_TARGET_GEAR1 + 1);
|
|
}
|
|
|
|
/**
|
|
* 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_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";
|
|
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.%d°C", 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
|