123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 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<Interop.NiFpgaDll_ReadArrayU8>();
- }
- private double[] GetData()
- {
- _Session.CheckResult(Read(_Session.Session, Indicator, ref Unsafe.As<ulong, byte>(ref tempbuffer[0]), (uint)tempbuffer.Length * 8));
- 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(); }
- }
- }
|