using TuneBoss.Core.Protocol; namespace TuneBoss.Core.Models; /// /// Throttle-position-sensor calibration rules, resolved verbatim from the legacy /// WinForms (saveUserReadInfo / TPS page) and Android (TpsCalPage / /// BtApp.sendTpsCalWrite) sources. /// /// The calibration captures the raw TPS ADC (, /// channel [18]) at closed and wide-open throttle over several sweeps, validates the /// captured min/max against the per-unit absolute bounds, then writes an 8-byte 1D /// block and sets the first-use flag. /// /// ── Generation addressing (absolute ECU addresses) ─────────────────────────── /// Gen2 / Gen3 (small-address layout — PG1/EZ115 is Gen3): /// CalWrite = 0x0078 (120) ← 8-byte calibration block /// AbsBounds = 0x0088 (136) ← [max, min, store2, store3] sanity bounds /// AdvSettings = 0x0094 (148) ← advanced TPS toggle (FW ≥ 152) /// Gen1 (legacy ~1788 range): CalWrite = 0x06FC, AbsBounds = 0x070C, no adv. /// public static class TpsCalibration { public readonly record struct Addresses( ushort CalWrite, ushort AbsBounds, ushort AdvSettings, bool AdvSupported); /// Minimum firmware that exposes the advanced TPS settings byte. public const int AdvMinFirmware = 152; /// Number of full open/close sweeps the guide asks the tuner to perform. public const int RequiredSweeps = 5; /// /// FirstUseInfo[0] sentinel (READ_INFO buf[32], the DD slot, overloaded as a flag /// on Gen2/Gen3). The unit is considered calibrated iff this byte == 1. /// public const byte CalDoneFlag = 1; // Absolute-bounds editor limits (legacy long-press editor ranges). public const byte AbsMaxFloor = 180, AbsMaxCeil = 198; public const byte AbsMinFloor = 2, AbsMinCeil = 10; /// Resolves the calibration addresses for a generation + firmware. public static Addresses For(GenerationType generation, byte fwVersion) => generation switch { GenerationType.Gen1 => new Addresses(0x06FC, 0x070C, 0x0000, AdvSupported: false), _ => new Addresses(0x0078, 0x0088, 0x0094, AdvSupported: fwVersion >= AdvMinFirmware), }; /// Convenience overload resolving the generation from . public static Addresses For(EcuInfo info) => For((GenerationType)info.AppInfoVersion, info.FwVersion); /// /// Builds the 8-byte 1D calibration payload exactly as the legacy /// sendTpsCalWrite does: [min, max, max+2, max+4, max+6, max+8, max+10, max+12]. /// The trailing ramp defines the TPS% curve above wide-open throttle. /// public static byte[] BuildCalWritePayload(int minRaw, int maxRaw) { byte m = (byte)(maxRaw & 0xFF); return new[] { (byte)(minRaw & 0xFF), m, (byte)(m + 2), (byte)(m + 4), (byte)(m + 6), (byte)(m + 8), (byte)(m + 10), (byte)(m + 12), }; } public enum Validation { Ok, MinBelowFloor, // captured closed-throttle raw fell below the absolute minimum MaxAboveCeiling, // captured WOT raw rose above the absolute maximum Extreme, // crossed/implausible values } /// /// Replicates the legacy acceptance test against the absolute bounds read from /// ([0] = absMax, [1] = absMin). /// public static Validation Validate(int minRaw, int maxRaw, int absMin, int absMax) { if (minRaw < absMin) return Validation.MinBelowFloor; if (maxRaw > absMax) return Validation.MaxAboveCeiling; if (minRaw > absMax || maxRaw < absMin) return Validation.Extreme; return Validation.Ok; } public static string Describe(Validation v, int minRaw, int maxRaw, int absMin, int absMax) => v switch { Validation.MinBelowFloor => $"Min raw {minRaw} is below the expected minimum of {absMin}. Please check your throttle.", Validation.MaxAboveCeiling => $"Max raw {maxRaw} is above the expected maximum of {absMax}. Please check your throttle.", Validation.Extreme => $"Calibration failed due to extreme TPS values (min {minRaw}, max {maxRaw}). Please check your throttle.", _ => $"Calibration successful — min {minRaw}, max {maxRaw}.", }; }