124 lines
3.7 KiB
C#
124 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Reactive;
|
|
using System.Threading;
|
|
using LoraGamepad.Models;
|
|
using LoraGamepad.Util;
|
|
using ReactiveUI;
|
|
|
|
namespace LoraGamepad.ViewModels;
|
|
|
|
public class TProViewModel : ViewModelBase
|
|
{
|
|
|
|
private bool _isOpenPortButtonVisible = true;
|
|
private bool _isClosePortButtonVisible;
|
|
|
|
private ObservableCollection<CBPortItem> _portList = new();
|
|
public CBPortItem PortSelectItem { get; set; }
|
|
public SliderConfig SliderValue { get; set; }
|
|
|
|
private readonly SerialPipeIn _serialPipeIn;
|
|
private readonly CrsfParserPipeIn _crsfParserPipeIn;
|
|
public ReactiveCommand<Unit, Unit> OpenPort { get; }
|
|
public ReactiveCommand<Unit, Unit> ClosePort { get; }
|
|
|
|
public Thread ReadThread;
|
|
public TProViewModel()
|
|
{
|
|
SliderValue = new SliderConfig();
|
|
|
|
PortList = CBPortItem.GetPortList();
|
|
_serialPipeIn = new SerialPipeIn();
|
|
_crsfParserPipeIn = new CrsfParserPipeIn();
|
|
_serialPipeIn.OnOut += _crsfParserPipeIn.Push;
|
|
_crsfParserPipeIn.OnOut += data =>
|
|
{
|
|
SliderValue.RightHorizonValue = data.channel[0];
|
|
SliderValue.RightVerticalValue = data.channel[1];
|
|
SliderValue.LeftHorizonValue = data.channel[2];
|
|
SliderValue.LeftVerticalValue = data.channel[3];
|
|
};
|
|
ReadThread = new Thread(ReadThreadEntry);
|
|
|
|
OpenPort = ReactiveCommand.Create(PortOpenButtonClick);
|
|
ClosePort = ReactiveCommand.Create(PortCloseButtonClick);
|
|
|
|
}
|
|
|
|
public void ReadThreadEntry()
|
|
{
|
|
while (true)
|
|
{
|
|
if (SerialPort.Me.TryRead(out var data))
|
|
{
|
|
// Console.WriteLine(BitConverter.ToString(data));
|
|
_serialPipeIn.Push(data);
|
|
}
|
|
SpinWait.SpinUntil(() => false, 1);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ComboBox串口列表绑定对象
|
|
/// </summary>
|
|
public ObservableCollection<CBPortItem> PortList
|
|
{
|
|
get => _portList;
|
|
set => this.RaiseAndSetIfChanged(ref _portList, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刷新串口列表事件处理函数
|
|
/// </summary>
|
|
public void OnPortListComboBoxPressed()
|
|
{
|
|
var newList = CBPortItem.GetPortList();
|
|
PortList.Except(newList,new CBPortItemCompare()).ToList().ForEach(a => { PortList.Remove(a); });
|
|
newList.Except(PortList, new CBPortItemCompare()).ToList().ForEach(a => { PortList.Add(a); });
|
|
newList.Except(PortList, new CBPortItemCompare()).ToList().ForEach(a => { PortList.Add(a); });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭串口按键可见性属性
|
|
/// </summary>
|
|
public bool IsClosePortButtonVisible
|
|
{
|
|
get => _isClosePortButtonVisible;
|
|
set => this.RaiseAndSetIfChanged(ref _isClosePortButtonVisible, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开串口按键可见性属性
|
|
/// </summary>
|
|
public bool IsOpenPortButtonVisible
|
|
{
|
|
get => _isOpenPortButtonVisible;
|
|
set => this.RaiseAndSetIfChanged(ref _isOpenPortButtonVisible, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开串口按键响应
|
|
/// </summary>
|
|
private void PortOpenButtonClick()
|
|
{
|
|
if (PortSelectItem.PortName == "") return;
|
|
SerialPort.Me.Open(PortSelectItem.PortName,115200,1000);
|
|
IsOpenPortButtonVisible = false;
|
|
IsClosePortButtonVisible = true;
|
|
ReadThread.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭串口按键响应
|
|
/// </summary>
|
|
private void PortCloseButtonClick()
|
|
{
|
|
SerialPort.Me.Close();
|
|
IsOpenPortButtonVisible = true;
|
|
IsClosePortButtonVisible = false;
|
|
}
|
|
|
|
} |