ReadFifo.cs 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Headers;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using FxpConvert.Common;
  10. namespace NIFPGA
  11. {
  12. public class ReadFifo<T>:Fifo
  13. where T : unmanaged
  14. {
  15. private object _Lock = new object();
  16. internal ReadFifo(FPGASession session, uint fifosession) : base(session, fifosession)
  17. {
  18. }
  19. public void Read(ref T value,uint count, uint timeout, ref uint elementsRemaining)
  20. {
  21. lock (_Lock)
  22. {
  23. switch (value)
  24. {
  25. case bool:
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoBool>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  27. break;
  28. case byte:
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  30. break;
  31. case sbyte:
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  33. break;
  34. case ushort:
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  36. break;
  37. case short:
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  39. break;
  40. case int:
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  42. break;
  43. case uint:
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  45. break;
  46. case long:
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  48. break;
  49. case ulong:
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  51. break;
  52. case float:
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  54. break;
  55. case double:
  56. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  57. break;
  58. }
  59. }
  60. }
  61. public uint ElementsRemaining
  62. {
  63. get
  64. {
  65. T val = default;
  66. uint elementsRemaining = 0;
  67. Read(ref val, 0, 10, ref elementsRemaining);
  68. return elementsRemaining;
  69. }
  70. }
  71. }
  72. public class ReadFXPFifo : ReadFifo<ulong>
  73. {
  74. private NiFpga_FxpTypeInfo _TypeInfo;
  75. private IFxpConvert _Convert;
  76. internal ReadFXPFifo(FPGASession session, uint fifosession,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, fifosession)
  77. {
  78. if(convert ==null)throw new ArgumentNullException(nameof(convert));
  79. _Convert = convert;
  80. _TypeInfo = typeInfo;
  81. }
  82. public void Read(ref float value,uint count, uint timeout, ref uint elementsRemaining)
  83. {
  84. if (count == 0)
  85. {
  86. ulong[] temp = new ulong[1];
  87. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  88. }
  89. else
  90. {
  91. ulong[] temp = new ulong[count];
  92. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  93. _Convert.FxpConvertToFloat(ref temp[0], _TypeInfo, ref value, count);
  94. }
  95. }
  96. }
  97. }