102 lines
4.6 KiB
C#
102 lines
4.6 KiB
C#
using TuneBoss.Core.Protocol;
|
|
|
|
namespace TuneBoss.Core.Models;
|
|
|
|
/// <summary>
|
|
/// Throttle-position-sensor calibration rules, resolved verbatim from the legacy
|
|
/// WinForms (<c>saveUserReadInfo</c> / TPS page) and Android (<c>TpsCalPage</c> /
|
|
/// <c>BtApp.sendTpsCalWrite</c>) sources.
|
|
///
|
|
/// The calibration captures the raw TPS ADC (<see cref="DiagnosticsFrame.TPS_Raw"/>,
|
|
/// 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.
|
|
/// </summary>
|
|
public static class TpsCalibration
|
|
{
|
|
public readonly record struct Addresses(
|
|
ushort CalWrite,
|
|
ushort AbsBounds,
|
|
ushort AdvSettings,
|
|
bool AdvSupported);
|
|
|
|
/// <summary>Minimum firmware that exposes the advanced TPS settings byte.</summary>
|
|
public const int AdvMinFirmware = 152;
|
|
|
|
/// <summary>Number of full open/close sweeps the guide asks the tuner to perform.</summary>
|
|
public const int RequiredSweeps = 5;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>Resolves the calibration addresses for a generation + firmware.</summary>
|
|
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),
|
|
};
|
|
|
|
/// <summary>Convenience overload resolving the generation from <see cref="EcuInfo"/>.</summary>
|
|
public static Addresses For(EcuInfo info) =>
|
|
For((GenerationType)info.AppInfoVersion, info.FwVersion);
|
|
|
|
/// <summary>
|
|
/// Builds the 8-byte 1D calibration payload exactly as the legacy
|
|
/// <c>sendTpsCalWrite</c> 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.
|
|
/// </summary>
|
|
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
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replicates the legacy acceptance test against the absolute bounds read from
|
|
/// <see cref="Addresses.AbsBounds"/> ([0] = absMax, [1] = absMin).
|
|
/// </summary>
|
|
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}.",
|
|
};
|
|
}
|