initial commit
This commit is contained in:
130
ViewModels/BenchConfigViewModel.cs
Normal file
130
ViewModels/BenchConfigViewModel.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using HC_APTBS.Models;
|
||||
|
||||
namespace HC_APTBS.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// ViewModel for the BenchParamConfig user control.
|
||||
///
|
||||
/// <para>
|
||||
/// Exposes all editable fields for one <see cref="CanBusParameter"/>: CAN message ID,
|
||||
/// byte positions, IIR filter alpha, and the six calibration coefficients P1–P6.
|
||||
/// Call <see cref="LoadParameter"/> to populate from a model instance, then
|
||||
/// <see cref="ApplyToParameter"/> to write edited values back.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed partial class BenchConfigViewModel : ObservableObject
|
||||
{
|
||||
// ── Display ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Human-readable name of the parameter being edited.</summary>
|
||||
[ObservableProperty] private string _name = string.Empty;
|
||||
|
||||
// ── CAN frame fields ──────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>CAN message ID as a hex string (e.g. "1A0").</summary>
|
||||
[ObservableProperty] private string _messageIdHex = string.Empty;
|
||||
|
||||
/// <summary>High-byte index within the CAN payload (0-based).</summary>
|
||||
[ObservableProperty] private string _byteH = string.Empty;
|
||||
|
||||
/// <summary>Low-byte index within the CAN payload (0-based).</summary>
|
||||
[ObservableProperty] private string _byteL = string.Empty;
|
||||
|
||||
/// <summary>IIR smoothing factor α (0–1). 1 = no filtering.</summary>
|
||||
[ObservableProperty] private string _alpha = string.Empty;
|
||||
|
||||
// ── Calibration formula ───────────────────────────────────────────────────
|
||||
|
||||
/// <summary>True when the 6-parameter calibration formula is enabled.</summary>
|
||||
[ObservableProperty] private bool _formulaEnabled;
|
||||
|
||||
/// <summary>Calibration coefficient P1.</summary>
|
||||
[ObservableProperty] private string _p1 = string.Empty;
|
||||
|
||||
/// <summary>Calibration coefficient P2.</summary>
|
||||
[ObservableProperty] private string _p2 = string.Empty;
|
||||
|
||||
/// <summary>Calibration coefficient P3.</summary>
|
||||
[ObservableProperty] private string _p3 = string.Empty;
|
||||
|
||||
/// <summary>Calibration coefficient P4.</summary>
|
||||
[ObservableProperty] private string _p4 = string.Empty;
|
||||
|
||||
/// <summary>Calibration coefficient P5.</summary>
|
||||
[ObservableProperty] private string _p5 = string.Empty;
|
||||
|
||||
/// <summary>Calibration coefficient P6.</summary>
|
||||
[ObservableProperty] private string _p6 = string.Empty;
|
||||
|
||||
// ── Public API ────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Populates all editable fields from <paramref name="parameter"/>.</summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes all edited values back to <paramref name="parameter"/>.
|
||||
/// Returns <see langword="false"/> if any field fails to parse.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user