using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Uestc.Auto6.Dso.Core; using Uestc.Auto6.Dso.Core.Decode; using Uni_Trend.MSO7000X.Common.Helper; using Uni_Trend.MSO7000X.UserControls; using Uni_Trend.MSO7000X.UserControls.Style; using Uni_Trend.MSO7000X.Language; namespace Uestc.Auto6.Dso.Protocol.I2C { public partial class I2CAddressPage : UserControl, ITriggerSerialView { #region 属性 private FloatForm _FloatForm; private Int32 _RowHeight = 56; private Int32 _ColnumCount = 2; public I2CTrigSerialPrsnt Presenter { get; set; } ITriggerPrsnt IView.Presenter { get => Presenter; set => Presenter = (I2CTrigSerialPrsnt)value; } protected new Boolean DesignMode { get { Boolean rtnflag = false; #if DEBUG rtnflag = DesignTimeHelper.InDesignMode(this); #endif return rtnflag; } } #endregion 属性 public I2CAddressPage() { InitializeComponent(); Controls.Cast().ToList().ForEach(x => StyleManager.Instance.RegisterControl(x)); StyleManager.Instance.RegisterControl(this); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); InitView(); } private void InitView() { Int32 rowcount = 1; this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute() != null && x.GetValue(this) is Control).Select(x => { RowColAttribute attribute = x.GetCustomAttribute(); Control control = x.GetValue(this) as Control; return (attribute, control); }).GroupBy(x => x.attribute, (x => x.control)).ToList().ForEach(x => { Control lable = x.First(x => x is UestcLabel); Control control = x.First(x => !(x is UestcLabel)); lable.Left = Width / _ColnumCount * x.Key.Colnum; lable.Width = control.Width; lable.Height = lable.Font.Height; lable.Top = 10 + _RowHeight * x.Key.Row; control.Left = lable.Left; control.Top = lable.Bottom + 2; lable.Width = 120; control.Width = 120; rowcount = Math.Max(rowcount, x.Key.Row + 1); }); Height = rowcount * _RowHeight; InitAddressBitWidth(); SetAddressData(); InitDataDirection(); InitRelation(); } private void InitAddressBitWidth() { this.CbxAddressBitWidth.ButtonItems = Enum.GetValues().Select(x=> { return new RadioButtonItem() { Text = x switch { ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7 => "7bit", ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => "10bit", _ => "7bit", }, Tag = x, }; }).ToArray(); this.CbxAddressBitWidth.IndexChanged += (_, _) => { Presenter.BitWidth =(ComModel.ProtocolI2C.AddrBitWidth)this.CbxAddressBitWidth.ButtonItems[this.CbxAddressBitWidth.ChoosedButtonIndex].Tag; }; this.CbxAddressBitWidth.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == Presenter.BitWidth); } private void InitDataDirection() { this.CbxDataDirection.ButtonItems = Enum.GetValues().Select(x => { return new RadioButtonItem() { Text = x switch { ComModel.ProtocolI2C.DataDirection.Read => "Read", ComModel.ProtocolI2C.DataDirection.Write => "Write", ComModel.ProtocolI2C.DataDirection.Both=>"Both", _ => "Read", }, Tag = x, }; }).ToArray(); this.CbxDataDirection.IndexChanged += (_, _) => { Presenter.Direction = (ComModel.ProtocolI2C.DataDirection)this.CbxDataDirection.ButtonItems[this.CbxDataDirection.ChoosedButtonIndex].Tag; }; this.CbxDataDirection.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == Presenter.Direction); } private void InitRelation() { this.CbxRelation.DataSource = Presenter.Relation.GetEnumList(); this.CbxRelation.ValueMember = "Value"; this.CbxRelation.DisplayMember = "Key"; this.CbxRelation.SelectedValueChanged += (_, _) => { Presenter.Relation = (ComModel.PulseCondition) this.CbxRelation.SelectedValue; }; this.CbxRelation.SelectedValue = Presenter.Relation; } public void UpdateView(String propertyName) { if(String.IsNullOrEmpty(propertyName)) { UpdateView(); return; } this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => { UpdatePropertyAttribute attribute = x.GetCustomAttribute(); return attribute != null && attribute.PropertyName == propertyName; }).FirstOrDefault()?.Invoke(this, null); } private void UpdateView() { this.GetType().GetMethods().Where(x => x.GetCustomAttribute() != null).ToList().ForEach(x => x?.Invoke(this, null)); this.Refresh(); } #region UpdateFunction [UpdateProperty(nameof(I2CTrigSerialPrsnt.BitWidth))] private void SetAddressBitWidth() { this.CbxAddressBitWidth.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == Presenter.BitWidth); SetAddressData(); } [UpdateProperty(nameof(I2CTrigSerialPrsnt.AddressData))] private void SetAddressData() { Int32 bitcount = 1; switch(Presenter.BitWidth) { case ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10: this.BtnAddressData.Text = "0x"+ string.Join("", Presenter.AddressData.Select(x => Convert.ToString(x, 16).ToUpper().PadLeft(2, '0'))); bitcount = 2; break; case ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7: this.BtnAddressData.Text ="0x" +Convert.ToString(Presenter.AddressData[0], 16).ToUpper().PadLeft(2, '0'); bitcount = 1; break; } if(_FloatForm!=null && !_FloatForm.IsDisposed) { HexNumberKeyboard numberKeyboard = (HexNumberKeyboard)_FloatForm.Controls.Cast().FirstOrDefault(x => x is HexNumberKeyboard); if (numberKeyboard == null) return; numberKeyboard.MaxValue = Presenter.BitWidth switch { ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7 => (long)Math.Pow(2, 7) - 1, ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => (long)Math.Pow(2, 10) - 1, _ => (long)Math.Pow(2, 7) - 1, }; } this.BtnAddressData.Click += (_, _) => { Boolean lastflag = true; if (this.FindForm() is FloatForm form) { lastflag = form.CanClose; form.CanClose = false; } if (_FloatForm != null && !_FloatForm.IsDisposed) _FloatForm?.Close(); _FloatForm = new FloatForm(); _FloatForm.BackColor = BackColor; _FloatForm.ForeColor = ForeColor; _FloatForm.Title = "设置地址"; _FloatForm.Width = 380; _FloatForm.Height = 500 + _FloatForm.HeadHeight; HexNumberKeyboard numberKeyboard = new HexNumberKeyboard(); numberKeyboard.Controls.Cast().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x)); numberKeyboard.Top = _FloatForm.HeadHeight; numberKeyboard.Height = _FloatForm.Height - _FloatForm.HeadHeight; numberKeyboard.Width = _FloatForm.Width; numberKeyboard.ForeColor = ForeColor; numberKeyboard.BackColor = BackColor; numberKeyboard.MaxValue = Presenter.BitWidth switch { ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_7=>(Int64)Math.Pow(2,7)-1, ComModel.ProtocolI2C.AddrBitWidth.AddrBitWidth_10 => (Int64)Math.Pow(2,10)-1, _ => (Int64)Math.Pow(2, 7)-1, }; numberKeyboard.MinValue = 0; numberKeyboard.ValueType = HexNumberKeyboard.HexValueType.Hex; numberKeyboard.Value = Presenter.AddressData[1] << 8 + Presenter.AddressData[0]; numberKeyboard.OkClick += (sender, args) => { Byte[] tempbytes = BitConverter.GetBytes((Int16)args.Data); if (bitcount == 1) tempbytes[1] = 0; Presenter.AddressData = tempbytes; _FloatForm.Close(); }; numberKeyboard.CancelClick += (_, _) => _FloatForm.Close(); _FloatForm.Controls.Add(numberKeyboard); _FloatForm.FormClosing += (_, _) => { if (this.FindForm() is FloatForm form) form.CanClose = lastflag; }; EventBus.EventBroker.Instance.GetEvent().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs() { Current = _FloatForm, Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm, }); }; } [UpdateProperty(nameof(I2CTrigSerialPrsnt.Direction))] private void SetDirection() { this.CbxDataDirection.ChoosedButtonIndex = Enum.GetValues().ToList().FindIndex(x => x == Presenter.Direction); } [UpdateProperty(nameof(I2CTrigSerialPrsnt.Relation))] private void SetRelation() { this.CbxRelation.SelectedValue = Presenter.Relation; } #endregion UpdateFunction } }