Skip to content

Commit

Permalink
🐧 Bug fix for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
SydneyOwl committed May 2, 2024
1 parent e7b1460 commit 90c12c1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ jobs:
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
./*.zip
SenhaixFreqWriterArtifacts/*.zip
28 changes: 26 additions & 2 deletions Utils/HID/HIDTools.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using HidSharp;
using SenhaixFreqWriter.Constants.Gt12;
using SenhaixFreqWriter.DataModels.Gt12;
Expand Down Expand Up @@ -30,21 +31,27 @@ public class HIDTools
//TODO: enhance
public byte[] rxBuffer = new byte[64];
public bool flagReceiveData;

// For unix
private CancellationTokenSource pollTokenSource;

public static bool isSHXHIDExist()
{
var instance = new HIDTools();
instance.devList = DeviceList.Local;
return instance.devList.GetHidDeviceOrNull(GT12_HID.VID, GT12_HID.PID) != null;
}


public static HIDTools getInstance()
{
if (instance == null)
{
instance = new HIDTools();
instance.pollTokenSource = new CancellationTokenSource();
instance.devList = DeviceList.Local;

// On unix system we poll!
#if WINDOWS
instance.devList.Changed += (sender, args) =>
{
Console.WriteLine("changed...");
Expand All @@ -58,11 +65,28 @@ public static HIDTools getInstance()
instance.findAndConnect();
}
};
#endif
Task.Run(()=>instance.pollDevStatus(instance.pollTokenSource.Token));
}

return instance;
}

private void pollDevStatus(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
if (devList.GetHidDeviceOrNull(GT12_HID.VID, GT12_HID.PID) == null)
{
isDeviceConnected = false;
updateLabel(false);
}
else
{
findAndConnect();
}
Thread.Sleep(100);
}
}
public HID_STATUS findAndConnect()
{
if (isDeviceConnected)
Expand Down
8 changes: 7 additions & 1 deletion Views/Common/DeviceSelectWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Avalonia;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using MsBox.Avalonia;
using SenhaixFreqWriter.Utils.HID;

namespace SenhaixFreqWriter.Views.Common;
Expand All @@ -10,6 +12,10 @@ public partial class DeviceSelectWindow : Window
{
public DeviceSelectWindow()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
MessageBoxManager.GetMessageBoxStandard("注意", "请您确认是否已经以sudo权限打开软件~如果是请忽略").ShowWindowDialogAsync(this);
}
InitializeComponent();
if (HIDTools.isSHXHIDExist())
{
Expand Down

0 comments on commit 90c12c1

Please sign in to comment.