FPGAWriteProperty.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. T oldvalue = GetValue();
  22. if (object.Equals(value, oldvalue)) return;
  23. switch (value)
  24. {
  25. case Boolean boolvalue:
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteBool>()(_Session.Session, Indicator, Convert.ToByte(boolvalue)));
  27. break;
  28. case Byte:
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU8>()(_Session.Session, Indicator, Unsafe.As<T, byte>(ref value)));
  30. break;
  31. case SByte:
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI8>()(_Session.Session, Indicator, Unsafe.As<T, sbyte>(ref value)));
  33. break;
  34. case Int16:
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI16>()(_Session.Session, Indicator, Unsafe.As<T, short>(ref value)));
  36. break;
  37. case UInt16:
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU16>()(_Session.Session, Indicator, Unsafe.As<T, ushort>(ref value)));
  39. break;
  40. case Int32:
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI32>()(_Session.Session, Indicator, Unsafe.As<T, int>(ref value)));
  42. break;
  43. case UInt32:
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU32>()(_Session.Session, Indicator, Unsafe.As<T, uint>(ref value)));
  45. break;
  46. case Int64:
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI64>()(_Session.Session, Indicator, Unsafe.As<T, long>(ref value)));
  48. break;
  49. case UInt64:
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, Unsafe.As<T, ulong>(ref value)));
  51. break;
  52. case Single:
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteSgl>()(_Session.Session, Indicator, Unsafe.As<T, float>(ref value)));
  54. break;
  55. case double:
  56. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteDbl>()(_Session.Session, Indicator, Unsafe.As<T, double>(ref value)));
  57. break;
  58. }
  59. }
  60. }
  61. private T GetValue()
  62. {
  63. lock (lockObject)
  64. {
  65. T value = default;
  66. switch (value)
  67. {
  68. case Boolean:
  69. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadBool>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref value)));
  70. break;
  71. case Byte:
  72. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU8>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref value)));
  73. break;
  74. case SByte:
  75. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI8>()(_Session.Session, Indicator, ref Unsafe.As<T, sbyte>(ref value)));
  76. break;
  77. case Int16:
  78. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI16>()(_Session.Session, Indicator, ref Unsafe.As<T, short>(ref value)));
  79. break;
  80. case UInt16:
  81. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU16>()(_Session.Session, Indicator, ref Unsafe.As<T, ushort>(ref value)));
  82. break;
  83. case Int32:
  84. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI32>()(_Session.Session, Indicator, ref Unsafe.As<T, int>(ref value)));
  85. break;
  86. case UInt32:
  87. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU32>()(_Session.Session, Indicator, ref Unsafe.As<T, uint>(ref value)));
  88. break;
  89. case Int64:
  90. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI64>()(_Session.Session, Indicator, ref Unsafe.As<T, long>(ref value)));
  91. break;
  92. case UInt64:
  93. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU64>()(_Session.Session, Indicator, ref Unsafe.As<T, ulong>(ref value)));
  94. break;
  95. case Single:
  96. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadSgl>()(_Session.Session, Indicator, ref Unsafe.As<T, float>(ref value)));
  97. break;
  98. case double:
  99. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadDbl>()(_Session.Session, Indicator, ref Unsafe.As<T, double>(ref value)));
  100. break;
  101. }
  102. return value;
  103. }
  104. }
  105. public T Value { get => GetValue(); set => SetValue(value); }
  106. }
  107. }