FPGAArrayWriteProperty.cs 5.9 KB

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