123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- using ScottPlot;
- using ScottPlot.Plottable;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uestc.Auto6.Dso.ComModel;
- namespace Uestc.Auto6.Dso.U2
- {
- public partial class DecodeFigure : BaseDisplayForm, ITimebaseView, ITriggerView, IChnlView,IProtocolView
- {
- public DecodeFigure()
- {
- InitializeComponent();
- }
- public DecodeFigure(DisplayPrsnt displayPrsnt,ChannelId id) : this()
- {
- DisplayPresenter = displayPrsnt;
- this._Id = id;
- ConfigureWavePlot();
- _TimerRefresh.Interval = 100;
- _TimerRefresh.Tick += TimerRefresh_Tick;
- _TimerRefresh.Enabled = true;
- }
- private Dictionary<ChannelId, DecodePlot> _DecodePlotDictionary = new Dictionary<ChannelId, DecodePlot>();
- private Dictionary<ChannelId, VLIndicationArea> _VLIndicationAreaDictionary = new Dictionary<ChannelId, VLIndicationArea>();
- private System.Windows.Forms.Timer _TimerRefresh = new System.Windows.Forms.Timer();
- private VLIndicationArea _VLIndicationArea;
- private ScottPlot.Plottable.DecodePlot _DecodePlot;
- private ChannelId _Id;
- private Boolean _IsRefreshing = false;
- private Boolean _IsDraging = false;
- private DecodePrsnt _DecodePrsnt;
- private void ConfigureWavePlot()
- {
- //控件属性配置
- ScottPlotFormControl.Configuration.MiddleClickAutoAxisMarginX = 0;
- ScottPlotFormControl.Configuration.Quality = 0;
- //波形线路名称
- ScottPlot.Renderable.Legend legend = ScottPlotFormControl.Plot.Legend();
- legend.FontColor = Color.Gray;
- legend.OutlineColor = Color.White;
- legend.FillColor = Color.Black;
- ScottPlotFormControl.Plot.AxisAutoX(margin: 0);
- ScottPlotFormControl.Plot.SetAxisLimits(xMin: 0, xMax: 10000, yMin: -5000, yMax: 5000);//初始视阈
- ScottPlotFormControl.Plot.SetViewLimits(xMin: -32768, xMax: 32768, yMin: -10000, yMax: 10000);//视阈限制
- ScottPlotFormControl.Dock = DockStyle.Fill;
- //图形颜色设置
- ScottPlotFormControl.Plot.Style(figureBackground: Color.Transparent, dataBackground: Color.Transparent, grid: Color.Gray, tick: Color.Gray, axisLabel: Color.White, titleLabel: Color.Gray);
- //网格属性配置
- ScottPlotFormControl.Plot.Grid(enable: true, color: Color.Gray, lineStyle: LineStyle.Dot);
- ScottPlotFormControl.BackColor = Color.Black;
- AddWavePlot();
- AddDragArea();
- }
- /// <summary>
- /// 添加可拖拽指示区域
- /// </summary>
- private void AddDragArea()
- {
- PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1);
- _VLIndicationArea = ScottPlotFormControl.Plot.AddVLIndicationArea(plotDimensions, color: Color.Red);
- _VLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0));
- //_BusPlot.YOffset = vLIndicationArea.GetIndicationValue();
- //vLIndicationArea.LabelFont = new Font("微软雅黑", 6);
- _VLIndicationAreaDictionary[_Id] = _VLIndicationArea;
- _VLIndicationArea.Dragged += (sender, e) =>
- {
- if (Program.Oscilloscope.TryGetChannel(_Id, out IChnlPrsnt channel))
- {
- double s = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)_VLIndicationArea.GetIndicationValue());
- (channel as DecodePrsnt).PosIndexBymDiv = s;
- }
- };
- //vLIndicationArea.MouseDown += (sender, e) =>
- //{
- // Program.Oscilloscope.FocusId = Id;
- //};
- }
- private void InitDecode()
- {
- if (_DecodePrsnt == null)
- return;
- _DecodePlot.LineColor = _DecodePrsnt.DrawColor;
- _DecodePlot.Label = _DecodePrsnt.Name;
- //_BusPlot.YOffset = busPrsnt.PosIndexBymDiv;
- _VLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(_DecodePrsnt.PosIndexBymDiv));
- _VLIndicationArea.Color = _DecodePrsnt.DrawColor;
- //vLIndicationArea.Label = busPrsnt.Label;
- _VLIndicationArea.Name = _DecodePrsnt.Name;
- ScottPlotFormControl.Render();
- }
- /// <summary>
- /// 添加波形
- /// </summary>
- private void AddWavePlot()
- {
- if (_DecodePlot == null)
- //if (Program.Oscilloscope.TryGetChannel(_Id, out IChnlPrsnt channel))
- if (DecodeApp.Default.TryGetPresenter(_Id,out var decodePrsnt))
- if (true /*channel.Pack != null*/)
- {
- _DecodePlot = ScottPlotFormControl.Plot.AddDecode(new DecodePlot.BaseDecodeData[0]);
- _DecodePlotDictionary[_Id] = _DecodePlot;
- }
- }
- /// <summary>
- /// 网格亮度
- /// </summary>
- public Int32 GridIntensity
- {
- get => DisplayPresenter.GridIntensity;
- set => DisplayPresenter.GridIntensity = value;
- }
- ITimebasePrsnt IView<ITimebasePrsnt>.Presenter
- {
- get => TmbPresenter;
- set => TmbPresenter = (TimebasePrsnt)value;
- }
- ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
- {
- get => TrgPresenter;
- set => TrgPresenter = (TriggerPrsnt)value;
- }
- public TriggerPrsnt TrgPresenter
- {
- get;
- set;
- }
- IBadge IView<IBadge>.Presenter { get; set; }
- public DisplayPrsnt DisplayPresenter
- {
- get;
- set;
- }
- public TimebasePrsnt TmbPresenter
- {
- get;
- set;
- }
- public DecodePrsnt DecodePrsnt
- {
- get => _DecodePrsnt;
- set
- {
- if (value != _DecodePrsnt)
- {
- _DecodePrsnt = value;
- InitDecode();
- if (value != null && value.DecodeChPrsnt != null)
- value.DecodeChPrsnt.TryAddView(this);
- _DecodePlot.DecodeDatas.Clear();
- _DecodePlot.DecodeDatas.AddRange(GetSimulationData());
- }
- }
- }
- public IProtocolPrsnt Presenter { get; set; }
- public void UpdateView(string propertyName)
- {
- switch (propertyName)
- {
- case nameof(DecodePrsnt.Label):
- _DecodePlot.Label = DecodePrsnt.Label;
- break;
- case nameof(DecodePrsnt.Name):
- _VLIndicationArea.Name = DecodePrsnt.Name;
- break;
- case nameof(DecodePrsnt.Format):
- if (_DecodePlot!=null && _DecodePlot.DecodeDatas != null && _DecodePlot.DecodeDatas.Count > 0)
- {
- _DecodePlot.DecodeDatas.Where(x => x is DecodePlot.HexagonDecodeData).Select(x => x as DecodePlot.HexagonDecodeData).ToList().ForEach(x => x.DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format);
- this.ScottPlotFormControl.Plot.Render(true);
- }
- break;
- }
- }
- private void TimerRefresh_Tick(object sender, EventArgs e)
- {
- if (!_IsRefreshing)
- DrawWave();
- }
-
-
- /// <summary>
- /// 更新波形
- /// </summary>
- private void DrawWave()
- {
- _IsRefreshing = true;
- if (_DecodePlotDictionary.Count > 0)
- {
- //foreach (var id in ChannelIdExt.GetBuses())
- //{
- if (DecodeApp.Default.TryGetPresenter(_Id, out DecodePrsnt channel))
- {
- _DecodePlotDictionary[_Id].IsVisible = channel.Active;
- if (channel.Active == true)
- {
- //_BusPlot.YOffset = busPrsnt.PosIndexBymDiv;
- _DecodePlot.DecodeDatas.Clear();
- _DecodePlot.DecodeDatas.AddRange(GetSimulationData());
- //更新数据
- //if (channel.Pack != null)
- // _WavePlotDictionary[id].YTs = channel.Pack.Buffer;
- //更新通道刻度信息
- if (channel.Id == DsoPrsnt.FocusId)
- {
- ScottPlotFormControl.Plot.ResetChannelParameter((Int32)(channel as DecodePrsnt).PosIndexBymDiv, (channel as DecodePrsnt).ScaleIndex, (channel as DecodePrsnt).Prefix.ToString(), channel.Unit, Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.White)/*channel.DrawColor*/);
- ScottPlotFormControl.Plot.YAxis.IsVisible = true;
- }
- if (_VLIndicationAreaDictionary.ContainsKey(_Id) && _IsDraging == false)
- {
- _VLIndicationAreaDictionary[_Id].IsVisible = true;
- _VLIndicationAreaDictionary[_Id].Color = channel.DrawColor;
- _VLIndicationAreaDictionary[_Id].SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-(Int32)(channel as DecodePrsnt).PosIndexBymDiv));
- }
- }
- else
- _VLIndicationAreaDictionary[_Id].IsVisible = false;
- }
- //}
- }
- if (_DecodePlotDictionary.Count == 0)
- AddWavePlot();
- ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PosIndexBymDiv - 5000, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.White));
- ScottPlotFormControl.Render(skipIfCurrentlyRendering: true);//未知栈溢出错误,标记栈的可能性较大
- _IsRefreshing = false;
- }
- private List<DecodePlot.BaseDecodeData> GetSimulationData()
- {
- Random random = new Random();
- List<DecodePlot.BaseDecodeData> datas = new List<DecodePlot.BaseDecodeData>();
- datas.Add(new DecodePlot.LineDecodeData()
- {
- XStart = random.Next(100,2000),
- BorderWidth = 2,
- Color = Color.White,
- });
- Int32 count = random.Next(1, 8);
- for (Int32 i = 0; i < count; i++)
- {
- datas.Add(new DecodePlot.HexagonDecodeData()
- {
- Color = Color.Green,
- Data = (Byte)random.Next(0, 255),
- DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format,
- XStart = datas[^1].XStart + (i==0? datas[^1].BorderWidth:(datas[^1] as DecodePlot.HexagonDecodeData).XLenght),
- XLenght = random.Next(100, 1000),
- });
- }
- datas.Add(new DecodePlot.LineDecodeData()
- {
- XStart = datas[^1].XStart + (datas[^1] as DecodePlot.HexagonDecodeData).XLenght,
- BorderWidth = 2,
- Color = Color.Red,
- });
- datas.Add(new DecodePlot.LineDecodeData()
- {
- XStart = random.Next(5000, 5500),
- BorderWidth = 2,
- Color = Color.White,
- });
- count = random.Next(1, 8);
- for (Int32 i = 0; i < count; i++)
- {
- datas.Add(new DecodePlot.HexagonDecodeData()
- {
- Color = Color.Green,
- Data = (Byte)random.Next(0, 255),
- DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format,
- XStart = datas[^1].XStart + (i == 0 ? datas[^1].BorderWidth : (datas[^1] as DecodePlot.HexagonDecodeData).XLenght),
- XLenght = random.Next(100, 1000),
- });
- }
- datas.Add(new DecodePlot.LineDecodeData()
- {
- XStart = datas[^1].XStart + (datas[^1] as DecodePlot.HexagonDecodeData).XLenght,
- BorderWidth = 2,
- Color = Color.Red,
- });
- return datas;
- }
- }
- }
|