feat: 遥感测试通过;

master
朱雅鸿 2022-11-08 17:37:39 +08:00
parent f6e29673d1
commit f866baad8d
6 changed files with 86 additions and 14 deletions

View File

@ -7,18 +7,30 @@ using System.Linq;
using Microsoft.VisualBasic; using Microsoft.VisualBasic;
namespace LoraGamepad.Models; 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 class CrsfChMsg
{ {
public int[] channel = new int[16]; public int[] channel = new int[16];
public int channel0 = 0;
public static CrsfChMsg FromBytes(byte[] payload) public static CrsfChMsg FromBytes(byte[] payload)
{ {
var ch = new int[16]; 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}"); // 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 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<ch.Length; i++)
// {
// rs[i].CopyTo(ch,i);
// }
ch[15] = ((raw[0 ] & 0xFF) << 3 ) + ((raw[1 ] & 0xE0) >> 5); //res: 5 ch[15] = ((raw[0 ] & 0xFF) << 3 ) + ((raw[1 ] & 0xE0) >> 5); //res: 5
ch[14] = ((raw[1 ] & 0x1F) << 6 ) + ((raw[2 ] & 0xFC) >> 2); //res: 3 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); 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[2] = ((raw[17] & 0x01) << 10) + ((raw[18] & 0xFF) << 2) + ((raw[19] & 0xC0) >> 6);
ch[1] = ((raw[19] & 0x3F) << 5 ) + ((raw[20] & 0xF8) >> 3); ch[1] = ((raw[19] & 0x3F) << 5 ) + ((raw[20] & 0xF8) >> 3);
ch[0] = ((raw[20] & 0x07) << 8 ) + ((raw[21] & 0xFF) >> 0); ch[0] = ((raw[20] & 0x07) << 8 ) + ((raw[21] & 0xFF) >> 0);
// ch[1] = ((payload[2] & 0xF8) >> 3) + ((payload[3] & 0x3F) << 5); // res: 2 // 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[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 // ch[3] = (uint)(((payload[5] & 0x7F) << 4) + ((payload[6] & 0xF0) >> 4)); // res: 4
ScalingChannel(ch, out var sch);
return new CrsfChMsg() 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();
}
} }

View File

@ -128,7 +128,7 @@ namespace Autolabor
private const int PortInfoBufferSize = 32; private const int PortInfoBufferSize = 32;
private const int ReadBufferSize = 1024; private const int ReadBufferSize = 64;
private const int WriteBufferSize = 1024; private const int WriteBufferSize = 1024;
private const int ExceptionBufferSize = 256; //固定尺寸,不能修改 private const int ExceptionBufferSize = 256; //固定尺寸,不能修改

View File

@ -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);
}
}

View File

@ -17,7 +17,6 @@ public class CrsfParserPipeIn: IAsyncPipe<byte[],CrsfChMsg>
switch (data[0]) switch (data[0])
{ {
case 0x16: case 0x16:
Console.WriteLine("Get Channel Message!");
OnOut(CrsfChMsg.FromBytes(data)); OnOut(CrsfChMsg.FromBytes(data));
break; break;
} }

View File

@ -18,6 +18,7 @@ public class TProViewModel : ViewModelBase
private ObservableCollection<CBPortItem> _portList = new(); private ObservableCollection<CBPortItem> _portList = new();
public CBPortItem PortSelectItem { get; set; } public CBPortItem PortSelectItem { get; set; }
public SliderConfig SliderValue { get; set; }
private readonly SerialPipeIn _serialPipeIn; private readonly SerialPipeIn _serialPipeIn;
private readonly CrsfParserPipeIn _crsfParserPipeIn; private readonly CrsfParserPipeIn _crsfParserPipeIn;
@ -27,14 +28,18 @@ public class TProViewModel : ViewModelBase
public Thread ReadThread; public Thread ReadThread;
public TProViewModel() public TProViewModel()
{ {
SliderValue = new SliderConfig();
PortList = CBPortItem.GetPortList(); PortList = CBPortItem.GetPortList();
_serialPipeIn = new SerialPipeIn(); _serialPipeIn = new SerialPipeIn();
_crsfParserPipeIn = new CrsfParserPipeIn(); _crsfParserPipeIn = new CrsfParserPipeIn();
_serialPipeIn.OnOut += _crsfParserPipeIn.Push; _serialPipeIn.OnOut += _crsfParserPipeIn.Push;
_crsfParserPipeIn.OnOut += data => _crsfParserPipeIn.OnOut += data =>
{ {
Console.WriteLine( SliderValue.RightHorizonValue = data.channel[0];
$"Get Channel 0:{data.channel[0]},1:{data.channel[1]},2:{data.channel[2]},3:{data.channel[3]}"); SliderValue.RightVerticalValue = data.channel[1];
SliderValue.LeftHorizonValue = data.channel[2];
SliderValue.LeftVerticalValue = data.channel[3];
}; };
ReadThread = new Thread(ReadThreadEntry); ReadThread = new Thread(ReadThreadEntry);
@ -52,7 +57,7 @@ public class TProViewModel : ViewModelBase
// Console.WriteLine(BitConverter.ToString(data)); // Console.WriteLine(BitConverter.ToString(data));
_serialPipeIn.Push(data); _serialPipeIn.Push(data);
} }
// Thread.Sleep(1); SpinWait.SpinUntil(() => false, 1);
} }
} }

View File

@ -29,6 +29,12 @@
<Setter Property="Background" Value="{DynamicResource BrightRedBrush}"/> <Setter Property="Background" Value="{DynamicResource BrightRedBrush}"/>
<Setter Property="TextBlock.Foreground" Value="White"/> <Setter Property="TextBlock.Foreground" Value="White"/>
</Style> </Style>
<Style Selector="Slider">
<Setter Property="TickFrequency" Value="10"/>
<Setter Property="TickPlacement" Value="BottomRight"/>
<Setter Property="Maximum" Value="100"/>
<Setter Property="Minimum" Value="-100"/>
</Style>
</UserControl.Styles> </UserControl.Styles>
<!-- 页面布局 --> <!-- 页面布局 -->
<Grid RowDefinitions="60,*,40,2*"> <Grid RowDefinitions="60,*,40,2*">
@ -72,15 +78,19 @@
</Grid> </Grid>
<Grid Grid.Row="3" Background="Green" ColumnDefinitions="*,2*,*"> <Grid Grid.Row="3" Background="Green" ColumnDefinitions="*,2*,*">
<Grid Grid.Column="0"> <Grid Grid.Column="0">
<Slider VerticalAlignment="Center"/> <Slider VerticalAlignment="Center"
<Slider Orientation="Vertical" HorizontalAlignment="Center"/> Value="{Binding SliderValue.LeftVerticalValue}"/>
<Slider Orientation="Vertical" HorizontalAlignment="Center"
Value="{Binding SliderValue.LeftHorizonValue}"/>
</Grid> </Grid>
<Grid Grid.Column="1" Background="Purple"> <Grid Grid.Column="1" Background="Purple">
<TextBox Text="Hello T-Pro!"/> <TextBox Text="Hello T-Pro!"/>
</Grid> </Grid>
<Grid Grid.Column="2"> <Grid Grid.Column="2">
<Slider VerticalAlignment="Center"/> <Slider VerticalAlignment="Center"
<Slider Orientation="Vertical" HorizontalAlignment="Center"/> Value="{Binding SliderValue.RightHorizonValue}"/>
<Slider Orientation="Vertical" HorizontalAlignment="Center"
Value="{Binding SliderValue.RightVerticalValue}"/>
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>