Initial Commit

This commit is contained in:
2026-09-28 16:55:31 +08:00
commit 14f8706198
128 changed files with 19860 additions and 0 deletions
+221
View File
@@ -0,0 +1,221 @@
namespace TuneBoss.Core.Models;
/// <summary>
/// Data model for an AFR Target table (AFR_Target1, AFR_Target2).
///
/// Structurally identical to TuningTable (24 cols × 8 rows, same split read/write,
/// same wire layout) but with a critical difference in value encoding:
///
/// ── Fixed-point ×10 encoding ─────────────────────────────────────────────────
/// All cell values are stored as byte × 10 on the wire.
/// Wire byte 145 = AFR 14.5, byte 137 = AFR 13.7, etc.
/// Display range set by caller: minDataVal=0.0, maxDataVal=15.0 (from populateInitTables)
///
/// ── Modifier encoding ─────────────────────────────────────────────────────────
/// MaxRaw / MinRaw are also stored ×10 on wire:
/// modifiersInArray[0] = (byte)(maxRawVal * 10.0)
/// modifiersInArray[1] = (byte)(minRawVal * 10.0)
/// modifiersInArray[2] = (byte)multRawVal (same as TuningTable)
/// modifiersInArray[3] = (byte)signRawVal (same as TuningTable)
/// Confirmed from writeBtn_Click and prepareAllData() in TuningTablePanelAFR.cs.
///
/// ── Button increments ────────────────────────────────────────────────────────
/// +/- large = 5.0 AFR (minusFive_Click / plusFive_Click)
/// +/- small = 0.1 AFR (minusOne_Click / plusOne_Click) ← different from TuningTable
/// Validated as double, displayed as "N1" (1 decimal place).
///
/// ── Wire format ──────────────────────────────────────────────────────────────
/// Identical layout to TuningTable (228 bytes, split at 0x7F):
/// [0..191] Data — 192 bytes, row-major, each byte = AFR × 10
/// [192..199] Load — 8 bytes
/// [200..223] RPM — 24 bytes, each × 100 = display RPM
/// [224] MaxRaw — stored as byte = maxAFR × 10
/// [225] MinRaw — stored as byte = minAFR × 10
/// [226] Mult
/// [227] Sign
///
/// Read length from populateInitTables: 0xEF (239) — larger than TuningTable's 0xE3.
/// </summary>
public sealed record AfrTable
{
public const int Cols = 24;
public const int Rows = 8;
public const int DataBytes = Cols * Rows; // 192
public const int SplitFirstLength = 0x7F; // 127 — same as TuningTable
public const int TotalPayloadBytes = DataBytes + Rows + Cols + 4; // 228
public const byte ReadLength = 0xEF; // 239 — from populateInitTables
// ── Raw wire arrays ───────────────────────────────────────────────────────
/// <summary>192 bytes, row-major [row * 24 + col]. Wire value = AFR × 10.</summary>
public byte[] Data { get; init; } = new byte[DataBytes];
/// <summary>8 bytes — load axis (0–110).</summary>
public byte[] LoadAxis { get; init; } = new byte[Rows];
/// <summary>24 bytes — RPM axis, each × 100 = display RPM.</summary>
public byte[] RpmAxis { get; init; } = new byte[Cols];
// ── Modifiers [224–227] ───────────────────────────────────────────────────
/// <summary>Wire: maxAFR × 10. E.g. byte 150 = AFR 15.0.</summary>
public byte MaxRaw { get; init; }
/// <summary>Wire: minAFR × 10. E.g. byte 0 = AFR 0.0.</summary>
public byte MinRaw { get; init; }
public byte Mult { get; init; }
public byte Sign { get; init; }
// ── Derived display values ────────────────────────────────────────────────
public bool IsSigned => Sign > 0;
/// <summary>Display AFR value for a cell (wire byte ÷ 10.0).</summary>
public double GetCell(int row, int col)
{
byte raw = Data[row * Cols + col];
return IsSigned
? unchecked((sbyte)raw) / 10.0
: raw / 10.0;
}
/// <summary>Max AFR display value.</summary>
public double MaxAfrDisplay => IsSigned
? unchecked((sbyte)MaxRaw) / 10.0
: MaxRaw / 10.0;
/// <summary>Min AFR display value.</summary>
public double MinAfrDisplay => IsSigned
? unchecked((sbyte)MinRaw) / 10.0
: MinRaw / 10.0;
/// <summary>RPM axis in display units (raw × 100).</summary>
public int[] RpmValues => RpmAxis.Select(r => r * 100).ToArray();
/// <summary>Load axis values (raw, 0–110).</summary>
public int[] LoadValues => LoadAxis.Select(l => (int)l).ToArray();
// ── Serialisation ─────────────────────────────────────────────────────────
/// <summary>
/// Builds the combined 228-byte write payload.
/// Matches prepareAllData() / writeBtn_Click() in TuningTablePanelAFR.cs.
/// Modifier bytes [224–225] are stored as AFR × 10.
/// </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;
}
public byte[] ToWirePayloadPart1() => ToWirePayload()[..(SplitFirstLength + 1)];
public byte[] ToWirePayloadPart2() => ToWirePayload()[(SplitFirstLength + 1)..];
// ── Deserialisation ───────────────────────────────────────────────────────
/// <summary>
/// Parses a combined 228-byte buffer from two split read responses.
/// Matches sendReadSplitTableToPanelThread (AFR path) in Form1_TableHandling.cs.
/// </summary>
public static AfrTable 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;
return new AfrTable
{
Data = data,
LoadAxis = loadAxis,
RpmAxis = rpmAxis,
MaxRaw = combined[modBase],
MinRaw = combined[modBase + 1],
Mult = combined[modBase + 2],
Sign = combined[modBase + 3],
};
}
/// <summary>
/// Assembles from two raw device response buffers (data starts at offset 4).
/// </summary>
public static AfrTable FromSplitResponses(byte[] part1Response, byte[] part2Response)
{
var combined = new byte[TotalPayloadBytes];
Buffer.BlockCopy(part1Response, 4, combined, 0, SplitFirstLength + 1);
int part2Len = TotalPayloadBytes - (SplitFirstLength + 1);
Buffer.BlockCopy(part2Response, 4, combined, SplitFirstLength + 1, part2Len);
return FromWirePayload(combined);
}
// ── Mutation ──────────────────────────────────────────────────────────────
/// <summary>
/// Returns a new AfrTable with cell (row, col) adjusted by delta (in AFR units, e.g. 0.1).
/// Delta is converted to wire units (×10) before clamping.
/// Matches minusButton/addButton in TuningTablePanelAFR.cs.
/// </summary>
public AfrTable WithCellAdjusted(int row, int col, double deltaAfr,
double minAfr = 0.0, double maxAfr = 15.0)
{
var newData = (byte[])Data.Clone();
int idx = row * Cols + col;
double current = GetCell(row, col);
double adjusted = Math.Clamp(current + deltaAfr, minAfr, maxAfr);
// Round to 1 decimal place then encode ×10
double rounded = Math.Round(adjusted, 1, MidpointRounding.AwayFromZero);
newData[idx] = IsSigned
? unchecked((byte)(sbyte)(rounded * 10.0))
: (byte)(rounded * 10.0);
return new AfrTable
{
Data = newData,
LoadAxis = LoadAxis,
RpmAxis = RpmAxis,
MaxRaw = MaxRaw,
MinRaw = MinRaw,
Mult = Mult,
Sign = Sign,
};
}
// ── Convenience ──────────────────────────────────────────────────────────
/// <summary>
/// Returns modifier bytes as stored on wire.
/// MaxRaw/MinRaw are AFR × 10; Mult/Sign are unchanged.
/// </summary>
public byte[] ToModifierBytes() => new[]
{
MaxRaw, MinRaw, Mult, Sign
};
/// <summary>
/// Builds modifier bytes from display AFR values for writing.
/// Matches: modifiersInArray[0] = (byte)(maxRawVal * 10.0)
/// </summary>
public static byte[] BuildModifierBytes(double maxAfr, double minAfr,
byte mult = 1, byte sign = 0) => new[]
{
(byte)(maxAfr * 10.0),
(byte)(minAfr * 10.0),
mult,
sign
};
}