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 _DecodePlotDictionary = new Dictionary(); private Dictionary _VLIndicationAreaDictionary = new Dictionary(); 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(); } /// /// 添加可拖拽指示区域 /// 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(); } /// /// 添加波形 /// 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; } } /// /// 网格亮度 /// public Int32 GridIntensity { get => DisplayPresenter.GridIntensity; set => DisplayPresenter.GridIntensity = value; } ITimebasePrsnt IView.Presenter { get => TmbPresenter; set => TmbPresenter = (TimebasePrsnt)value; } ITriggerPrsnt IView.Presenter { get => TrgPresenter; set => TrgPresenter = (TriggerPrsnt)value; } public TriggerPrsnt TrgPresenter { get; set; } IBadge IView.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(); } /// /// 更新波形 /// 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 GetSimulationData() { Random random = new Random(); List datas = new List(); 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; } } }