123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using System;
- using System.Collections;
- using System.Linq;
- using System.Reflection;
- using System.Windows.Forms;
- using Uestc.Auto6.Dso.Core;
- using Uestc.Auto6.Dso.Core.Decode;
- using Uestc.Auto6.Dso.ComModel;
- using Uni_Trend.MSO7000X.Language;
- using Uni_Trend.MSO7000X.Common.Helper;
- using Uni_Trend.MSO7000X.UserControls;
- namespace Uestc.Auto6.Dso.Protocol.I2C
- {
- public partial class I2CSetControl : UserControl,IProtocolView
- {
- #region 属性定义
- protected new Boolean DesignMode
- {
- get
- {
- Boolean rtnflag = false;
- #if DEBUG
- rtnflag = DesignTimeHelper.InDesignMode(this);
- #endif
- return rtnflag;
- }
- }
- private I2CDecodePrsnt DestinDecodePrsnt
- {
- get => destinDecodePrsnt;
- set
- {
- if (destinDecodePrsnt!=value)
- {
- destinDecodePrsnt = value;
- ///当有新的同步目标时,应先将目标中参数同步到本地<see cref="_I2CDecodePrsnt"/>属性中
- if (_I2CDecodePrsnt != null && value != null)
- {
- typeof(I2CDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(_I2CDecodePrsnt, x.GetValue(value));
- });
- }
- }
- }
- }
- private I2CDecodePrsnt destinDecodePrsnt;
- private I2CDecodePrsnt _I2CDecodePrsnt => Presenter as I2CDecodePrsnt;
- public IProtocolPrsnt Presenter { get; set; }
- private FloatForm _FloatForm;
- private Int32 _RowHeight = 56;
- private Int32 _ColnumCount = 2;
- #endregion 属性定义
- public I2CSetControl()
- {
- InitializeComponent();
- InitView();
- }
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- Init();
- }
- private void Init()
- {
- GetDestinPrsnt();
- InitSCLK();
- InitSCLKThreshold();
- InitSDA();
- InitSDAThreshold();
- }
- /// <summary>
- /// 获取需要同步的Prsnt
- /// 当<see cref="_I2CDecodePrsnt"/>为<see cref="null"/>时表示不需要与其他Prsnt进行同步
- /// </summary>
- private void GetDestinPrsnt()
- {
- if (_I2CDecodePrsnt == null)
- return;
- if (_I2CDecodePrsnt.IsTrigger)
- DestinDecodePrsnt = Presenter.GetChannlesDecodePrsnt().Where(x => x is I2CDecodePrsnt decodePrsnt && decodePrsnt.SDA == _I2CDecodePrsnt.SDA).Cast<I2CDecodePrsnt>().OrderBy(x=>x.SDA).FirstOrDefault();
- else
- {
- if (Presenter.GetTriggerDecodePrsnt() is I2CDecodePrsnt i2cdecodePrsnt && i2cdecodePrsnt.SDA == _I2CDecodePrsnt.SDA)
- DestinDecodePrsnt = i2cdecodePrsnt;
- else
- DestinDecodePrsnt = null;
- }
- }
- /// <summary>
- /// 同步两个Prsnt
- /// </summary>
- /// <param name="propertyName">属性名称</param>
- private void DecodeSynchronization(String propertyName)
- {
- if (DestinDecodePrsnt == null)
- return;
- ///由于使用了<see cref="II2CDecodePrsnt.SDA"/>进行判断具体同步的目标
- ///因此<see cref="II2CDecodePrsnt.SDA"/>参数不进行同步
- if (propertyName == nameof(_I2CDecodePrsnt.SDA))
- return;
- if (String.IsNullOrEmpty(propertyName))
- {
- typeof(I2CDecodePrsnt).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x=>x.CanWrite).ToList().ForEach(x =>
- {
- x.SetValue(DestinDecodePrsnt, x.GetValue(_I2CDecodePrsnt));
- });
- }
- else
- {
- PropertyInfo propertyInfo = typeof(I2CDecodePrsnt).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (propertyInfo != null)
- propertyInfo.SetValue(DestinDecodePrsnt, propertyInfo.GetValue(_I2CDecodePrsnt));
- }
- }
- private void InitSDA()
- {
- this.CbxSDA.DataSource = ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.GetDescription(), x, true)).ToList();
- this.CbxSDA.DisplayMember = "Key";
- this.CbxSDA.ValueMember = "Value";
- this.CbxSDA.SelectedValueChanged += (_, _) =>
- {
- _I2CDecodePrsnt.SDA = (ChannelId)this.CbxSDA.SelectedValue;
- };
- this.CbxSDA.SelectedValue = _I2CDecodePrsnt.SDA;
- }
- private void InitSDAThreshold()
- {
- this.BtnSDAThreshold.Text = SIHelper.ValueChangeToSI(_I2CDecodePrsnt.SDAThreshold, 2, "V");
- this.BtnSDAThreshold.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;
- NumberKeyboard numberKeyboard = new NumberKeyboard();
- numberKeyboard.Controls.Cast<Control>().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 = 12;
- numberKeyboard.MinValue = -12;
- numberKeyboard.DecimalNumber = 2;
- numberKeyboard.DefaultValue = _I2CDecodePrsnt.SDAThreshold;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- _I2CDecodePrsnt.SDAThreshold = (Double)args.Data;
- UpdateSDAThreshold();
- _FloatForm.Close();
- };
- numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
- _FloatForm.Controls.Add(numberKeyboard);
- _FloatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
- {
- Current = _FloatForm,
- Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
- });
- };
- }
- private void InitSCLKThreshold()
- {
- this.BtnSCLKThreshold.Text = SIHelper.ValueChangeToSI(_I2CDecodePrsnt.SCLKThreshold, 2, "V");
- this.BtnSCLKThreshold.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;
- NumberKeyboard numberKeyboard = new NumberKeyboard();
- numberKeyboard.Controls.Cast<Control>().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 = 12;
- numberKeyboard.MinValue = -12;
- numberKeyboard.DecimalNumber = 2;
- numberKeyboard.DefaultValue = _I2CDecodePrsnt.SCLKThreshold;
- numberKeyboard.OkClickEvent += (sender, args) =>
- {
- _I2CDecodePrsnt.SCLKThreshold = (Double)args.Data;
- UpdateSCLKThreshold();
- _FloatForm.Close();
- };
- numberKeyboard.CancelEvent += (_, _) => _FloatForm.Close();
- _FloatForm.Controls.Add(numberKeyboard);
- _FloatForm.FormClosing += (_, _) =>
- {
- if (this.FindForm() is FloatForm form)
- form.CanClose = lastflag;
- };
- EventBus.EventBroker.Instance.GetEvent<Uni_Trend.MSO7000X.Common.Structs.FormEventArgs>().Publish(this, new Uni_Trend.MSO7000X.Common.Structs.FormEventArgs()
- {
- Current = _FloatForm,
- Type = Uni_Trend.MSO7000X.Common.Structs.FormType.InfoForm,
- });
- };
- }
- private void InitSCLK()
- {
- this.CbxSCLK.DataSource = ChannelIdExt.GetAnalogs().Select(x => new ComboBoxData<ChannelId>(x.GetDescription(), x, x!=_I2CDecodePrsnt.SDA)).ToList();
- this.CbxSCLK.DisplayMember = "Key";
- this.CbxSCLK.ValueMember = "Value";
- this.CbxSCLK.SelectedValueChanged += (_, _) =>
- {
- _I2CDecodePrsnt.SCLK = (ChannelId)this.CbxSCLK.SelectedValue;
- };
- this.CbxSCLK.SelectedValue = _I2CDecodePrsnt.SCLK;
- }
- private void InitView()
- {
- Int32 rowcount = 1;
- Controls.Cast<Control>().ToList().ForEach(x => Uni_Trend.MSO7000X.UserControls.Style.StyleManager.Instance.RegisterControl(x));
- this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute<RowColAttribute>() != null && x.GetValue(this) is Control).Select(x =>
- {
- RowColAttribute attribute = x.GetCustomAttribute<RowColAttribute>();
- 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;
- }
- /// <summary>
- /// <see cref="II2CDecodePrsnt.SCLK"/>与<see cref="II2CDecodePrsnt.SDA"/>中值不能相同
- /// </summary>
- private void ChangeSDA()
- {
- if (_I2CDecodePrsnt.SCLK == _I2CDecodePrsnt.SDA)
- {
- if (_I2CDecodePrsnt.SDA == ChannelIdExt.GetAnalogs().Last())
- _I2CDecodePrsnt.SCLK = ChannelIdExt.GetAnalogs().Last() - 1;
- else if (_I2CDecodePrsnt.SDA == ChannelIdExt.GetAnalogs().First())
- _I2CDecodePrsnt.SCLK = ChannelIdExt.GetAnalogs().First() + 1;
- else
- _I2CDecodePrsnt.SCLK++;
- }
- if (this.CbxSCLK.DataSource is IList list)
- {
- foreach (dynamic val in list)
- {
- val.Visible = val.Value != _I2CDecodePrsnt.SDA;
- }
- }
- }
-
- private class ComboBoxData<T>
- {
- public String Key { get; set; }
- public T Value { get; set; }
- public Boolean Visible { get; set; }
- public ComboBoxData(String key, T value, Boolean visible = true)
- {
- Key = key;
- Value = value;
- Visible = visible;
- }
- }
- #region UpdateFunction
- [UpdateProperty(nameof(I2CDecodePrsnt.SDAThreshold))]
- void UpdateSDAThreshold()
- {
- this.BtnSDAThreshold.Text = SIHelper.ValueChangeToSI(_I2CDecodePrsnt.SDAThreshold, 2, "V");
- }
- [UpdateProperty(nameof(I2CDecodePrsnt.SCLKThreshold))]
- void UpdateSCLKThreshold()
- {
- this.BtnSCLKThreshold.Text = SIHelper.ValueChangeToSI(_I2CDecodePrsnt.SCLKThreshold, 2, "V");
- }
- [UpdateProperty(nameof(I2CDecodePrsnt.SCLK))]
- void UpdateSCLK()
- {
- this.CbxSCLK.SelectedValue = _I2CDecodePrsnt.SCLK;
- }
- [UpdateProperty(nameof(I2CDecodePrsnt.SDA))]
- void UpdateSDA()
- {
- this.CbxSDA.SelectedValue = _I2CDecodePrsnt.SDA;
- GetDestinPrsnt();
- ChangeSDA();
- }
- #endregion UpdateFunction
- public void UpdateView(String propertyName)
- {
- DecodeSynchronization(propertyName);
- if (String.IsNullOrEmpty(propertyName))
- {
- UpdateView();
- return;
- }
- this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Where(x =>
- {
- UpdatePropertyAttribute attribute = x.GetCustomAttribute<UpdatePropertyAttribute>();
- return attribute != null && attribute.PropertyName == propertyName;
- }).FirstOrDefault()?.Invoke(this, null);
- }
- private void UpdateView()
- {
- this.GetType().GetMethods().Where(x => x.GetCustomAttribute<UpdatePropertyAttribute>() != null).ToList().ForEach(x => x?.Invoke(this, null));
- this.Refresh();
- }
-
- }
- [AttributeUsage(AttributeTargets.Field)]
- internal class RowColAttribute : Attribute
- {
- public RowColAttribute(Int32 row = 0, Int32 col = 0)
- {
- Row = row;
- Colnum = col;
- }
- public Int32 Row { get; set; }
- public Int32 Colnum { get; set; }
- public override Boolean Equals(Object obj)
- {
- if (obj == null)
- return false;
- if (!(obj is RowColAttribute attribute))
- return false;
- return attribute.Colnum == Colnum && attribute.Row == Row;
- }
- public override Int32 GetHashCode()
- {
- Int32 hashCode = Row.GetHashCode();
- if (Row.GetHashCode() != Colnum.GetHashCode())
- hashCode ^= Colnum.GetHashCode();
- return hashCode;
- }
- }
- [AttributeUsage(AttributeTargets.Method)]
- internal class UpdatePropertyAttribute : Attribute
- {
- public UpdatePropertyAttribute(string propertyName) => this.PropertyName = propertyName;
- public String PropertyName { get; set; }
- }
- }
|