diff --git a/LoraGamepad/Models/CrsfChMsg.cs b/LoraGamepad/Models/CrsfChMsg.cs index 171d29b..96de29d 100644 --- a/LoraGamepad/Models/CrsfChMsg.cs +++ b/LoraGamepad/Models/CrsfChMsg.cs @@ -7,18 +7,30 @@ using System.Linq; using Microsoft.VisualBasic; namespace LoraGamepad.Models; - +// OpenTx RC PWM (BF) +// min -1024 172( 22) 988us +// ctr 0 992(124) 1500us +// max 1024 1811(226) 2012us public class CrsfChMsg { public int[] channel = new int[16]; - public int channel0 = 0; - public static CrsfChMsg FromBytes(byte[] payload) { + + var ch = new int[16]; // Console.WriteLine($"Raw Data: 1:{payload[1]:X},2:{payload[2]:X}");//,2:{payload[3]:X},3{payload[4]:X},4{payload[5]:X},5{payload[6]:X}"); var raw = payload.Skip(1).Reverse().ToArray(); + + var bitRaw11 = new BitArray(raw); + + // var rs=Enumerable.Range(0, 11).Select(_=>bitRaw11).ToArray().Reverse().ToList(); + // for (var i = 0; i < rs.Count&&i> 5); //res: 5 ch[14] = ((raw[1 ] & 0x1F) << 6 ) + ((raw[2 ] & 0xFC) >> 2); //res: 3 ch[13] = ((raw[2 ] & 0x03) << 9 ) + ((raw[3 ] & 0xFF) << 1)+ ((raw[4 ] & 0x80) >> 7); @@ -36,12 +48,19 @@ public class CrsfChMsg ch[2] = ((raw[17] & 0x01) << 10) + ((raw[18] & 0xFF) << 2) + ((raw[19] & 0xC0) >> 6); ch[1] = ((raw[19] & 0x3F) << 5 ) + ((raw[20] & 0xF8) >> 3); ch[0] = ((raw[20] & 0x07) << 8 ) + ((raw[21] & 0xFF) >> 0); + // ch[1] = ((payload[2] & 0xF8) >> 3) + ((payload[3] & 0x3F) << 5); // res: 2 // ch[2] = ((payload[3] & 0x03) << 9) + ((payload[4] & 0xFF) << 1) + ((payload[5] & 0x80) >> 7); // res: 16 // ch[3] = (uint)(((payload[5] & 0x7F) << 4) + ((payload[6] & 0xF0) >> 4)); // res: 4 + ScalingChannel(ch, out var sch); return new CrsfChMsg() { - channel = ch + channel = sch }; } + + private static void ScalingChannel(int[] datain, out int[] dataout) + { + dataout = datain.ToList().ConvertAll((a) => (a - 1000) / 9).ToArray(); + } } \ No newline at end of file diff --git a/LoraGamepad/Models/SerialBase.cs b/LoraGamepad/Models/SerialBase.cs index f7cfc1d..59fa403 100644 --- a/LoraGamepad/Models/SerialBase.cs +++ b/LoraGamepad/Models/SerialBase.cs @@ -128,7 +128,7 @@ namespace Autolabor private const int PortInfoBufferSize = 32; - private const int ReadBufferSize = 1024; + private const int ReadBufferSize = 64; private const int WriteBufferSize = 1024; private const int ExceptionBufferSize = 256; //固定尺寸,不能修改 diff --git a/LoraGamepad/Models/SliderConfig.cs b/LoraGamepad/Models/SliderConfig.cs new file mode 100644 index 0000000..7103cdd --- /dev/null +++ b/LoraGamepad/Models/SliderConfig.cs @@ -0,0 +1,39 @@ +using ReactiveUI; + +namespace LoraGamepad.Models; + + +public class SliderConfig : ReactiveObject +{ + private double _rightHorizonValue; + private double _rightVerticalValue; + private double _leftHorizonValue; + private double _lefttVerticalValue; + + public SliderConfig() + { + _rightHorizonValue = _rightVerticalValue = _leftHorizonValue = _lefttVerticalValue = 0; + } + + public double RightHorizonValue + { + get => _rightHorizonValue; + set => this.RaiseAndSetIfChanged(ref _rightHorizonValue, value); + } + + public double RightVerticalValue + { + get => _rightVerticalValue; + set => this.RaiseAndSetIfChanged(ref _rightVerticalValue, value); + } + public double LeftHorizonValue + { + get => _leftHorizonValue; + set => this.RaiseAndSetIfChanged(ref _leftHorizonValue, value); + } + public double LeftVerticalValue + { + get => _lefttVerticalValue; + set => this.RaiseAndSetIfChanged(ref _lefttVerticalValue, value); + } +} \ No newline at end of file diff --git a/LoraGamepad/Util/CrsfParserPipeIn.cs b/LoraGamepad/Util/CrsfParserPipeIn.cs index ebce00e..42822d1 100644 --- a/LoraGamepad/Util/CrsfParserPipeIn.cs +++ b/LoraGamepad/Util/CrsfParserPipeIn.cs @@ -17,7 +17,6 @@ public class CrsfParserPipeIn: IAsyncPipe switch (data[0]) { case 0x16: - Console.WriteLine("Get Channel Message!"); OnOut(CrsfChMsg.FromBytes(data)); break; } diff --git a/LoraGamepad/ViewModels/TProViewModel.cs b/LoraGamepad/ViewModels/TProViewModel.cs index e9a0e26..4575c07 100644 --- a/LoraGamepad/ViewModels/TProViewModel.cs +++ b/LoraGamepad/ViewModels/TProViewModel.cs @@ -18,7 +18,8 @@ public class TProViewModel : ViewModelBase private ObservableCollection _portList = new(); public CBPortItem PortSelectItem { get; set; } - + public SliderConfig SliderValue { get; set; } + private readonly SerialPipeIn _serialPipeIn; private readonly CrsfParserPipeIn _crsfParserPipeIn; public ReactiveCommand OpenPort { get; } @@ -27,14 +28,18 @@ public class TProViewModel : ViewModelBase public Thread ReadThread; public TProViewModel() { + SliderValue = new SliderConfig(); + PortList = CBPortItem.GetPortList(); _serialPipeIn = new SerialPipeIn(); _crsfParserPipeIn = new CrsfParserPipeIn(); _serialPipeIn.OnOut += _crsfParserPipeIn.Push; _crsfParserPipeIn.OnOut += data => { - Console.WriteLine( - $"Get Channel 0:{data.channel[0]},1:{data.channel[1]},2:{data.channel[2]},3:{data.channel[3]}"); + SliderValue.RightHorizonValue = data.channel[0]; + SliderValue.RightVerticalValue = data.channel[1]; + SliderValue.LeftHorizonValue = data.channel[2]; + SliderValue.LeftVerticalValue = data.channel[3]; }; ReadThread = new Thread(ReadThreadEntry); @@ -52,7 +57,7 @@ public class TProViewModel : ViewModelBase // Console.WriteLine(BitConverter.ToString(data)); _serialPipeIn.Push(data); } - // Thread.Sleep(1); + SpinWait.SpinUntil(() => false, 1); } } diff --git a/LoraGamepad/Views/TProView.axaml b/LoraGamepad/Views/TProView.axaml index 802ccdc..2f572be 100644 --- a/LoraGamepad/Views/TProView.axaml +++ b/LoraGamepad/Views/TProView.axaml @@ -29,6 +29,12 @@ + @@ -72,15 +78,19 @@ - - + + - - + +