2022-11-06 20:13:24 +08:00
|
|
|
using System;
|
2022-11-06 18:44:23 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2022-11-06 20:13:24 +08:00
|
|
|
using System.Diagnostics;
|
2022-11-06 18:44:23 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Reactive;
|
2022-11-06 20:13:24 +08:00
|
|
|
using System.Threading;
|
2022-11-06 18:44:23 +08:00
|
|
|
using LoraGamepad.Models;
|
2022-11-06 20:13:24 +08:00
|
|
|
using LoraGamepad.Util;
|
2022-11-06 18:44:23 +08:00
|
|
|
using ReactiveUI;
|
|
|
|
|
2022-11-06 14:49:30 +08:00
|
|
|
namespace LoraGamepad.ViewModels;
|
|
|
|
|
|
|
|
public class TProViewModel : ViewModelBase
|
|
|
|
{
|
2022-11-06 18:44:23 +08:00
|
|
|
|
|
|
|
private bool _isOpenPortButtonVisible = true;
|
|
|
|
private bool _isClosePortButtonVisible;
|
|
|
|
|
|
|
|
private ObservableCollection<CBPortItem> _portList = new();
|
|
|
|
public CBPortItem PortSelectItem { get; set; }
|
2022-11-08 23:54:44 +08:00
|
|
|
public SliderConfig SliderValue { get; }
|
|
|
|
public BtnConfig BtnIsPressed { get; }
|
2022-11-08 17:37:39 +08:00
|
|
|
|
2022-11-06 20:13:24 +08:00
|
|
|
private readonly SerialPipeIn _serialPipeIn;
|
2022-11-08 15:05:47 +08:00
|
|
|
private readonly CrsfParserPipeIn _crsfParserPipeIn;
|
2022-11-06 18:44:23 +08:00
|
|
|
public ReactiveCommand<Unit, Unit> OpenPort { get; }
|
|
|
|
public ReactiveCommand<Unit, Unit> ClosePort { get; }
|
2022-11-06 20:13:24 +08:00
|
|
|
|
|
|
|
public Thread ReadThread;
|
2022-11-06 14:49:30 +08:00
|
|
|
public TProViewModel()
|
|
|
|
{
|
2022-11-08 17:37:39 +08:00
|
|
|
SliderValue = new SliderConfig();
|
2022-11-08 23:54:44 +08:00
|
|
|
BtnIsPressed = new BtnConfig();
|
2022-11-08 17:37:39 +08:00
|
|
|
|
2022-11-06 18:44:23 +08:00
|
|
|
PortList = CBPortItem.GetPortList();
|
2022-11-06 20:13:24 +08:00
|
|
|
_serialPipeIn = new SerialPipeIn();
|
2022-11-08 15:05:47 +08:00
|
|
|
_crsfParserPipeIn = new CrsfParserPipeIn();
|
|
|
|
_serialPipeIn.OnOut += _crsfParserPipeIn.Push;
|
|
|
|
_crsfParserPipeIn.OnOut += data =>
|
|
|
|
{
|
2022-11-08 17:37:39 +08:00
|
|
|
SliderValue.RightHorizonValue = data.channel[0];
|
|
|
|
SliderValue.RightVerticalValue = data.channel[1];
|
|
|
|
SliderValue.LeftHorizonValue = data.channel[2];
|
|
|
|
SliderValue.LeftVerticalValue = data.channel[3];
|
2022-11-08 23:54:44 +08:00
|
|
|
// Console.WriteLine($"{data.channel[4]},{data.channel[5]},{data.channel[6]},{data.channel[7]},{data.channel[8]},{data.channel[9]}");
|
|
|
|
// Console.WriteLine($"{data.channel[10]},{data.channel[11]},{data.channel[12]},{data.channel[13]},{data.channel[14]},{data.channel[15]}");
|
|
|
|
SliderValue.LeftUpValue = data.channel[4];
|
|
|
|
BtnIsPressed.BtnLeftUp = data.channel[5] > 0;
|
|
|
|
BtnIsPressed.BtnRightUp = data.channel[6] > 0;
|
|
|
|
BtnIsPressed.BtnRightDown = data.channel[7] > 0;
|
|
|
|
SliderValue.MidLeftValue = data.channel[8];
|
|
|
|
SliderValue.MidRightValue = data.channel[9];
|
|
|
|
BtnIsPressed.BtnModel1 = data.channel[10] > 0;
|
|
|
|
BtnIsPressed.BtnModel2 = data.channel[11] > 0;
|
|
|
|
BtnIsPressed.BtnModel3 = data.channel[12] > 0;
|
|
|
|
BtnIsPressed.BtnModel4 = data.channel[13] > 0;
|
|
|
|
BtnIsPressed.BtnModel5 = data.channel[14] > 0;
|
|
|
|
BtnIsPressed.BtnModel6 = data.channel[15] > 0;
|
2022-11-08 15:05:47 +08:00
|
|
|
};
|
2022-11-06 20:13:24 +08:00
|
|
|
ReadThread = new Thread(ReadThreadEntry);
|
2022-11-06 14:49:30 +08:00
|
|
|
|
2022-11-06 18:44:23 +08:00
|
|
|
OpenPort = ReactiveCommand.Create(PortOpenButtonClick);
|
|
|
|
ClosePort = ReactiveCommand.Create(PortCloseButtonClick);
|
2022-11-06 20:13:24 +08:00
|
|
|
|
|
|
|
}
|
2022-11-08 15:05:47 +08:00
|
|
|
|
2022-11-06 20:13:24 +08:00
|
|
|
public void ReadThreadEntry()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (SerialPort.Me.TryRead(out var data))
|
|
|
|
{
|
|
|
|
// Console.WriteLine(BitConverter.ToString(data));
|
|
|
|
_serialPipeIn.Push(data);
|
|
|
|
}
|
2022-11-08 17:37:39 +08:00
|
|
|
SpinWait.SpinUntil(() => false, 1);
|
2022-11-06 20:13:24 +08:00
|
|
|
}
|
2022-11-06 18:44:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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);
|
2022-11-06 14:49:30 +08:00
|
|
|
}
|
2022-11-06 18:44:23 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 打开串口按键可见性属性
|
|
|
|
/// </summary>
|
|
|
|
public bool IsOpenPortButtonVisible
|
|
|
|
{
|
|
|
|
get => _isOpenPortButtonVisible;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _isOpenPortButtonVisible, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 打开串口按键响应
|
|
|
|
/// </summary>
|
|
|
|
private void PortOpenButtonClick()
|
|
|
|
{
|
|
|
|
if (PortSelectItem.PortName == "") return;
|
2022-11-08 23:54:44 +08:00
|
|
|
SerialPort.Me.Open(PortSelectItem.PortName,250000,1000);
|
2022-11-06 18:44:23 +08:00
|
|
|
IsOpenPortButtonVisible = false;
|
|
|
|
IsClosePortButtonVisible = true;
|
2022-11-06 20:13:24 +08:00
|
|
|
ReadThread.Start();
|
2022-11-06 18:44:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 关闭串口按键响应
|
|
|
|
/// </summary>
|
|
|
|
private void PortCloseButtonClick()
|
|
|
|
{
|
|
|
|
SerialPort.Me.Close();
|
|
|
|
IsOpenPortButtonVisible = true;
|
|
|
|
IsClosePortButtonVisible = false;
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:49:30 +08:00
|
|
|
}
|