1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using FxpConvert.Common;
- using System.Buffers.Binary;
- using System.Runtime.CompilerServices;
- namespace NIFPGA
- {
- public sealed class FPGAArrayFXPReadProperty : FPGABaseProperty
- {
- private object _Locker = new object();
- Interop.NiFpgaDll_ReadArrayU8 Read;
- private NiFpga_FxpTypeInfo _TypeInfo;
- private IFxpConvert _Convert;
- private uint bytescount = 0;
- 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))];
- bytescount = (uint)Math.Ceiling(typeInfo.wordLength * count / 8.0);
- Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>();
- }
- private double[] GetData()
- {
- lock (_Locker)
- {
- _Session.CheckResult(Read(_Session.Session, Indicator, ref Unsafe.As<ulong, byte>(ref tempbuffer[0]), bytescount));
- for (int i = 0; i < tempbuffer.Length; i++)
- {
- tempbuffer[i] = BinaryPrimitives.ReverseEndianness(tempbuffer[i]);
- }
- int tempdatalen = Unsafe.SizeOf<ulong>() * 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(); }
- }
- }
|