159 lines
6.6 KiB
C#
159 lines
6.6 KiB
C#
namespace TuneBoss.Core.Models;
|
||
|
||
/// <summary>
|
||
/// Calculates which table cells to highlight during live diagnostics.
|
||
///
|
||
/// Given a live sensor value and a table's axis array, returns two indices:
|
||
/// [0] = the primary (exact) cell — the bracket the value falls into
|
||
/// [1] = the adjacent cell — either the previous or next bracket
|
||
/// (-1 means "before first" or "after last" — no secondary highlight)
|
||
///
|
||
/// ── Algorithm ────────────────────────────────────────────────────────────────
|
||
/// For each pair of adjacent axis values, the midpoint defines the boundary
|
||
/// between brackets. The live value is assigned to the bracket whose midpoint
|
||
/// boundary it falls within, then the adjacent bracket is the one whose axis
|
||
/// value is closest to the live value.
|
||
///
|
||
/// Reverse-engineered from getRpmToHighlight() and getLoadToHighlight() in the
|
||
/// WinForms app. Both functions are identical in structure — only the axis
|
||
/// length differs (24 for RPM, 8 for Load/TPS).
|
||
///
|
||
/// ── Usage ────────────────────────────────────────────────────────────────────
|
||
/// var (col, adjCol) = TableTracker.FindIndex(table.RpmAxis, frame.RPM_Raw);
|
||
/// var (row, adjRow) = TableTracker.FindIndex(table.LoadAxis, frame.Load);
|
||
///
|
||
/// // TPS-keyed tables (user-adjust: diag4/5/10/11/12)
|
||
/// var (row, adjRow) = TableTracker.FindIndex(table.LoadAxis, frame.TPS_Percent);
|
||
/// </summary>
|
||
public static class TableTracker
|
||
{
|
||
/// <summary>
|
||
/// Finds the highlight and adjacent indices for a live value against an axis array.
|
||
/// </summary>
|
||
/// <param name="axis">The table axis (RPM ÷100 bytes or load bytes).</param>
|
||
/// <param name="value">The live sensor value in the same units as the axis.</param>
|
||
/// <returns>
|
||
/// (highlight, adjacent) where -1 means "out of range on that side".
|
||
/// </returns>
|
||
public static (int Highlight, int Adjacent) FindIndex(byte[] axis, byte value)
|
||
{
|
||
int n = axis.Length;
|
||
if (n == 0) return (0, -1);
|
||
if (n == 1) return (0, -1);
|
||
|
||
// Below first midpoint → bracket 0
|
||
if (value < Midpoint(axis, 0, 1))
|
||
{
|
||
int adj = value < axis[0] ? -1 : 1;
|
||
return (0, adj);
|
||
}
|
||
|
||
// Middle brackets
|
||
for (int i = 1; i < n - 1; i++)
|
||
{
|
||
int lo = Midpoint(axis, i - 1, i);
|
||
int hi = Midpoint(axis, i, i + 1);
|
||
|
||
if (lo <= value && value < hi)
|
||
{
|
||
int adj = value < axis[i] ? i - 1 : i + 1;
|
||
return (i, adj);
|
||
}
|
||
}
|
||
|
||
// Above last midpoint → bracket n-1
|
||
{
|
||
int adj = value < axis[n - 1] ? n - 2 : -1;
|
||
return (n - 1, adj);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Produces a <see cref="DiagnosticsTracker"/> for a tuning table from live frame data.
|
||
/// </summary>
|
||
/// <param name="rpmAxis">Table RPM axis (raw bytes, each × 100 = RPM).</param>
|
||
/// <param name="loadAxis">Table load axis (raw bytes).</param>
|
||
/// <param name="rpmRaw">Live RPM raw value from DiagnosticsFrame.RPM_Raw.</param>
|
||
/// <param name="loadValue">Live load/TPS value — use frame.Load for main tables,
|
||
/// frame.TPS_Percent for user-adjust tables.</param>
|
||
public static DiagnosticsTracker Calculate(
|
||
byte[] rpmAxis,
|
||
byte[] loadAxis,
|
||
byte rpmRaw,
|
||
byte loadValue)
|
||
{
|
||
var (col, adjCol) = FindIndex(rpmAxis, rpmRaw);
|
||
var (row, adjRow) = FindIndex(loadAxis, loadValue);
|
||
|
||
return new DiagnosticsTracker(
|
||
ColHighlight: col,
|
||
ColLowlight: adjCol,
|
||
RowHighlight: row,
|
||
RowLowlight: adjRow);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Calculates trackers for all loaded tables in a session from one frame.
|
||
/// Returns a dictionary keyed by table name.
|
||
/// Only includes tables whose axes are loaded.
|
||
/// </summary>
|
||
public static IReadOnlyDictionary<string, DiagnosticsTracker> CalculateAll(
|
||
EcuSession session,
|
||
DiagnosticsFrame frame)
|
||
{
|
||
var result = new Dictionary<string, DiagnosticsTracker>();
|
||
|
||
byte rpm = frame.RPM_Raw;
|
||
byte load = frame.Load;
|
||
byte tps = frame.TPS_Percent;
|
||
|
||
void Add(string name, TuningTable? t, byte axisValue)
|
||
{
|
||
if (t == null) return;
|
||
result[name] = Calculate(t.RpmAxis, t.LoadAxis, rpm, axisValue);
|
||
}
|
||
|
||
void AddAfr(string name, AfrTable? t, byte axisValue)
|
||
{
|
||
if (t == null) return;
|
||
result[name] = Calculate(t.RpmAxis, t.LoadAxis, rpm, axisValue);
|
||
}
|
||
|
||
// Cyl1 main tables — load-keyed
|
||
Add("Fuel_PulseWidth1", session.Fuel_PulseWidth1, load);
|
||
Add("Spark_Angle1", session.Spark_Angle1, load);
|
||
Add("Fuel_Angle2D", session.Fuel_Angle2D, load);
|
||
Add("Secondary_Load_Conversion", session.Secondary_Load_Conversion, load);
|
||
AddAfr("AFR_Target1", session.AFR_Target1, load);
|
||
|
||
// Cyl1 user-adjust — TPS-keyed
|
||
Add("Fuel_PulseWidth_Adjust1", session.Fuel_PulseWidth_Adjust1, tps);
|
||
Add("Spark_Angle_Adjust1", session.Spark_Angle_Adjust1, tps);
|
||
|
||
if (session.IsTwoCylinder)
|
||
{
|
||
// Cyl2 main tables — load-keyed
|
||
Add("Fuel_PulseWidth2", session.Fuel_PulseWidth2, load);
|
||
Add("Spark_Angle2", session.Spark_Angle2, load);
|
||
Add("Fuel_Angle2D_2", session.Fuel_Angle2D_2, load);
|
||
AddAfr("AFR_Target2", session.AFR_Target2, load);
|
||
|
||
// Cyl2 user-adjust — TPS-keyed
|
||
Add("Fuel_PulseWidth_Adjust2", session.Fuel_PulseWidth_Adjust2, tps);
|
||
Add("Spark_Angle_Adjust2", session.Spark_Angle_Adjust2, tps);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// Integer midpoint of two adjacent axis values.
|
||
/// Matches (rpmArrayRef[i] + rpmArrayRef[i+1]) / 2 in the original code.
|
||
/// Uses integer division — no rounding.
|
||
/// </summary>
|
||
private static byte Midpoint(byte[] axis, int i, int j)
|
||
=> (byte)((axis[i] + axis[j]) / 2);
|
||
}
|