namespace TuneBoss.Core.Models; /// /// 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); /// public static class TableTracker { /// /// Finds the highlight and adjacent indices for a live value against an axis array. /// /// The table axis (RPM ÷100 bytes or load bytes). /// The live sensor value in the same units as the axis. /// /// (highlight, adjacent) where -1 means "out of range on that side". /// 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); } } /// /// Produces a for a tuning table from live frame data. /// /// Table RPM axis (raw bytes, each × 100 = RPM). /// Table load axis (raw bytes). /// Live RPM raw value from DiagnosticsFrame.RPM_Raw. /// Live load/TPS value — use frame.Load for main tables, /// frame.TPS_Percent for user-adjust tables. 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); } /// /// 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. /// public static IReadOnlyDictionary CalculateAll( EcuSession session, DiagnosticsFrame frame) { var result = new Dictionary(); 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 ─────────────────────────────────────────────────────────────── /// /// Integer midpoint of two adjacent axis values. /// Matches (rpmArrayRef[i] + rpmArrayRef[i+1]) / 2 in the original code. /// Uses integer division — no rounding. /// private static byte Midpoint(byte[] axis, int i, int j) => (byte)((axis[i] + axis[j]) / 2); }