FPGAArrayWriteProperty.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public override bool IsArray => true;
  16. private void SetValue(T[] values)
  17. {
  18. lock (lockObject)
  19. {
  20. if (values.Length != Count || Count ==0) return;
  21. T[] oldvalue = GetValues();
  22. bool changed = Enumerable.Range(0, (int)Count).Any(x => !object.Equals(values[x], oldvalue[x]));
  23. if (!changed) return;
  24. switch (values[0])
  25. {
  26. case (bool):
  27. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayBool>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref values[0]), Count));
  28. break;
  29. case (byte):
  30. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU8>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref values[0]), Count));
  31. break;
  32. case (sbyte):
  33. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI8>()(_Session.Session, Register, ref Unsafe.As<T, sbyte>(ref values[0]), Count));
  34. break;
  35. case (short):
  36. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI16>()(_Session.Session, Register, ref Unsafe.As<T, short>(ref values[0]), Count));
  37. break;
  38. case (ushort):
  39. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU16>()(_Session.Session, Register, ref Unsafe.As<T, ushort>(ref values[0]), Count));
  40. break;
  41. case (int):
  42. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI32>()(_Session.Session, Register, ref Unsafe.As<T, int>(ref values[0]), Count));
  43. break;
  44. case (uint):
  45. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU32>()(_Session.Session, Register, ref Unsafe.As<T, uint>(ref values[0]), Count));
  46. break;
  47. case (long):
  48. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI64>()(_Session.Session, Register, ref Unsafe.As<T, long>(ref values[0]), Count));
  49. break;
  50. case (ulong):
  51. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU64>()(_Session.Session, Register, ref Unsafe.As<T, ulong>(ref values[0]), Count));
  52. break;
  53. case (float):
  54. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArraySgl>()(_Session.Session, Register, ref Unsafe.As<T, float>(ref values[0]), Count));
  55. break;
  56. case (double):
  57. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayDbl>()(_Session.Session, Register, ref Unsafe.As<T, double>(ref values[0]), Count));
  58. break;
  59. }
  60. }
  61. }
  62. private T[] GetValues()
  63. {
  64. lock (lockObject)
  65. {
  66. if (Count == 0) return tempvalue;
  67. switch (tempvalue[0])
  68. {
  69. case (bool):
  70. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayBool>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  71. break;
  72. case (byte):
  73. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>()(_Session.Session, Register, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  74. break;
  75. case (sbyte):
  76. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI8>()(_Session.Session, Register, ref Unsafe.As<T, sbyte>(ref tempvalue[0]), Count));
  77. break;
  78. case short:
  79. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI16>()(_Session.Session, Register, ref Unsafe.As<T, short>(ref tempvalue[0]), Count));
  80. break;
  81. case (ushort):
  82. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU16>()(_Session.Session, Register, ref Unsafe.As<T, ushort>(ref tempvalue[0]), Count));
  83. break;
  84. case (int):
  85. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI32>()(_Session.Session, Register, ref Unsafe.As<T, int>(ref tempvalue[0]), Count));
  86. break;
  87. case (uint):
  88. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU32>()(_Session.Session, Register, ref Unsafe.As<T, uint>(ref tempvalue[0]), Count));
  89. break;
  90. case (long):
  91. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI64>()(_Session.Session, Register, ref Unsafe.As<T, long>(ref tempvalue[0]), Count));
  92. break;
  93. case (ulong):
  94. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU64>()(_Session.Session, Register, ref Unsafe.As<T, ulong>(ref tempvalue[0]), Count));
  95. break;
  96. case (float):
  97. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArraySgl>()(_Session.Session, Register, ref Unsafe.As<T, float>(ref tempvalue[0]), Count));
  98. break;
  99. case (double):
  100. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayDbl>()(_Session.Session, Register, ref Unsafe.As<T, double>(ref tempvalue[0]), Count));
  101. break;
  102. }
  103. return tempvalue;
  104. }
  105. }
  106. public uint Count { get; }
  107. public T[] Value { get => GetValues(); set => SetValue(value); }
  108. }
  109. }