FPGAWriteProperty.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Metrics;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NIFPGA
  9. {
  10. public sealed class FPGAWriteProperty<T> : FPGABaseProperty
  11. where T : unmanaged
  12. {
  13. private object lockObject = new object();
  14. internal FPGAWriteProperty(FPGASession session, uint indicator) : base(session, indicator, false)
  15. {
  16. }
  17. private void SetValue(T value)
  18. {
  19. lock (lockObject)
  20. {
  21. switch (value)
  22. {
  23. case Boolean boolvalue:
  24. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteBool>()(_Session.Session, Indicator, Convert.ToByte(boolvalue)));
  25. break;
  26. case Byte:
  27. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU8>()(_Session.Session, Indicator, Unsafe.As<T, byte>(ref value)));
  28. break;
  29. case SByte:
  30. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI8>()(_Session.Session, Indicator, Unsafe.As<T, sbyte>(ref value)));
  31. break;
  32. case Int16:
  33. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI16>()(_Session.Session, Indicator, Unsafe.As<T, short>(ref value)));
  34. break;
  35. case UInt16:
  36. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU16>()(_Session.Session, Indicator, Unsafe.As<T, ushort>(ref value)));
  37. break;
  38. case Int32:
  39. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI32>()(_Session.Session, Indicator, Unsafe.As<T, int>(ref value)));
  40. break;
  41. case UInt32:
  42. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU32>()(_Session.Session, Indicator, Unsafe.As<T, uint>(ref value)));
  43. break;
  44. case Int64:
  45. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI64>()(_Session.Session, Indicator, Unsafe.As<T, long>(ref value)));
  46. break;
  47. case UInt64:
  48. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, Unsafe.As<T, ulong>(ref value)));
  49. break;
  50. case Single:
  51. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteSgl>()(_Session.Session, Indicator, Unsafe.As<T, float>(ref value)));
  52. break;
  53. case double:
  54. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteDbl>()(_Session.Session, Indicator, Unsafe.As<T, double>(ref value)));
  55. break;
  56. }
  57. }
  58. }
  59. private T GetValue()
  60. {
  61. lock (lockObject)
  62. {
  63. T value = default;
  64. switch (value)
  65. {
  66. case Boolean:
  67. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadBool>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref value)));
  68. break;
  69. case Byte:
  70. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU8>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref value)));
  71. break;
  72. case SByte:
  73. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI8>()(_Session.Session, Indicator, ref Unsafe.As<T, sbyte>(ref value)));
  74. break;
  75. case Int16:
  76. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI16>()(_Session.Session, Indicator, ref Unsafe.As<T, short>(ref value)));
  77. break;
  78. case UInt16:
  79. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU16>()(_Session.Session, Indicator, ref Unsafe.As<T, ushort>(ref value)));
  80. break;
  81. case Int32:
  82. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI32>()(_Session.Session, Indicator, ref Unsafe.As<T, int>(ref value)));
  83. break;
  84. case UInt32:
  85. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU32>()(_Session.Session, Indicator, ref Unsafe.As<T, uint>(ref value)));
  86. break;
  87. case Int64:
  88. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI64>()(_Session.Session, Indicator, ref Unsafe.As<T, long>(ref value)));
  89. break;
  90. case UInt64:
  91. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU64>()(_Session.Session, Indicator, ref Unsafe.As<T, ulong>(ref value)));
  92. break;
  93. case Single:
  94. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadSgl>()(_Session.Session, Indicator, ref Unsafe.As<T, float>(ref value)));
  95. break;
  96. case double:
  97. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadDbl>()(_Session.Session, Indicator, ref Unsafe.As<T, double>(ref value)));
  98. break;
  99. }
  100. return value;
  101. }
  102. }
  103. public T Value { get => GetValue(); set => SetValue(value); }
  104. }
  105. }