using System; using System.Collections.Generic; using System.Diagnostics.Metrics; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using FxpConvert.Common; namespace NIFPGA { public sealed class FPGAProperty : FPGABaseProperty where T : unmanaged { internal FPGAProperty(FPGASession session, uint indicator,bool isIndicator = true) : base(session, indicator,isIndicator) { } private void SetValue(T value) { switch(value) { case Boolean boolvalue: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Convert.ToByte(boolvalue))); break; case Byte: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case SByte: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case Int16: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case UInt16: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case Int32: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case UInt32: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case Int64: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case UInt64: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case Single: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; case double: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, Unsafe.As(ref value))); break; } } private T GetValue() { T value = default; switch (value) { case Boolean: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator,ref Unsafe.As(ref value))); break; case Byte: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case SByte: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case Int16: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case UInt16: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case Int32: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case UInt32: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case Int64: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case UInt64: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case Single: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; case double: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref value))); break; } return value; } public T Value { get=>GetValue(); set=>SetValue(value); } } public sealed class FPGAFXPProperty: FPGABaseProperty { private NiFpga_FxpTypeInfo _TypeInfo; private IFxpConvert _Convert; internal FPGAFXPProperty(FPGASession session, uint indicator,NiFpga_FxpTypeInfo typeInfo,IFxpConvert convert, bool isIndicator = true) : base(session, indicator,isIndicator) { _TypeInfo = typeInfo; _Convert = convert; } private float GetValue() { ulong value=0; float temp = 0; _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref value)); _Convert.FxpConvertToFloat(ref value, _TypeInfo, ref temp, 1); return temp; } private void SetValue(float value) { ulong temp = 0; _Convert.FloatConvertToDxp(ref value, _TypeInfo, ref temp, 1); _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, temp)); } public float Value { get=>GetValue(); set=>SetValue(value); } } public sealed class FPGAArrayProperty : FPGABaseProperty where T : unmanaged { private T[] tempvalue = new T[0]; internal FPGAArrayProperty(FPGASession session, uint indicator,uint count, bool isIndicator = true) : base(session, indicator,isIndicator) { Count= count; tempvalue= new T[Count]; } private void SetValue(T[] values) { if (values.Length == 0 || values.Length!=Count) return; switch (values[0]) { case (bool): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (byte): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (sbyte): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (short): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (ushort): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (int): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (uint): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (long): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (ulong): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (float): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; case (double): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref values[0]), Count)); break; } } private T[] GetValues() { if (Count == 0) return tempvalue; switch (tempvalue[0]) { case (bool): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]), Count)); break; case (byte): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]), Count)); break; case (sbyte): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]), Count)); break; case short: _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (ushort): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (int): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (uint): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (long): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (ulong): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (float): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]),Count)); break; case (double): _Session.CheckResult(_Session.GetDelegate()(_Session.Session, Indicator, ref Unsafe.As(ref tempvalue[0]), Count)); break; } return tempvalue; } public uint Count { get; } public T[] Values { get=>GetValues(); set=>SetValue(value); } } public sealed class FPGAArrayFXPProperty : FPGABaseProperty { private NiFpga_FxpTypeInfo _TypeInfo; private IFxpConvert _Convert; private ulong[] tempvalue = new ulong[0]; private float[] doubles= new float[0]; internal FPGAArrayFXPProperty(FPGASession session, uint indicator, uint count,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert, bool isIndicator = true) : base(session, indicator,isIndicator) { _Convert = convert; Count = count; tempvalue = new ulong[count]; doubles = new float[count]; _TypeInfo = typeInfo; } private void SetData(float[] values) { if (values.Length != Count) return; _Convert.FloatConvertToDxp(ref values[0], _TypeInfo, ref tempvalue[0], Count); _Session.CheckResult(_Session.GetDelegate()(_Session.Session,Indicator, ref tempvalue[0], Count)); } private float[] GetData() { _Session.CheckResult(_Session.GetDelegate()(_Session.Session,Indicator, ref tempvalue[0], Count)); _Convert.FxpConvertToFloat(ref tempvalue[0], _TypeInfo,ref doubles[0], Count); return doubles; } public uint Count { get; } public float[] Value { get=>GetData(); set=>SetData(value); } } }