using System;
namespace TuneBoss.Core.Models
{
///
/// A 1D lookup table as stored on the ECU: 8 raw-axis breakpoints mapped to
/// 8 conversion (output) values, plus 8 metadata bytes (scaling / sign / clamp).
///
/// Fixed 24-byte wire layout:
/// [0..7] raw axis breakpoints (signed when RawSignedFlag > 0)
/// [8..15] conversion/output vals (signed when ConvSignedFlag > 0)
/// [16] MaxRaw (follows raw sign)
/// [17] MinRaw (follows raw sign)
/// [18] RawSignedFlag (> 0 => axis bytes are signed)
/// [19] ConvSignedFlag (> 0 => value bytes are signed)
/// [20] RawMultiplier (firmware-side display scaling)
/// [21] ConvMultiplier
/// [22] RawRounding
/// [23] ConvOffset (ALWAYS signed)
///
/// Read with APP_MEMRD_1D (0x43), written with APP_MEMWR_1D (0x4B).
/// The sign flags and the four scaling bytes are round-tripped VERBATIM so a
/// write never disturbs metadata the firmware relies on.
///
public sealed class OneDimTable
{
public const int PointCount = 8;
public const int WireLength = 24;
public int[] RawAxis { get; }
public int[] ConvValues { get; }
// metadata (bytes 16..23)
public int MaxRaw { get; set; }
public int MinRaw { get; set; }
public byte RawSignedFlag { get; set; }
public byte ConvSignedFlag { get; set; }
public byte RawMultiplier { get; set; }
public byte ConvMultiplier { get; set; }
public byte RawRounding { get; set; }
public sbyte ConvOffset { get; set; }
public bool IsRawSigned => RawSignedFlag > 0;
public bool IsConvSigned => ConvSignedFlag > 0;
public OneDimTable()
{
RawAxis = new int[PointCount];
ConvValues = new int[PointCount];
}
/// Decode a 24-byte block (e.g. the data section of a 0x51 0x43 response).
public static OneDimTable FromWire(byte[] data, int offset = 0)
{
if (data is null) throw new ArgumentNullException(nameof(data));
if (data.Length - offset < WireLength)
throw new ArgumentException($"1D payload needs {WireLength} bytes from offset {offset}.", nameof(data));
var t = new OneDimTable
{
RawSignedFlag = data[offset + 18],
ConvSignedFlag = data[offset + 19],
RawMultiplier = data[offset + 20],
ConvMultiplier = data[offset + 21],
RawRounding = data[offset + 22],
ConvOffset = unchecked((sbyte)data[offset + 23]),
};
bool rawSigned = t.RawSignedFlag > 0;
bool convSigned = t.ConvSignedFlag > 0;
for (int i = 0; i < PointCount; i++)
{
t.RawAxis[i] = Decode(data[offset + i], rawSigned);
t.ConvValues[i] = Decode(data[offset + 8 + i], convSigned);
}
t.MaxRaw = Decode(data[offset + 16], rawSigned);
t.MinRaw = Decode(data[offset + 17], rawSigned);
return t;
}
/// Produce the 24-byte data block to send with APP_MEMWR_1D.
public byte[] ToWirePayload()
{
var d = new byte[WireLength];
bool rawSigned = IsRawSigned;
bool convSigned = IsConvSigned;
for (int i = 0; i < PointCount; i++)
{
d[i] = Encode(RawAxis[i], rawSigned);
d[8 + i] = Encode(ConvValues[i], convSigned);
}
d[16] = Encode(MaxRaw, rawSigned);
d[17] = Encode(MinRaw, rawSigned);
d[18] = RawSignedFlag;
d[19] = ConvSignedFlag;
d[20] = RawMultiplier;
d[21] = ConvMultiplier;
d[22] = RawRounding;
d[23] = unchecked((byte)ConvOffset);
return d;
}
public OneDimTable Clone()
{
var c = new OneDimTable
{
MaxRaw = MaxRaw, MinRaw = MinRaw,
RawSignedFlag = RawSignedFlag, ConvSignedFlag = ConvSignedFlag,
RawMultiplier = RawMultiplier, ConvMultiplier = ConvMultiplier,
RawRounding = RawRounding, ConvOffset = ConvOffset,
};
Array.Copy(RawAxis, c.RawAxis, PointCount);
Array.Copy(ConvValues, c.ConvValues, PointCount);
return c;
}
private static int Decode(byte b, bool signed) => signed ? unchecked((sbyte)b) : b;
private static byte Encode(int v, bool signed) => signed
? unchecked((byte)(sbyte)Math.Clamp(v, sbyte.MinValue, sbyte.MaxValue))
: (byte)Math.Clamp(v, byte.MinValue, byte.MaxValue);
}
}