using FxpConvert.Common; using System.Runtime.CompilerServices; namespace NIFPGA { public sealed class FPGAArrayFXPReadProperty : FPGABaseProperty { Interop.NiFpgaDll_ReadArrayU8 Read; private NiFpga_FxpTypeInfo _TypeInfo; private IFxpConvert _Convert; private ulong[] tempbuffer = new ulong[0]; private ulong[] tempvalue = new ulong[0]; private double[] doubles = new double[0]; internal FPGAArrayFXPReadProperty(FPGASession session, uint indicator, uint count, NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, indicator, true) { _Convert = convert; Count = count; tempvalue = new ulong[count]; doubles = new double[count]; _TypeInfo = typeInfo; tempbuffer = new ulong[(int)Math.Ceiling(typeInfo.wordLength * count / (8*8.0))]; Read = _Session.GetDelegate(); } private double[] GetData() { _Session.CheckResult(Read(_Session.Session, Indicator, ref Unsafe.As(ref tempbuffer[0]), (uint)tempbuffer.Length * 8)); int tempdatalen = Unsafe.SizeOf() * 8; ulong mask = (1ul << _TypeInfo.wordLength) - 1; for (int i = 0; i < Count; i++) { int index = i * _TypeInfo.wordLength / tempdatalen; int bitindex = tempdatalen - (i * _TypeInfo.wordLength % tempdatalen); if (bitindex >= _TypeInfo.wordLength) { tempvalue[i] = (tempbuffer[index] >> (bitindex - _TypeInfo.wordLength)) & mask; } else { tempvalue[i] = (((tempbuffer[index] & ((1ul << bitindex) - 1)) << (_TypeInfo.wordLength - bitindex)) | (tempbuffer[index + 1] >> (tempdatalen - (_TypeInfo.wordLength - bitindex)))); } } _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count); return doubles; } public uint Count { get; } public double[] Value { get => GetData(); } } }