WriteFifo.cs 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Tracing;
  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 class WriteFifo<T>:Fifo
  12. where T : unmanaged
  13. {
  14. private object _Locker = new object();
  15. internal WriteFifo(FPGASession session, uint fifosession) : base(session, fifosession)
  16. {
  17. }
  18. public void Write(T[] value,uint timeout,ref uint emptyElementsRemaining)
  19. {
  20. lock (_Locker)
  21. {
  22. if (value.Length == 0) return;
  23. switch (value[0])
  24. {
  25. case bool:
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoBool>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  27. break;
  28. case byte:
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  30. break;
  31. case sbyte:
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  33. break;
  34. case short:
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  36. break;
  37. case ushort:
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  39. break;
  40. case int:
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  42. break;
  43. case uint:
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  45. break;
  46. case long:
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  48. break;
  49. case ulong:
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  51. break;
  52. case float:
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  54. break;
  55. case double:
  56. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value[0]), (uint)value.Length, timeout, ref emptyElementsRemaining));
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. public class WriteFXPFifo : WriteFifo<ulong>
  63. {
  64. private IFxpConvert _Convert;
  65. private NiFpga_FxpTypeInfo _TypeInfo;
  66. internal WriteFXPFifo(FPGASession session, uint fifosession, NiFpga_FxpTypeInfo typeInfo,IFxpConvert convert) : base(session, fifosession)
  67. {
  68. if(convert == null) throw new ArgumentNullException(nameof(convert));
  69. _Convert = convert;
  70. _TypeInfo = typeInfo;
  71. }
  72. public void Write(ref float value, uint timeout, uint length, ref uint emptyElementsRemaining)
  73. {
  74. if (length == 0) return;
  75. ulong[] temp= new ulong[length];
  76. _Convert.FloatConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  77. base.Write(temp, timeout, ref emptyElementsRemaining);
  78. }
  79. public void Write(ref double value, uint timeout, uint length, ref uint emptyElementsRemaining)
  80. {
  81. if (length == 0) return;
  82. ulong[] temp = new ulong[length];
  83. _Convert.DoubleConvertToDxp(ref value, _TypeInfo, ref temp[0], (uint)length);
  84. base.Write(temp, timeout, ref emptyElementsRemaining);
  85. }
  86. }
  87. }