Initial Commit
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
namespace TuneBoss.Core.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Data model for a subtable (AFR_Target_SubTable, Fuel_PulseWidth_Adjust_SubTable,
|
||||
/// Spark_Angle_Adjust_SubTable — one per cylinder pair).
|
||||
///
|
||||
/// A subtable is a small 2D correction grid: 4 load rows × 6 RPM columns,
|
||||
/// always signed sbyte values. It provides a secondary axis multiplier on
|
||||
/// top of the main 2D table, keyed by RPM range pairs rather than absolute RPM.
|
||||
///
|
||||
/// ── Wire format — single read/write, no split ─────────────────────────────────
|
||||
///
|
||||
/// Field Size Notes
|
||||
/// ─────────── ────── ────────────────────────────────────────────────────
|
||||
/// Data cells 24 B sbyte[4 rows × 6 cols], row-major, always signed
|
||||
/// Load axis 4 B byte[4] — fixed default {5, 15, 40, 70}
|
||||
/// RPM axis 12 B byte[12] — pairs: [lo÷100, hi÷100] × 6 columns
|
||||
///
|
||||
/// Total 40 B = dataInArray(24) + loadInArray(4) + rpmInArray(12)
|
||||
///
|
||||
/// ── Key differences from TuningTable ─────────────────────────────────────────
|
||||
/// - Always signed sbyte (no sign flag — hard-coded in the UI's Convert.ToSByte)
|
||||
/// - Smaller grid: 4 rows × 6 cols vs 8 rows × 24 cols
|
||||
/// - RPM axis uses pairs (low/high range per column), not single values
|
||||
/// Each RPM cell displays as "loRPM\nhiRPM" (two lines)
|
||||
/// - No modifier bytes (no MaxRaw/MinRaw/Mult/Sign trailer)
|
||||
/// - Write sends only dataOutArray (no RPM/load arrays on write)
|
||||
/// confirmed from writeSubTableFunctionThread: userDataArrayFromTable = sttp.dataOutArray
|
||||
/// - Read response length comes from _tableMemReadBuffer[2] (Fix 1 in CheckNPreturn)
|
||||
///
|
||||
/// ── Default load axis ─────────────────────────────────────────────────────────
|
||||
/// Fixed at {5, 15, 40, 70} — hardcoded in SubTuningTablePanel.InitializeTable()
|
||||
/// </summary>
|
||||
public sealed record SubTable
|
||||
{
|
||||
public const int Rows = 4;
|
||||
public const int Cols = 6;
|
||||
public const int DataBytes = Rows * Cols; // 24
|
||||
public const int LoadBytes = Rows; // 4
|
||||
public const int RpmBytes = Cols * 2; // 12 (lo+hi pair per column)
|
||||
public const int TotalBytes = DataBytes + LoadBytes + RpmBytes; // 40
|
||||
|
||||
// ── Default axis values ───────────────────────────────────────────────────
|
||||
public static readonly byte[] DefaultLoadAxis = { 5, 15, 40, 70 };
|
||||
|
||||
// ── Raw wire arrays ───────────────────────────────────────────────────────
|
||||
/// <summary>
|
||||
/// 24 bytes — cell data, row-major [row * 6 + col].
|
||||
/// Always signed sbyte on wire (stored as byte, interpreted as sbyte).
|
||||
/// </summary>
|
||||
public byte[] Data { get; init; } = new byte[DataBytes];
|
||||
|
||||
/// <summary>4 bytes — load axis values. Default: {5, 15, 40, 70}.</summary>
|
||||
public byte[] LoadAxis { get; init; } = (byte[])DefaultLoadAxis.Clone();
|
||||
|
||||
/// <summary>
|
||||
/// 12 bytes — RPM axis pairs, [lo÷100, hi÷100] interleaved.
|
||||
/// rpmInArray[col*2] = lo RPM ÷ 100, rpmInArray[col*2+1] = hi RPM ÷ 100.
|
||||
/// Display: "loRPM\nhiRPM" (two lines per cell in the UI).
|
||||
/// </summary>
|
||||
public byte[] RpmAxis { get; init; } = new byte[RpmBytes];
|
||||
|
||||
// ── Derived display values ────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Gets the signed display value for a data cell.
|
||||
/// SubTable cells are always sbyte — matches Convert.ToSByte() in the UI.
|
||||
/// </summary>
|
||||
public int GetCell(int row, int col) =>
|
||||
unchecked((sbyte)Data[row * Cols + col]);
|
||||
|
||||
/// <summary>Low RPM bound for a column (display units).</summary>
|
||||
public int GetRpmLo(int col) => RpmAxis[col * 2] * 100;
|
||||
|
||||
/// <summary>High RPM bound for a column (display units).</summary>
|
||||
public int GetRpmHi(int col) => RpmAxis[col * 2 + 1] * 100;
|
||||
|
||||
/// <summary>Load axis value for a row (raw, 0–110).</summary>
|
||||
public int GetLoad(int row) => LoadAxis[row];
|
||||
|
||||
// ── Deserialisation ───────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Parses the device response for a subtable read.
|
||||
/// The response payload starts at byte offset 4 (after the 4-byte header).
|
||||
/// Response length is given by _tableMemReadBuffer[2] per CheckNPreturn Fix 1.
|
||||
///
|
||||
/// Layout matches sendReadSplitTableToPanelThread (subtable path):
|
||||
/// response[4 .. 4+DataBytes-1] → Data
|
||||
/// response[4+DataBytes .. 4+DataBytes+3] → LoadAxis
|
||||
/// response[4+DataBytes+4 .. 4+TotalBytes-1] → RpmAxis
|
||||
/// </summary>
|
||||
public static SubTable FromPayload(byte[] payload)
|
||||
{
|
||||
int expectedMin = 4 + TotalBytes;
|
||||
if (payload.Length < expectedMin)
|
||||
throw new ArgumentException(
|
||||
$"Expected at least {expectedMin} bytes, got {payload.Length}.", nameof(payload));
|
||||
|
||||
const int o = 4;
|
||||
var data = payload[o..(o + DataBytes)];
|
||||
var loadAxis = payload[(o + DataBytes)..(o + DataBytes + LoadBytes)];
|
||||
var rpmAxis = payload[(o + DataBytes + LoadBytes)..(o + TotalBytes)];
|
||||
|
||||
return new SubTable
|
||||
{
|
||||
Data = data,
|
||||
LoadAxis = loadAxis,
|
||||
RpmAxis = rpmAxis,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses when only the data segment length is known from the response header.
|
||||
/// Matches the CheckNPreturn Fix 1 pattern:
|
||||
/// sttp.dataInArray = new byte[_tableMemReadBuffer[2]]
|
||||
/// BlockCopy from _tableMemReadBuffer[4]
|
||||
/// Load/RPM axes use defaults when not included in the response.
|
||||
/// </summary>
|
||||
public static SubTable FromDataOnlyPayload(byte[] payload, int dataLength)
|
||||
{
|
||||
if (payload.Length < 4 + dataLength)
|
||||
throw new ArgumentException(
|
||||
$"Payload too short for {dataLength} data bytes.", nameof(payload));
|
||||
|
||||
var data = new byte[dataLength];
|
||||
Buffer.BlockCopy(payload, 4, data, 0, dataLength);
|
||||
|
||||
return new SubTable
|
||||
{
|
||||
Data = data,
|
||||
LoadAxis = (byte[])DefaultLoadAxis.Clone(),
|
||||
RpmAxis = new byte[RpmBytes],
|
||||
};
|
||||
}
|
||||
|
||||
// ── Serialisation ─────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Returns the data-only write payload (24 bytes).
|
||||
/// Matches writeSubTableFunctionThread: only dataOutArray is sent,
|
||||
/// no RPM/load arrays on write.
|
||||
/// </summary>
|
||||
public byte[] ToWirePayload() => (byte[])Data.Clone();
|
||||
|
||||
/// <summary>
|
||||
/// Full 40-byte payload including load and RPM axes.
|
||||
/// Used when full round-trip serialisation is needed.
|
||||
/// </summary>
|
||||
public byte[] ToFullPayload()
|
||||
{
|
||||
var buf = new byte[TotalBytes];
|
||||
Buffer.BlockCopy(Data, 0, buf, 0, DataBytes);
|
||||
Buffer.BlockCopy(LoadAxis, 0, buf, DataBytes, LoadBytes);
|
||||
Buffer.BlockCopy(RpmAxis, 0, buf, DataBytes + LoadBytes, RpmBytes);
|
||||
return buf;
|
||||
}
|
||||
|
||||
// ── Mutation ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new SubTable with cell (row, col) adjusted by delta,
|
||||
/// clamped to the signed sbyte range [-128, 127].
|
||||
/// Matches minusButton/addButton in SubTuningTablePanel using Convert.ToSByte.
|
||||
/// </summary>
|
||||
public SubTable WithCellAdjusted(int row, int col, int delta,
|
||||
int minVal = -128, int maxVal = 127)
|
||||
{
|
||||
var newData = (byte[])Data.Clone();
|
||||
int idx = row * Cols + col;
|
||||
int current = unchecked((sbyte)newData[idx]);
|
||||
int adjusted = Math.Clamp(current + delta, minVal, maxVal);
|
||||
newData[idx] = unchecked((byte)(sbyte)adjusted);
|
||||
|
||||
return new SubTable
|
||||
{
|
||||
Data = newData,
|
||||
LoadAxis = LoadAxis,
|
||||
RpmAxis = RpmAxis,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user