233 lines
10 KiB
C#
233 lines
10 KiB
C#
namespace TuneBoss.Core.Models;
|
||
|
||
/// <summary>
|
||
/// Complete data model for a 2D tuning table (Fuel PulseWidth, Spark Angle,
|
||
/// AFR Target, etc.).
|
||
///
|
||
/// ── Wire format on the device ────────────────────────────────────────────────
|
||
/// Total payload = 227 bytes, split across two reads/writes at 0x7F boundary:
|
||
///
|
||
/// Offset Length Field
|
||
/// ─────── ─────── ──────────────────────────────────────────────────────
|
||
/// 0 192 Data cells (24 cols × 8 rows, row-major, byte each)
|
||
/// 192 8 Load axis (8 bytes, ascending, 0–110 range)
|
||
/// 200 24 RPM axis (24 bytes, each × 100 = actual RPM)
|
||
/// 224 1 Max raw (byte; sbyte if signed)
|
||
/// 225 1 Min raw (byte; sbyte if signed)
|
||
/// 226 1 Mult (multiplier for display scaling)
|
||
/// 227 1 Sign (0 = unsigned byte, >0 = signed sbyte)
|
||
///
|
||
/// Split read:
|
||
/// Part 1: bytes [0..127] → lengthFirstPart = 0x7F (127)
|
||
/// Part 2: bytes [128..227] → lengthSecondPart = 227 - 127 - 1 = 99
|
||
/// Part 2 address = baseAddress + 0x7F + 1
|
||
///
|
||
/// Split write (same split, different order — data is written first):
|
||
/// Part 1 writes dataArray[0..127]
|
||
/// Part 2 writes dataArray[128..191] + loadArray[0..7]
|
||
/// + rpmArray[0..23] + modifiers[0..3]
|
||
///
|
||
/// ── Display / UI mapping ─────────────────────────────────────────────────────
|
||
/// RPM axis : stored as raw byte (÷100 on wire), displayed × 100 (e.g. 50→5000)
|
||
/// Load axis : stored as raw byte (0–110), displayed as-is
|
||
/// Data cells: if Sign > 0 → sbyte interpretation, else byte
|
||
/// minDataVal/maxDataVal derived from min/maxRawVal via sign flag
|
||
/// Color map : 7 linear bands from minDataVal to maxDataVal (red→green)
|
||
/// </summary>
|
||
public sealed record TuningTable
|
||
{
|
||
// ── Dimensions ────────────────────────────────────────────────────────────
|
||
public const int Cols = 24; // RPM columns
|
||
public const int Rows = 8; // Load rows
|
||
public const int DataBytes = Cols * Rows; // 192
|
||
|
||
// ── Split constants (match TableHandling lengthFirstPart) ─────────────────
|
||
public const int SplitFirstLength = 0x7F; // 127
|
||
public const int TotalPayloadBytes = DataBytes + Rows + Cols + 4; // 228
|
||
|
||
// ── Raw wire arrays ───────────────────────────────────────────────────────
|
||
/// <summary>192 bytes, row-major [row * 24 + col].</summary>
|
||
public byte[] Data { get; init; } = new byte[DataBytes];
|
||
|
||
/// <summary>8 bytes — load axis values (0–110).</summary>
|
||
public byte[] LoadAxis { get; init; } = new byte[Rows];
|
||
|
||
/// <summary>24 bytes — RPM axis, each × 100 = actual RPM.</summary>
|
||
public byte[] RpmAxis { get; init; } = new byte[Cols];
|
||
|
||
// ── Modifiers (bytes 224–227) ─────────────────────────────────────────────
|
||
/// <summary>Raw max value from device (index [0] in modifiersInArray).</summary>
|
||
public byte MaxRaw { get; init; }
|
||
|
||
/// <summary>Raw min value from device (index [1] in modifiersInArray).</summary>
|
||
public byte MinRaw { get; init; }
|
||
|
||
/// <summary>Multiplier for display scaling (index [2]).</summary>
|
||
public byte Mult { get; init; }
|
||
|
||
/// <summary>Sign flag: 0 = unsigned byte, >0 = signed sbyte (index [3]).</summary>
|
||
public byte Sign { get; init; }
|
||
|
||
// ── Derived display values ────────────────────────────────────────────────
|
||
public bool IsSigned => Sign > 0;
|
||
|
||
/// <summary>Display min — sbyte if signed, byte if unsigned.</summary>
|
||
public int MinDataVal => IsSigned
|
||
? unchecked((sbyte)MinRaw)
|
||
: (int)MinRaw;
|
||
|
||
/// <summary>Display max — sbyte if signed, byte if unsigned.</summary>
|
||
public int MaxDataVal => IsSigned
|
||
? unchecked((sbyte)MaxRaw)
|
||
: (int)MaxRaw;
|
||
|
||
/// <summary>
|
||
/// Gets the display value for a cell.
|
||
/// Applies sign interpretation matching UpdateTable(readCmd=1) in TuningTablePanel.
|
||
/// </summary>
|
||
public int GetCell(int row, int col) =>
|
||
IsSigned
|
||
? unchecked((sbyte)Data[row * Cols + col])
|
||
: Data[row * Cols + col];
|
||
|
||
/// <summary>RPM axis values in display units (raw × 100).</summary>
|
||
public int[] RpmValues => RpmAxis.Select(r => r * 100).ToArray();
|
||
|
||
/// <summary>Load axis values (raw, 0–110 range).</summary>
|
||
public int[] LoadValues => LoadAxis.Select(l => (int)l).ToArray();
|
||
|
||
/// <summary>
|
||
/// 7-band color range thresholds, matching setDataValRange() in TuningTablePanel.
|
||
/// dataValRange[x] = minDataVal + (x * (maxDataVal - minDataVal)) / 7
|
||
/// </summary>
|
||
public double[] ColorBandThresholds()
|
||
{
|
||
var range = new double[8];
|
||
int span = MaxDataVal - MinDataVal;
|
||
for (int x = 0; x < 8; x++)
|
||
range[x] = MinDataVal + (x * span) / 7.0;
|
||
return range;
|
||
}
|
||
|
||
// ── Serialisation (write path) ────────────────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// Builds the combined 228-byte payload for the write path.
|
||
/// Matches the order in writeTableFunctionSplitThreadCustom:
|
||
/// [0..191] data cells
|
||
/// [192..199] load axis
|
||
/// [200..223] rpm axis
|
||
/// [224] MaxRaw
|
||
/// [225] MinRaw
|
||
/// [226] Mult
|
||
/// [227] Sign
|
||
/// </summary>
|
||
public byte[] ToWirePayload()
|
||
{
|
||
var buf = new byte[TotalPayloadBytes];
|
||
Buffer.BlockCopy(Data, 0, buf, 0, DataBytes);
|
||
Buffer.BlockCopy(LoadAxis,0, buf, DataBytes, Rows);
|
||
Buffer.BlockCopy(RpmAxis, 0, buf, DataBytes + Rows, Cols);
|
||
buf[DataBytes + Rows + Cols] = MaxRaw;
|
||
buf[DataBytes + Rows + Cols + 1] = MinRaw;
|
||
buf[DataBytes + Rows + Cols + 2] = Mult;
|
||
buf[DataBytes + Rows + Cols + 3] = Sign;
|
||
return buf;
|
||
}
|
||
|
||
/// <summary>First 128 bytes of the payload — sent as part 1 of split write.</summary>
|
||
public byte[] ToWirePayloadPart1() => ToWirePayload()[..( SplitFirstLength + 1)];
|
||
|
||
/// <summary>Remaining bytes — sent as part 2 of split write.</summary>
|
||
public byte[] ToWirePayloadPart2() => ToWirePayload()[(SplitFirstLength + 1)..];
|
||
|
||
// ── Deserialisation (read path) ───────────────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// Parses a combined 228-byte buffer assembled from the two split reads.
|
||
/// Matches sendReadSplitTableToPanelThreadCustom() in Form1_TableHandling.cs.
|
||
///
|
||
/// Part 1 response[4..131] → dataCombined[0..127]
|
||
/// Part 2 response[4..103] → dataCombined[128..227]
|
||
/// </summary>
|
||
public static TuningTable FromWirePayload(byte[] combined)
|
||
{
|
||
if (combined.Length < TotalPayloadBytes)
|
||
throw new ArgumentException(
|
||
$"Expected {TotalPayloadBytes} bytes, got {combined.Length}.", nameof(combined));
|
||
|
||
var data = new byte[DataBytes];
|
||
var loadAxis = new byte[Rows];
|
||
var rpmAxis = new byte[Cols];
|
||
|
||
Buffer.BlockCopy(combined, 0, data, 0, DataBytes);
|
||
Buffer.BlockCopy(combined, DataBytes, loadAxis,0, Rows);
|
||
Buffer.BlockCopy(combined, DataBytes + Rows, rpmAxis, 0, Cols);
|
||
|
||
int modBase = DataBytes + Rows + Cols;
|
||
byte maxRaw = combined[modBase];
|
||
byte minRaw = combined[modBase + 1];
|
||
byte mult = combined[modBase + 2];
|
||
byte sign = combined[modBase + 3];
|
||
|
||
return new TuningTable
|
||
{
|
||
Data = data,
|
||
LoadAxis = loadAxis,
|
||
RpmAxis = rpmAxis,
|
||
MaxRaw = maxRaw,
|
||
MinRaw = minRaw,
|
||
Mult = mult,
|
||
Sign = sign,
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Assembles the combined payload from the two raw device response buffers.
|
||
/// Each response buffer has a 4-byte header; data starts at offset 4.
|
||
/// </summary>
|
||
public static TuningTable FromSplitResponses(byte[] part1Response, byte[] part2Response)
|
||
{
|
||
var combined = new byte[TotalPayloadBytes];
|
||
|
||
// Part 1: bytes [4 .. 4+SplitFirstLength] → combined[0..SplitFirstLength]
|
||
Buffer.BlockCopy(part1Response, 4, combined, 0,
|
||
SplitFirstLength + 1);
|
||
|
||
// Part 2: bytes [4 ..] → combined[SplitFirstLength+1..]
|
||
int part2Len = TotalPayloadBytes - (SplitFirstLength + 1);
|
||
Buffer.BlockCopy(part2Response, 4, combined, SplitFirstLength + 1,
|
||
part2Len);
|
||
|
||
return FromWirePayload(combined);
|
||
}
|
||
|
||
// ── Mutation (for +1/-1/+5/-5 operations) ────────────────────────────────
|
||
|
||
/// <summary>
|
||
/// Returns a new TuningTable with the cell at (row, col) adjusted by delta,
|
||
/// clamped to [MinDataVal, MaxDataVal].
|
||
/// </summary>
|
||
public TuningTable WithCellAdjusted(int row, int col, int delta)
|
||
{
|
||
var newData = (byte[])Data.Clone();
|
||
int idx = row * Cols + col;
|
||
int current = IsSigned ? unchecked((sbyte)newData[idx]) : newData[idx];
|
||
int adjusted = Math.Clamp(current + delta, MinDataVal, MaxDataVal);
|
||
newData[idx] = IsSigned
|
||
? unchecked((byte)(sbyte)adjusted)
|
||
: (byte)adjusted;
|
||
|
||
return new TuningTable
|
||
{
|
||
Data = newData,
|
||
LoadAxis = LoadAxis,
|
||
RpmAxis = RpmAxis,
|
||
MaxRaw = MaxRaw,
|
||
MinRaw = MinRaw,
|
||
Mult = Mult,
|
||
Sign = Sign,
|
||
};
|
||
}
|
||
}
|