diff --git a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Services/SlipResistanceDeviceService.cs b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Services/SlipResistanceDeviceService.cs
index 5bc891f..8237277 100644
--- a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Services/SlipResistanceDeviceService.cs
+++ b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Services/SlipResistanceDeviceService.cs
@@ -127,20 +127,65 @@ namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Services
public Task PulseResetAsync() => PulseCoilAsync(ResetCoil);
- public Task ToggleMoveLeftAsync() => ToggleCoilAsync(MoveLeftCoil);
-
- public Task ToggleMoveRightAsync() => ToggleCoilAsync(MoveRightCoil);
-
- public async Task LiftAsync()
+ public async Task StartLiftAsync()
{
await WriteCoilAsync(LowerCoil, false);
await WriteCoilAsync(LiftCoil, true);
+ Log.Information("提升按下运行:M{LiftCoil}=1, M{LowerCoil}=0", LiftCoil, LowerCoil);
}
- public async Task LowerAsync()
+ public async Task StopLiftAsync()
+ {
+ await WriteCoilAsync(LiftCoil, false);
+ Log.Information("提升松开停止:M{LiftCoil}=0", LiftCoil);
+ }
+
+ public async Task StartLowerAsync()
{
await WriteCoilAsync(LiftCoil, false);
await WriteCoilAsync(LowerCoil, true);
+ Log.Information("下降按下运行:M{LowerCoil}=1, M{LiftCoil}=0", LowerCoil, LiftCoil);
+ }
+
+ public async Task StopLowerAsync()
+ {
+ await WriteCoilAsync(LowerCoil, false);
+ Log.Information("下降松开停止:M{LowerCoil}=0", LowerCoil);
+ }
+
+ public async Task StartMoveLeftAsync()
+ {
+ await WriteCoilAsync(MoveRightCoil, false);
+ await WriteCoilAsync(MoveLeftCoil, true);
+ Log.Information("左移按下运行:M{MoveLeftCoil}=1, M{MoveRightCoil}=0", MoveLeftCoil, MoveRightCoil);
+ }
+
+ public async Task StopMoveLeftAsync()
+ {
+ await WriteCoilAsync(MoveLeftCoil, false);
+ Log.Information("左移松开停止:M{MoveLeftCoil}=0", MoveLeftCoil);
+ }
+
+ public async Task StartMoveRightAsync()
+ {
+ await WriteCoilAsync(MoveLeftCoil, false);
+ await WriteCoilAsync(MoveRightCoil, true);
+ Log.Information("右移按下运行:M{MoveRightCoil}=1, M{MoveLeftCoil}=0", MoveRightCoil, MoveLeftCoil);
+ }
+
+ public async Task StopMoveRightAsync()
+ {
+ await WriteCoilAsync(MoveRightCoil, false);
+ Log.Information("右移松开停止:M{MoveRightCoil}=0", MoveRightCoil);
+ }
+
+ public async Task StopAllMotionAsync()
+ {
+ await WriteCoilAsync(LiftCoil, false);
+ await WriteCoilAsync(LowerCoil, false);
+ await WriteCoilAsync(MoveLeftCoil, false);
+ await WriteCoilAsync(MoveRightCoil, false);
+ Log.Information("全部运动停止:M{LiftCoil}=0, M{LowerCoil}=0, M{MoveLeftCoil}=0, M{MoveRightCoil}=0", LiftCoil, LowerCoil, MoveLeftCoil, MoveRightCoil);
}
public Task WriteManualSpeedAsync(double value) => WriteFloatRegisterAsync(ManualSpeedRegister, value);
diff --git a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/ViewModels/MainWindowViewModel.cs b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/ViewModels/MainWindowViewModel.cs
index aeb3188..4e4a402 100644
--- a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/ViewModels/MainWindowViewModel.cs
+++ b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/ViewModels/MainWindowViewModel.cs
@@ -301,17 +301,23 @@ namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.ViewModel
}
}
- [RelayCommand]
- private async Task Lift() => await RunDeviceCommand(deviceService.LiftAsync(), "垂直架提升指令已发送 M5");
+ public Task StartLiftMotionAsync() => RunDeviceCommand(deviceService.StartLiftAsync(), "垂直架提升中,松开停止");
- [RelayCommand]
- private async Task Lower() => await RunDeviceCommand(deviceService.LowerAsync(), "垂直架下降指令已发送 M4");
+ public Task StopLiftMotionAsync() => RunDeviceCommand(deviceService.StopLiftAsync(), "垂直架提升已停止");
- [RelayCommand]
- private async Task MoveLeft() => await RunDeviceCommand(deviceService.ToggleMoveLeftAsync(), "水平板左移状态已切换 M1");
+ public Task StartLowerMotionAsync() => RunDeviceCommand(deviceService.StartLowerAsync(), "垂直架下降中,松开停止");
- [RelayCommand]
- private async Task MoveRight() => await RunDeviceCommand(deviceService.ToggleMoveRightAsync(), "水平板右移状态已切换 M2");
+ public Task StopLowerMotionAsync() => RunDeviceCommand(deviceService.StopLowerAsync(), "垂直架下降已停止");
+
+ public Task StartMoveLeftMotionAsync() => RunDeviceCommand(deviceService.StartMoveLeftAsync(), "水平板左移中,松开停止");
+
+ public Task StopMoveLeftMotionAsync() => RunDeviceCommand(deviceService.StopMoveLeftAsync(), "水平板左移已停止");
+
+ public Task StartMoveRightMotionAsync() => RunDeviceCommand(deviceService.StartMoveRightAsync(), "水平板右移中,松开停止");
+
+ public Task StopMoveRightMotionAsync() => RunDeviceCommand(deviceService.StopMoveRightAsync(), "水平板右移已停止");
+
+ public Task StopAllMotionAsync() => RunDeviceCommand(deviceService.StopAllMotionAsync(), "全部运动已停止");
[RelayCommand]
private void DeleteSelectedSample()
@@ -425,6 +431,15 @@ namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.ViewModel
public void Dispose()
{
refreshTimer.Stop();
+ try
+ {
+ deviceService.StopAllMotionAsync().Wait(500);
+ }
+ catch (Exception ex)
+ {
+ Log.Warning(ex, "关闭窗口时停止全部运动失败");
+ }
+
deviceService.Dispose();
}
diff --git a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml
index 56203c1..6189f40 100644
--- a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml
+++ b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml
@@ -334,16 +334,38 @@
-
-
+
+
-
-
+
+
diff --git a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml.cs b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml.cs
index 25f8cd1..92443e0 100644
--- a/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml.cs
+++ b/Footwear Test methodsfor wholeshoe Slipresistanceperformance/Views/MainWindow.axaml.cs
@@ -1,23 +1,111 @@
+using Avalonia.Controls;
+using Avalonia.Input;
+using Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.ViewModels;
using SukiUI.Controls;
using System;
-using Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.ViewModels;
+using System.Collections.Generic;
+using System.Threading.Tasks;
namespace Footwear_Test_methodsfor_wholeshoe_Slipresistanceperformance.Views
{
public partial class MainWindow : SukiWindow
{
+ private readonly HashSet activeMotionTags = [];
+
public MainWindow()
{
InitializeComponent();
Closed += OnClosed;
+ Deactivated += OnDeactivated;
}
- private void OnClosed(object? sender, EventArgs e)
+ private async void MotionButton_PointerPressed(object? sender, PointerPressedEventArgs e)
{
+ if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
+ {
+ return;
+ }
+
+ if (!activeMotionTags.Add(tag))
+ {
+ return;
+ }
+
+ e.Pointer.Capture(button);
+ e.Handled = true;
+ await StartMotionAsync(viewModel, tag);
+ }
+
+ private async void MotionButton_PointerReleased(object? sender, PointerReleasedEventArgs e)
+ {
+ if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
+ {
+ return;
+ }
+
+ e.Pointer.Capture(null);
+ e.Handled = true;
+ await StopMotionAsync(viewModel, tag);
+ }
+
+ private async void MotionButton_PointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
+ {
+ if (sender is not Button button || button.Tag is not string tag || DataContext is not MainWindowViewModel viewModel)
+ {
+ return;
+ }
+
+ await StopMotionAsync(viewModel, tag);
+ }
+
+ private async void OnDeactivated(object? sender, EventArgs e)
+ {
+ await StopAllMotionAsync();
+ }
+
+ private async void OnClosed(object? sender, EventArgs e)
+ {
+ await StopAllMotionAsync();
if (DataContext is MainWindowViewModel viewModel)
{
viewModel.Dispose();
}
}
+
+ private static Task StartMotionAsync(MainWindowViewModel viewModel, string tag) =>
+ tag switch
+ {
+ "Lift" => viewModel.StartLiftMotionAsync(),
+ "Lower" => viewModel.StartLowerMotionAsync(),
+ "MoveLeft" => viewModel.StartMoveLeftMotionAsync(),
+ "MoveRight" => viewModel.StartMoveRightMotionAsync(),
+ _ => Task.CompletedTask
+ };
+
+ private async Task StopMotionAsync(MainWindowViewModel viewModel, string tag)
+ {
+ if (!activeMotionTags.Remove(tag))
+ {
+ return;
+ }
+
+ await (tag switch
+ {
+ "Lift" => viewModel.StopLiftMotionAsync(),
+ "Lower" => viewModel.StopLowerMotionAsync(),
+ "MoveLeft" => viewModel.StopMoveLeftMotionAsync(),
+ "MoveRight" => viewModel.StopMoveRightMotionAsync(),
+ _ => Task.CompletedTask
+ });
+ }
+
+ private async Task StopAllMotionAsync()
+ {
+ activeMotionTags.Clear();
+ if (DataContext is MainWindowViewModel viewModel)
+ {
+ await viewModel.StopAllMotionAsync();
+ }
+ }
}
}