using CommunityToolkit.Mvvm.ComponentModel; using HC_APTBS.Models; namespace HC_APTBS.ViewModels { /// /// ViewModel for the BenchParamConfig user control. /// /// /// Exposes all editable fields for one : CAN message ID, /// byte positions, IIR filter alpha, and the six calibration coefficients P1–P6. /// Call to populate from a model instance, then /// to write edited values back. /// /// public sealed partial class BenchConfigViewModel : ObservableObject { // ── Display ─────────────────────────────────────────────────────────────── /// Human-readable name of the parameter being edited. [ObservableProperty] private string _name = string.Empty; // ── CAN frame fields ────────────────────────────────────────────────────── /// CAN message ID as a hex string (e.g. "1A0"). [ObservableProperty] private string _messageIdHex = string.Empty; /// High-byte index within the CAN payload (0-based). [ObservableProperty] private string _byteH = string.Empty; /// Low-byte index within the CAN payload (0-based). [ObservableProperty] private string _byteL = string.Empty; /// IIR smoothing factor α (0–1). 1 = no filtering. [ObservableProperty] private string _alpha = string.Empty; // ── Calibration formula ─────────────────────────────────────────────────── /// True when the 6-parameter calibration formula is enabled. [ObservableProperty] private bool _formulaEnabled; /// Calibration coefficient P1. [ObservableProperty] private string _p1 = string.Empty; /// Calibration coefficient P2. [ObservableProperty] private string _p2 = string.Empty; /// Calibration coefficient P3. [ObservableProperty] private string _p3 = string.Empty; /// Calibration coefficient P4. [ObservableProperty] private string _p4 = string.Empty; /// Calibration coefficient P5. [ObservableProperty] private string _p5 = string.Empty; /// Calibration coefficient P6. [ObservableProperty] private string _p6 = string.Empty; // ── Public API ──────────────────────────────────────────────────────────── /// Populates all editable fields from . public void LoadParameter(CanBusParameter parameter) { Name = parameter.Name; MessageIdHex = parameter.ID.ToString("X"); ByteH = parameter.ByteH.ToString(); ByteL = parameter.ByteL.ToString(); Alpha = parameter.Alpha.ToString(); FormulaEnabled = !parameter.DisableCalibration; P1 = parameter.P1.ToString(); P2 = parameter.P2.ToString(); P3 = parameter.P3.ToString(); P4 = parameter.P4.ToString(); P5 = parameter.P5.ToString(); P6 = parameter.P6.ToString(); } /// /// Writes all edited values back to . /// Returns if any field fails to parse. /// public bool ApplyToParameter(CanBusParameter parameter) { if (!uint.TryParse(MessageIdHex, System.Globalization.NumberStyles.HexNumber, null, out uint id)) return false; if (!ushort.TryParse(ByteH, out ushort byteH)) return false; if (!ushort.TryParse(ByteL, out ushort byteL)) return false; if (!double.TryParse(Alpha, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out double alpha)) return false; parameter.ID = id; parameter.ByteH = byteH; parameter.ByteL = byteL; parameter.Alpha = alpha; parameter.DisableCalibration = !FormulaEnabled; if (FormulaEnabled) { if (!TryParseInvariant(P1, out double p1)) return false; if (!TryParseInvariant(P2, out double p2)) return false; if (!TryParseInvariant(P3, out double p3)) return false; if (!TryParseInvariant(P4, out double p4)) return false; if (!TryParseInvariant(P5, out double p5)) return false; if (!TryParseInvariant(P6, out double p6)) return false; parameter.P1 = p1; parameter.P2 = p2; parameter.P3 = p3; parameter.P4 = p4; parameter.P5 = p5; parameter.P6 = p6; } return true; } // ── Helpers ─────────────────────────────────────────────────────────────── private static bool TryParseInvariant(string text, out double result) => double.TryParse(text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out result); } }