DecodeFigure.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using ScottPlot;
  2. using ScottPlot.Plottable;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using Uestc.Auto6.Dso.Core;
  13. using Uestc.Auto6.Dso.Core.Decode;
  14. using Uestc.Auto6.Dso.ComModel;
  15. namespace Uestc.Auto6.Dso.U2
  16. {
  17. public partial class DecodeFigure : BaseDisplayForm, ITimebaseView, ITriggerView, IChnlView,IProtocolView
  18. {
  19. public DecodeFigure()
  20. {
  21. InitializeComponent();
  22. }
  23. public DecodeFigure(DisplayPrsnt displayPrsnt,ChannelId id) : this()
  24. {
  25. DisplayPresenter = displayPrsnt;
  26. this._Id = id;
  27. ConfigureWavePlot();
  28. _TimerRefresh.Interval = 100;
  29. _TimerRefresh.Tick += TimerRefresh_Tick;
  30. _TimerRefresh.Enabled = true;
  31. }
  32. private Dictionary<ChannelId, DecodePlot> _DecodePlotDictionary = new Dictionary<ChannelId, DecodePlot>();
  33. private Dictionary<ChannelId, VLIndicationArea> _VLIndicationAreaDictionary = new Dictionary<ChannelId, VLIndicationArea>();
  34. private System.Windows.Forms.Timer _TimerRefresh = new System.Windows.Forms.Timer();
  35. private VLIndicationArea _VLIndicationArea;
  36. private ScottPlot.Plottable.DecodePlot _DecodePlot;
  37. private ChannelId _Id;
  38. private Boolean _IsRefreshing = false;
  39. private Boolean _IsDraging = false;
  40. private DecodePrsnt _DecodePrsnt;
  41. private void ConfigureWavePlot()
  42. {
  43. //控件属性配置
  44. ScottPlotFormControl.Configuration.MiddleClickAutoAxisMarginX = 0;
  45. ScottPlotFormControl.Configuration.Quality = 0;
  46. //波形线路名称
  47. ScottPlot.Renderable.Legend legend = ScottPlotFormControl.Plot.Legend();
  48. legend.FontColor = Color.Gray;
  49. legend.OutlineColor = Color.White;
  50. legend.FillColor = Color.Black;
  51. ScottPlotFormControl.Plot.AxisAutoX(margin: 0);
  52. ScottPlotFormControl.Plot.SetAxisLimits(xMin: 0, xMax: 10000, yMin: -5000, yMax: 5000);//初始视阈
  53. ScottPlotFormControl.Plot.SetViewLimits(xMin: -32768, xMax: 32768, yMin: -10000, yMax: 10000);//视阈限制
  54. ScottPlotFormControl.Dock = DockStyle.Fill;
  55. //图形颜色设置
  56. ScottPlotFormControl.Plot.Style(figureBackground: Color.Transparent, dataBackground: Color.Transparent, grid: Color.Gray, tick: Color.Gray, axisLabel: Color.White, titleLabel: Color.Gray);
  57. //网格属性配置
  58. ScottPlotFormControl.Plot.Grid(enable: true, color: Color.Gray, lineStyle: LineStyle.Dot);
  59. ScottPlotFormControl.BackColor = Color.Black;
  60. AddWavePlot();
  61. AddDragArea();
  62. }
  63. /// <summary>
  64. /// 添加可拖拽指示区域
  65. /// </summary>
  66. private void AddDragArea()
  67. {
  68. PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1);
  69. _VLIndicationArea = ScottPlotFormControl.Plot.AddVLIndicationArea(plotDimensions, color: Color.Red);
  70. _VLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0));
  71. //_BusPlot.YOffset = vLIndicationArea.GetIndicationValue();
  72. //vLIndicationArea.LabelFont = new Font("微软雅黑", 6);
  73. _VLIndicationAreaDictionary[_Id] = _VLIndicationArea;
  74. _VLIndicationArea.Dragged += (sender, e) =>
  75. {
  76. if (Program.Oscilloscope.TryGetChannel(_Id, out IChnlPrsnt channel))
  77. {
  78. double s = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)_VLIndicationArea.GetIndicationValue());
  79. (channel as DecodePrsnt).PosIndexBymDiv = s;
  80. }
  81. };
  82. //vLIndicationArea.MouseDown += (sender, e) =>
  83. //{
  84. // Program.Oscilloscope.FocusId = Id;
  85. //};
  86. }
  87. private void InitDecode()
  88. {
  89. if (_DecodePrsnt == null)
  90. return;
  91. _DecodePlot.LineColor = _DecodePrsnt.DrawColor;
  92. _DecodePlot.Label = _DecodePrsnt.Name;
  93. //_BusPlot.YOffset = busPrsnt.PosIndexBymDiv;
  94. _VLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(_DecodePrsnt.PosIndexBymDiv));
  95. _VLIndicationArea.Color = _DecodePrsnt.DrawColor;
  96. //vLIndicationArea.Label = busPrsnt.Label;
  97. _VLIndicationArea.Name = _DecodePrsnt.Name;
  98. ScottPlotFormControl.Render();
  99. }
  100. /// <summary>
  101. /// 添加波形
  102. /// </summary>
  103. private void AddWavePlot()
  104. {
  105. if (_DecodePlot == null)
  106. //if (Program.Oscilloscope.TryGetChannel(_Id, out IChnlPrsnt channel))
  107. if (DecodeApp.Default.TryGetPresenter(_Id,out var decodePrsnt))
  108. if (true /*channel.Pack != null*/)
  109. {
  110. _DecodePlot = ScottPlotFormControl.Plot.AddDecode(new DecodePlot.BaseDecodeData[0]);
  111. _DecodePlotDictionary[_Id] = _DecodePlot;
  112. }
  113. }
  114. /// <summary>
  115. /// 网格亮度
  116. /// </summary>
  117. public Int32 GridIntensity
  118. {
  119. get => DisplayPresenter.GridIntensity;
  120. set => DisplayPresenter.GridIntensity = value;
  121. }
  122. ITimebasePrsnt IView<ITimebasePrsnt>.Presenter
  123. {
  124. get => TmbPresenter;
  125. set => TmbPresenter = (TimebasePrsnt)value;
  126. }
  127. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  128. {
  129. get => TrgPresenter;
  130. set => TrgPresenter = (TriggerPrsnt)value;
  131. }
  132. public TriggerPrsnt TrgPresenter
  133. {
  134. get;
  135. set;
  136. }
  137. IBadge IView<IBadge>.Presenter { get; set; }
  138. public DisplayPrsnt DisplayPresenter
  139. {
  140. get;
  141. set;
  142. }
  143. public TimebasePrsnt TmbPresenter
  144. {
  145. get;
  146. set;
  147. }
  148. public DecodePrsnt DecodePrsnt
  149. {
  150. get => _DecodePrsnt;
  151. set
  152. {
  153. if (value != _DecodePrsnt)
  154. {
  155. _DecodePrsnt = value;
  156. InitDecode();
  157. if (value != null && value.DecodeChPrsnt != null)
  158. value.DecodeChPrsnt.TryAddView(this);
  159. _DecodePlot.DecodeDatas.Clear();
  160. _DecodePlot.DecodeDatas.AddRange(GetSimulationData());
  161. }
  162. }
  163. }
  164. public IProtocolPrsnt Presenter { get; set; }
  165. public void UpdateView(string propertyName)
  166. {
  167. switch (propertyName)
  168. {
  169. case nameof(DecodePrsnt.Label):
  170. _DecodePlot.Label = DecodePrsnt.Label;
  171. break;
  172. case nameof(DecodePrsnt.Name):
  173. _VLIndicationArea.Name = DecodePrsnt.Name;
  174. break;
  175. case nameof(DecodePrsnt.Format):
  176. if (_DecodePlot!=null && _DecodePlot.DecodeDatas != null && _DecodePlot.DecodeDatas.Count > 0)
  177. {
  178. _DecodePlot.DecodeDatas.Where(x => x is DecodePlot.HexagonDecodeData).Select(x => x as DecodePlot.HexagonDecodeData).ToList().ForEach(x => x.DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format);
  179. this.ScottPlotFormControl.Plot.Render(true);
  180. }
  181. break;
  182. }
  183. }
  184. private void TimerRefresh_Tick(object sender, EventArgs e)
  185. {
  186. if (!_IsRefreshing)
  187. DrawWave();
  188. }
  189. /// <summary>
  190. /// 更新波形
  191. /// </summary>
  192. private void DrawWave()
  193. {
  194. _IsRefreshing = true;
  195. if (_DecodePlotDictionary.Count > 0)
  196. {
  197. //foreach (var id in ChannelIdExt.GetBuses())
  198. //{
  199. if (DecodeApp.Default.TryGetPresenter(_Id, out DecodePrsnt channel))
  200. {
  201. _DecodePlotDictionary[_Id].IsVisible = channel.Active;
  202. if (channel.Active == true)
  203. {
  204. //_BusPlot.YOffset = busPrsnt.PosIndexBymDiv;
  205. _DecodePlot.DecodeDatas.Clear();
  206. _DecodePlot.DecodeDatas.AddRange(GetSimulationData());
  207. //更新数据
  208. //if (channel.Pack != null)
  209. // _WavePlotDictionary[id].YTs = channel.Pack.Buffer;
  210. //更新通道刻度信息
  211. if (channel.Id == DsoPrsnt.FocusId)
  212. {
  213. 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*/);
  214. ScottPlotFormControl.Plot.YAxis.IsVisible = true;
  215. }
  216. if (_VLIndicationAreaDictionary.ContainsKey(_Id) && _IsDraging == false)
  217. {
  218. _VLIndicationAreaDictionary[_Id].IsVisible = true;
  219. _VLIndicationAreaDictionary[_Id].Color = channel.DrawColor;
  220. _VLIndicationAreaDictionary[_Id].SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-(Int32)(channel as DecodePrsnt).PosIndexBymDiv));
  221. }
  222. }
  223. else
  224. _VLIndicationAreaDictionary[_Id].IsVisible = false;
  225. }
  226. //}
  227. }
  228. if (_DecodePlotDictionary.Count == 0)
  229. AddWavePlot();
  230. ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PosIndexBymDiv - 5000, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.White));
  231. ScottPlotFormControl.Render(skipIfCurrentlyRendering: true);//未知栈溢出错误,标记栈的可能性较大
  232. _IsRefreshing = false;
  233. }
  234. private List<DecodePlot.BaseDecodeData> GetSimulationData()
  235. {
  236. Random random = new Random();
  237. List<DecodePlot.BaseDecodeData> datas = new List<DecodePlot.BaseDecodeData>();
  238. datas.Add(new DecodePlot.LineDecodeData()
  239. {
  240. XStart = random.Next(100,2000),
  241. BorderWidth = 2,
  242. Color = Color.White,
  243. });
  244. Int32 count = random.Next(1, 8);
  245. for (Int32 i = 0; i < count; i++)
  246. {
  247. datas.Add(new DecodePlot.HexagonDecodeData()
  248. {
  249. Color = Color.Green,
  250. Data = (Byte)random.Next(0, 255),
  251. DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format,
  252. XStart = datas[^1].XStart + (i==0? datas[^1].BorderWidth:(datas[^1] as DecodePlot.HexagonDecodeData).XLenght),
  253. XLenght = random.Next(100, 1000),
  254. });
  255. }
  256. datas.Add(new DecodePlot.LineDecodeData()
  257. {
  258. XStart = datas[^1].XStart + (datas[^1] as DecodePlot.HexagonDecodeData).XLenght,
  259. BorderWidth = 2,
  260. Color = Color.Red,
  261. });
  262. datas.Add(new DecodePlot.LineDecodeData()
  263. {
  264. XStart = random.Next(5000, 5500),
  265. BorderWidth = 2,
  266. Color = Color.White,
  267. });
  268. count = random.Next(1, 8);
  269. for (Int32 i = 0; i < count; i++)
  270. {
  271. datas.Add(new DecodePlot.HexagonDecodeData()
  272. {
  273. Color = Color.Green,
  274. Data = (Byte)random.Next(0, 255),
  275. DisPlayFormat = (DecodePlot.DisPlayFormat)DecodePrsnt.Format,
  276. XStart = datas[^1].XStart + (i == 0 ? datas[^1].BorderWidth : (datas[^1] as DecodePlot.HexagonDecodeData).XLenght),
  277. XLenght = random.Next(100, 1000),
  278. });
  279. }
  280. datas.Add(new DecodePlot.LineDecodeData()
  281. {
  282. XStart = datas[^1].XStart + (datas[^1] as DecodePlot.HexagonDecodeData).XLenght,
  283. BorderWidth = 2,
  284. Color = Color.Red,
  285. });
  286. return datas;
  287. }
  288. }
  289. }