FPGAArrayFXPReadProperty.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using FxpConvert.Common;
  2. using System.Runtime.CompilerServices;
  3. namespace NIFPGA
  4. {
  5. public sealed class FPGAArrayFXPReadProperty : FPGABaseProperty
  6. {
  7. Interop.NiFpgaDll_ReadArrayU8 Read;
  8. private NiFpga_FxpTypeInfo _TypeInfo;
  9. private IFxpConvert _Convert;
  10. private ulong[] tempbuffer = new ulong[0];
  11. private ulong[] tempvalue = new ulong[0];
  12. private double[] doubles = new double[0];
  13. internal FPGAArrayFXPReadProperty(FPGASession session, uint indicator, uint count, NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, indicator, true)
  14. {
  15. _Convert = convert;
  16. Count = count;
  17. tempvalue = new ulong[count];
  18. doubles = new double[count];
  19. _TypeInfo = typeInfo;
  20. tempbuffer = new ulong[(int)Math.Ceiling(typeInfo.wordLength * count / (8*8.0))];
  21. Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>();
  22. }
  23. private double[] GetData()
  24. {
  25. _Session.CheckResult(Read(_Session.Session, Indicator, ref Unsafe.As<ulong, byte>(ref tempbuffer[0]), (uint)tempbuffer.Length * 8));
  26. int tempdatalen = Unsafe.SizeOf<ulong>() * 8;
  27. ulong mask = (1ul << _TypeInfo.wordLength) - 1;
  28. for (int i = 0; i < Count; i++)
  29. {
  30. int index = i * _TypeInfo.wordLength / tempdatalen;
  31. int bitindex = tempdatalen - (i * _TypeInfo.wordLength % tempdatalen);
  32. if (bitindex >= _TypeInfo.wordLength)
  33. {
  34. tempvalue[i] = (tempbuffer[index] >> (bitindex - _TypeInfo.wordLength)) & mask;
  35. }
  36. else
  37. {
  38. tempvalue[i] = (((tempbuffer[index] & ((1ul << bitindex) - 1)) << (_TypeInfo.wordLength - bitindex)) | (tempbuffer[index + 1] >> (tempdatalen - (_TypeInfo.wordLength - bitindex))));
  39. }
  40. }
  41. _Convert.FxpConvertToDouble(ref tempvalue[0], _TypeInfo, ref doubles[0], Count);
  42. return doubles;
  43. }
  44. public uint Count { get; }
  45. public double[] Value { get => GetData(); }
  46. }
  47. }