FPGAProperty.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. using FxpConvert.Common;
  9. namespace NIFPGA
  10. {
  11. public sealed class FPGAProperty<T> : FPGABaseProperty
  12. where T : unmanaged
  13. {
  14. internal FPGAProperty(FPGASession session, uint indicator,bool isIndicator = true) : base(session, indicator,isIndicator)
  15. {
  16. }
  17. private void SetValue(T value)
  18. {
  19. switch(value)
  20. {
  21. case Boolean boolvalue:
  22. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteBool>()(_Session.Session, Indicator, Convert.ToByte(boolvalue)));
  23. break;
  24. case Byte:
  25. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU8>()(_Session.Session, Indicator, Unsafe.As<T,byte>(ref value)));
  26. break;
  27. case SByte:
  28. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI8>()(_Session.Session, Indicator, Unsafe.As<T, sbyte>(ref value)));
  29. break;
  30. case Int16:
  31. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI16>()(_Session.Session, Indicator, Unsafe.As<T, short>(ref value)));
  32. break;
  33. case UInt16:
  34. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU16>()(_Session.Session, Indicator, Unsafe.As<T, ushort>(ref value)));
  35. break;
  36. case Int32:
  37. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI32>()(_Session.Session, Indicator, Unsafe.As<T, int>(ref value)));
  38. break;
  39. case UInt32:
  40. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU32>()(_Session.Session, Indicator, Unsafe.As<T, uint>(ref value)));
  41. break;
  42. case Int64:
  43. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteI64>()(_Session.Session, Indicator, Unsafe.As<T, long>(ref value)));
  44. break;
  45. case UInt64:
  46. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, Unsafe.As<T, ulong>(ref value)));
  47. break;
  48. case Single:
  49. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteSgl>()(_Session.Session, Indicator, Unsafe.As<T, float>(ref value)));
  50. break;
  51. case double:
  52. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteDbl>()(_Session.Session, Indicator, Unsafe.As<T, double>(ref value)));
  53. break;
  54. }
  55. }
  56. private T GetValue()
  57. {
  58. T value = default;
  59. switch (value)
  60. {
  61. case Boolean:
  62. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadBool>()(_Session.Session, Indicator,ref Unsafe.As<T,byte>(ref value)));
  63. break;
  64. case Byte:
  65. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU8>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref value)));
  66. break;
  67. case SByte:
  68. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI8>()(_Session.Session, Indicator, ref Unsafe.As<T, sbyte>(ref value)));
  69. break;
  70. case Int16:
  71. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI16>()(_Session.Session, Indicator, ref Unsafe.As<T, short>(ref value)));
  72. break;
  73. case UInt16:
  74. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU16>()(_Session.Session, Indicator, ref Unsafe.As<T, ushort>(ref value)));
  75. break;
  76. case Int32:
  77. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI32>()(_Session.Session, Indicator, ref Unsafe.As<T, int>(ref value)));
  78. break;
  79. case UInt32:
  80. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU32>()(_Session.Session, Indicator, ref Unsafe.As<T, uint>(ref value)));
  81. break;
  82. case Int64:
  83. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadI64>()(_Session.Session, Indicator, ref Unsafe.As<T, long>(ref value)));
  84. break;
  85. case UInt64:
  86. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU64>()(_Session.Session, Indicator, ref Unsafe.As<T, ulong>(ref value)));
  87. break;
  88. case Single:
  89. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadSgl>()(_Session.Session, Indicator, ref Unsafe.As<T, float>(ref value)));
  90. break;
  91. case double:
  92. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadDbl>()(_Session.Session, Indicator, ref Unsafe.As<T, double>(ref value)));
  93. break;
  94. }
  95. return value;
  96. }
  97. public T Value { get=>GetValue(); set=>SetValue(value); }
  98. }
  99. public sealed class FPGAFXPProperty: FPGABaseProperty
  100. {
  101. private NiFpga_FxpTypeInfo _TypeInfo;
  102. private IFxpConvert _Convert;
  103. internal FPGAFXPProperty(FPGASession session, uint indicator,NiFpga_FxpTypeInfo typeInfo,IFxpConvert convert, bool isIndicator = true) : base(session, indicator,isIndicator)
  104. {
  105. _TypeInfo = typeInfo;
  106. _Convert = convert;
  107. }
  108. private float GetValue()
  109. {
  110. ulong value=0;
  111. float temp = 0;
  112. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadU64>()(_Session.Session, Indicator, ref value));
  113. _Convert.FxpConvertToFloat(ref value, _TypeInfo, ref temp, 1);
  114. return temp;
  115. }
  116. private void SetValue(float value)
  117. {
  118. ulong temp = 0;
  119. _Convert.FloatConvertToDxp(ref value, _TypeInfo, ref temp, 1);
  120. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, temp));
  121. }
  122. public float Value { get=>GetValue(); set=>SetValue(value); }
  123. }
  124. public sealed class FPGAArrayProperty<T> : FPGABaseProperty
  125. where T : unmanaged
  126. {
  127. private T[] tempvalue = new T[0];
  128. internal FPGAArrayProperty(FPGASession session, uint indicator,uint count, bool isIndicator = true) : base(session, indicator,isIndicator)
  129. {
  130. Count= count;
  131. tempvalue= new T[Count];
  132. }
  133. private void SetValue(T[] values)
  134. {
  135. if (values.Length == 0 || values.Length!=Count) return;
  136. switch (values[0])
  137. {
  138. case (bool):
  139. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayBool>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref values[0]), Count));
  140. break;
  141. case (byte):
  142. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU8>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref values[0]), Count));
  143. break;
  144. case (sbyte):
  145. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI8>()(_Session.Session, Indicator, ref Unsafe.As<T, sbyte>(ref values[0]), Count));
  146. break;
  147. case (short):
  148. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI16>()(_Session.Session, Indicator, ref Unsafe.As<T, short>(ref values[0]), Count));
  149. break;
  150. case (ushort):
  151. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU16>()(_Session.Session, Indicator, ref Unsafe.As<T, ushort>(ref values[0]), Count));
  152. break;
  153. case (int):
  154. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI32>()(_Session.Session, Indicator, ref Unsafe.As<T, int>(ref values[0]), Count));
  155. break;
  156. case (uint):
  157. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU32>()(_Session.Session, Indicator, ref Unsafe.As<T, uint>(ref values[0]), Count));
  158. break;
  159. case (long):
  160. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayI64>()(_Session.Session, Indicator, ref Unsafe.As<T, long>(ref values[0]), Count));
  161. break;
  162. case (ulong):
  163. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU64>()(_Session.Session, Indicator, ref Unsafe.As<T, ulong>(ref values[0]), Count));
  164. break;
  165. case (float):
  166. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArraySgl>()(_Session.Session, Indicator, ref Unsafe.As<T, float>(ref values[0]), Count));
  167. break;
  168. case (double):
  169. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayDbl>()(_Session.Session, Indicator, ref Unsafe.As<T, double>(ref values[0]), Count));
  170. break;
  171. }
  172. }
  173. private T[] GetValues()
  174. {
  175. if (Count == 0) return tempvalue;
  176. switch (tempvalue[0])
  177. {
  178. case (bool):
  179. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayBool>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  180. break;
  181. case (byte):
  182. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU8>()(_Session.Session, Indicator, ref Unsafe.As<T, byte>(ref tempvalue[0]), Count));
  183. break;
  184. case (sbyte):
  185. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI8>()(_Session.Session, Indicator, ref Unsafe.As<T, sbyte>(ref tempvalue[0]), Count));
  186. break;
  187. case short:
  188. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI16>()(_Session.Session, Indicator, ref Unsafe.As<T, short>(ref tempvalue[0]),Count));
  189. break;
  190. case (ushort):
  191. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU16>()(_Session.Session, Indicator, ref Unsafe.As<T, ushort>(ref tempvalue[0]),Count));
  192. break;
  193. case (int):
  194. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI32>()(_Session.Session, Indicator, ref Unsafe.As<T, int>(ref tempvalue[0]),Count));
  195. break;
  196. case (uint):
  197. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU32>()(_Session.Session, Indicator, ref Unsafe.As<T, uint>(ref tempvalue[0]),Count));
  198. break;
  199. case (long):
  200. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayI64>()(_Session.Session, Indicator, ref Unsafe.As<T, long>(ref tempvalue[0]),Count));
  201. break;
  202. case (ulong):
  203. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU64>()(_Session.Session, Indicator, ref Unsafe.As<T, ulong>(ref tempvalue[0]),Count));
  204. break;
  205. case (float):
  206. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArraySgl>()(_Session.Session, Indicator, ref Unsafe.As<T, float>(ref tempvalue[0]),Count));
  207. break;
  208. case (double):
  209. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayDbl>()(_Session.Session, Indicator, ref Unsafe.As<T, double>(ref tempvalue[0]), Count));
  210. break;
  211. }
  212. return tempvalue;
  213. }
  214. public uint Count { get; }
  215. public T[] Values { get=>GetValues(); set=>SetValue(value); }
  216. }
  217. public sealed class FPGAArrayFXPProperty : FPGABaseProperty
  218. {
  219. private NiFpga_FxpTypeInfo _TypeInfo;
  220. private IFxpConvert _Convert;
  221. private ulong[] tempvalue = new ulong[0];
  222. private float[] doubles= new float[0];
  223. internal FPGAArrayFXPProperty(FPGASession session, uint indicator, uint count,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert, bool isIndicator = true) : base(session, indicator,isIndicator)
  224. {
  225. _Convert = convert;
  226. Count = count;
  227. tempvalue = new ulong[count];
  228. doubles = new float[count];
  229. _TypeInfo = typeInfo;
  230. }
  231. private void SetData(float[] values)
  232. {
  233. if (values.Length != Count) return;
  234. _Convert.FloatConvertToDxp(ref values[0], _TypeInfo, ref tempvalue[0], Count);
  235. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteArrayU64>()(_Session.Session,Indicator, ref tempvalue[0], Count));
  236. }
  237. private float[] GetData()
  238. {
  239. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadArrayU64>()(_Session.Session,Indicator, ref tempvalue[0], Count));
  240. _Convert.FxpConvertToFloat(ref tempvalue[0], _TypeInfo,ref doubles[0], Count);
  241. return doubles;
  242. }
  243. public uint Count { get; }
  244. public float[] Value { get=>GetData(); set=>SetData(value); }
  245. }
  246. }