FPGAArrayWriteProperty.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using NIFPGA.lvbitx;
  2. using System.Runtime.CompilerServices;
  3. namespace NIFPGA
  4. {
  5. public sealed class FPGAArrayWriteProperty<T> : FPGABaseProperty
  6. where T : unmanaged
  7. {
  8. private T[] tempvalue = new T[0];
  9. private object lockObject = new object();
  10. internal FPGAArrayWriteProperty(FPGASession session, Register indicator) : base(session, indicator)
  11. {
  12. Count = indicator.Size;
  13. tempvalue = new T[Count];
  14. }
  15. private void SetValue(T[] values)
  16. {
  17. lock (lockObject)
  18. {
  19. if (values.Length != Count || Count ==0) return;
  20. T[] oldvalue = GetValues();
  21. bool changed = Enumerable.Range(0, (int)Count).Any(x => !object.Equals(values[x], oldvalue[x]));
  22. if (!changed) return;
  23. switch (values[0])
  24. {
  25. case (bool):
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayBool>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref values[0]), Count));
  27. break;
  28. case (byte):
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU8>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref values[0]), Count));
  30. break;
  31. case (sbyte):
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI8>()(_Session.Session, Register, ref Unsafe.As<T, sbyte>(ref values[0]), Count));
  33. break;
  34. case (short):
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI16>()(_Session.Session, Register, ref Unsafe.As<T, short>(ref values[0]), Count));
  36. break;
  37. case (ushort):
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU16>()(_Session.Session, Register, ref Unsafe.As<T, ushort>(ref values[0]), Count));
  39. break;
  40. case (int):
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI32>()(_Session.Session, Register, ref Unsafe.As<T, int>(ref values[0]), Count));
  42. break;
  43. case (uint):
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU32>()(_Session.Session, Register, ref Unsafe.As<T, uint>(ref values[0]), Count));
  45. break;
  46. case (long):
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI64>()(_Session.Session, Register, ref Unsafe.As<T, long>(ref values[0]), Count));
  48. break;
  49. case (ulong):
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU64>()(_Session.Session, Register, ref Unsafe.As<T, ulong>(ref values[0]), Count));
  51. break;
  52. case (float):
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArraySgl>()(_Session.Session, Register, ref Unsafe.As<T, float>(ref values[0]), Count));
  54. break;
  55. case (double):
  56. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayDbl>()(_Session.Session, Register, ref Unsafe.As<T, double>(ref values[0]), Count));
  57. break;
  58. }
  59. }
  60. }
  61. private T[] GetValues()
  62. {
  63. lock (lockObject)
  64. {
  65. if (Count == 0) return tempvalue;
  66. switch (tempvalue[0])
  67. {
  68. case (bool):
  69. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayBool>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  70. break;
  71. case (byte):
  72. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  73. break;
  74. case (sbyte):
  75. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI8>()(_Session.Session, Register, ref Unsafe.As<T, sbyte>(ref tempvalue[0]), Count));
  76. break;
  77. case short:
  78. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI16>()(_Session.Session, Register, ref Unsafe.As<T, short>(ref tempvalue[0]), Count));
  79. break;
  80. case (ushort):
  81. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU16>()(_Session.Session, Register, ref Unsafe.As<T, ushort>(ref tempvalue[0]), Count));
  82. break;
  83. case (int):
  84. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI32>()(_Session.Session, Register, ref Unsafe.As<T, int>(ref tempvalue[0]), Count));
  85. break;
  86. case (uint):
  87. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU32>()(_Session.Session, Register, ref Unsafe.As<T, uint>(ref tempvalue[0]), Count));
  88. break;
  89. case (long):
  90. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI64>()(_Session.Session, Register, ref Unsafe.As<T, long>(ref tempvalue[0]), Count));
  91. break;
  92. case (ulong):
  93. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU64>()(_Session.Session, Register, ref Unsafe.As<T, ulong>(ref tempvalue[0]), Count));
  94. break;
  95. case (float):
  96. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArraySgl>()(_Session.Session, Register, ref Unsafe.As<T, float>(ref tempvalue[0]), Count));
  97. break;
  98. case (double):
  99. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayDbl>()(_Session.Session, Register, ref Unsafe.As<T, double>(ref tempvalue[0]), Count));
  100. break;
  101. }
  102. return tempvalue;
  103. }
  104. }
  105. public uint Count { get; }
  106. public T[] Values { get => GetValues(); set => SetValue(value); }
  107. }
  108. }