I2CAddressPage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using Uestc.Auto6.Dso.Core;
  12. using Uestc.Auto6.Dso.Core.Decode;
  13. using Uni_Trend.MSO7000X.Common.Helper;
  14. using Uni_Trend.MSO7000X.UserControls;
  15. using Uni_Trend.MSO7000X.UserControls.Style;
  16. using Uni_Trend.MSO7000X.Language;
  17. namespace Uestc.Auto6.Dso.Protocol.I2C
  18. {
  19. public partial class I2CAddressPage : UserControl, ITriggerSerialView
  20. {
  21. #region 属性
  22. private FloatForm _FloatForm;
  23. private Int32 _RowHeight = 56;
  24. private Int32 _ColnumCount = 2;
  25. public I2CTrigSerialPrsnt Presenter { get; set; }
  26. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  27. {
  28. get => Presenter;
  29. set => Presenter = (I2CTrigSerialPrsnt)value;
  30. }
  31. protected new Boolean DesignMode
  32. {
  33. get
  34. {
  35. Boolean rtnflag = false;
  36. #if DEBUG
  37. rtnflag = DesignTimeHelper.InDesignMode(this);
  38. #endif
  39. return rtnflag;
  40. }
  41. }
  42. #endregion 属性
  43. public I2CAddressPage()
  44. {
  45. InitializeComponent();
  46. Controls.Cast<Control>().ToList().ForEach(x => StyleManager.Instance.RegisterControl(x));
  47. StyleManager.Instance.RegisterControl(this);
  48. }
  49. protected override void OnLoad(EventArgs e)
  50. {
  51. base.OnLoad(e);
  52. InitView();
  53. }
  54. private void InitView()
  55. {
  56. Int32 rowcount = 1;
  57. this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
  58. {
  59. RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
  60. Control control = x.GetValue(this) as Control;
  61. return (attribute, control);
  62. }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x =>
  63. {
  64. Control lable = x.First(x => x is UestcLabel);
  65. Control control = x.First(x => !(x is UestcLabel));
  66. lable.Left = Width / _ColnumCount * x.Key.Colnum;
  67. lable.Width = control.Width;
  68. lable.Height = lable.Font.Height;
  69. lable.Top = 10 + _RowHeight * x.Key.Row;
  70. control.Left = lable.Left;
  71. control.Top = lable.Bottom + 2;
  72. lable.Width = 120;
  73. control.Width = 120;
  74. rowcount = Math.Max(rowcount, x.Key.Row + 1);
  75. });
  76. Height = rowcount * _RowHeight;
  77. InitAddressBitWidth();
  78. SetAddressData();
  79. InitDataDirection();
  80. InitRelation();
  81. }
  82. private void InitAddressBitWidth()
  83. {
  84. this.CbxAddressBitWidth.ButtonItems = Enum.GetValues<ComModel.ProtocolI2C.AddrBitWidth>().Select(x=>
  85. {
  86. return new RadioButtonItem()
  87. {
  88. Text = x switch
  89. {
  90. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7 => "7bit",
  91. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => "10bit",
  92. _ => "7bit",
  93. },
  94. Tag = x,
  95. };
  96. }).ToArray();
  97. this.CbxAddressBitWidth.IndexChanged += (_, _) =>
  98. {
  99. Presenter.BitWidth =(ComModel.ProtocolI2C.AddrBitWidth)this.CbxAddressBitWidth.ButtonItems[this.CbxAddressBitWidth.ChoosedButtonIndex].Tag;
  100. };
  101. this.CbxAddressBitWidth.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolI2C.AddrBitWidth>().ToList().FindIndex(x => x == Presenter.BitWidth);
  102. }
  103. private void InitDataDirection()
  104. {
  105. this.CbxDataDirection.ButtonItems = Enum.GetValues<ComModel.ProtocolI2C.DataDirection>().Select(x =>
  106. {
  107. return new RadioButtonItem()
  108. {
  109. Text = x switch
  110. {
  111. ComModel.ProtocolI2C.DataDirection.Read => "Read",
  112. ComModel.ProtocolI2C.DataDirection.Write => "Write",
  113. ComModel.ProtocolI2C.DataDirection.Both=>"Both",
  114. _ => "Read",
  115. },
  116. Tag = x,
  117. };
  118. }).ToArray();
  119. this.CbxDataDirection.IndexChanged += (_, _) =>
  120. {
  121. Presenter.Direction = (ComModel.ProtocolI2C.DataDirection)this.CbxDataDirection.ButtonItems[this.CbxDataDirection.ChoosedButtonIndex].Tag;
  122. };
  123. this.CbxDataDirection.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolI2C.DataDirection>().ToList().FindIndex(x => x == Presenter.Direction);
  124. }
  125. private void InitRelation()
  126. {
  127. this.CbxRelation.DataSource = Presenter.Relation.GetEnumList();
  128. this.CbxRelation.ValueMember = "Value";
  129. this.CbxRelation.DisplayMember = "Key";
  130. this.CbxRelation.SelectedValueChanged += (_, _) =>
  131. {
  132. Presenter.Relation = (ComModel.PulseCondition) this.CbxRelation.SelectedValue;
  133. };
  134. this.CbxRelation.SelectedValue = Presenter.Relation;
  135. }
  136. public void UpdateView(String propertyName)
  137. {
  138. if(String.IsNullOrEmpty(propertyName))
  139. {
  140. UpdateView();
  141. return;
  142. }
  143. this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
  144. {
  145. UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
  146. return attribute != null && attribute.PropertyName == propertyName;
  147. }).FirstOrDefault()?.Invoke(this, null);
  148. }
  149. private void UpdateView()
  150. {
  151. this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
  152. this.Refresh();
  153. }
  154. #region UpdateFunction
  155. [UpdateProperty(nameof(I2CTrigSerialPrsnt.BitWidth))]
  156. private void SetAddressBitWidth()
  157. {
  158. this.CbxAddressBitWidth.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolI2C.AddrBitWidth>().ToList().FindIndex(x => x == Presenter.BitWidth);
  159. SetAddressData();
  160. }
  161. [UpdateProperty(nameof(I2CTrigSerialPrsnt.AddressData))]
  162. private void SetAddressData()
  163. {
  164. Int32 bitcount = 1;
  165. switch(Presenter.BitWidth)
  166. {
  167. case ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10:
  168. this.BtnAddressData.Text = "0x"+ string.Join("", Presenter.AddressData.Select(x => Convert.ToString(x, 16).ToUpper().PadLeft(2, '0')));
  169. bitcount = 2;
  170. break;
  171. case ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7:
  172. this.BtnAddressData.Text ="0x" +Convert.ToString(Presenter.AddressData[0], 16).ToUpper().PadLeft(2, '0');
  173. bitcount = 1;
  174. break;
  175. }
  176. if(_FloatForm!=null && !_FloatForm.IsDisposed)
  177. {
  178. HexNumberKeyboard numberKeyboard = (HexNumberKeyboard)_FloatForm.Controls.Cast<Control>().FirstOrDefault(x => x is HexNumberKeyboard);
  179. if (numberKeyboard == null)
  180. return;
  181. numberKeyboard.MaxValue = Presenter.BitWidth switch
  182. {
  183. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7 => (long)Math.Pow(2, 7) - 1,
  184. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => (long)Math.Pow(2, 10) - 1,
  185. _ => (long)Math.Pow(2, 7) - 1,
  186. };
  187. }
  188. this.BtnAddressData.Click += (_, _) =>
  189. {
  190. Boolean lastflag = true;
  191. if (this.FindForm() is FloatForm form)
  192. {
  193. lastflag = form.CanClose;
  194. form.CanClose = false;
  195. }
  196. if (_FloatForm != null && !_FloatForm.IsDisposed)
  197. _FloatForm?.Close();
  198. _FloatForm = new FloatForm();
  199. _FloatForm.BackColor = BackColor;
  200. _FloatForm.ForeColor = ForeColor;
  201. _FloatForm.Title = "设置地址";
  202. _FloatForm.Width = 380;
  203. _FloatForm.Height = 500 + _FloatForm.HeadHeight;
  204. HexNumberKeyboard numberKeyboard = new HexNumberKeyboard();
  205. numberKeyboard.Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
  206. numberKeyboard.Top = _FloatForm.HeadHeight;
  207. numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight;
  208. numberKeyboard.Width = _FloatForm.Width;
  209. numberKeyboard.ForeColor = ForeColor;
  210. numberKeyboard.BackColor = BackColor;
  211. numberKeyboard.MaxValue = Presenter.BitWidth switch
  212. {
  213. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7=>(Int64)Math.Pow(2,7)-1,
  214. ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => (Int64)Math.Pow(2,10)-1,
  215. _ => (Int64)Math.Pow(2, 7)-1,
  216. };
  217. numberKeyboard.MinValue = 0;
  218. numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Hex;
  219. numberKeyboard.Value = Presenter.AddressData[1] << 8 + Presenter.AddressData[0];
  220. numberKeyboard.OkClick += (sender, args) =>
  221. {
  222. Byte[] tempbytes = BitConverter.GetBytes((Int16)args.Data);
  223. if (bitcount == 1)
  224. tempbytes[1] = 0;
  225. Presenter.AddressData = tempbytes;
  226. _FloatForm.Close();
  227. };
  228. numberKeyboard.CancelClick += (_, _) => _FloatForm.Close();
  229. _FloatForm.Controls.Add(numberKeyboard);
  230. _FloatForm.FormClosing += (_, _) =>
  231. {
  232. if (this.FindForm() is FloatForm form)
  233. form.CanClose = lastflag;
  234. };
  235. EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
  236. {
  237. Current = _FloatForm,
  238. Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
  239. });
  240. };
  241. }
  242. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Direction))]
  243. private void SetDirection()
  244. {
  245. this.CbxDataDirection.ChoosedButtonIndex = Enum.GetValues<ComModel.ProtocolI2C.DataDirection>().ToList().FindIndex(x => x == Presenter.Direction);
  246. }
  247. [UpdateProperty(nameof(I2CTrigSerialPrsnt.Relation))]
  248. private void SetRelation()
  249. {
  250. this.CbxRelation.SelectedValue = Presenter.Relation;
  251. }
  252. #endregion UpdateFunction
  253. }
  254. }