ReadFifo.cs 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. internal ReadFifo(FPGASession session, uint fifosession) : base(session, fifosession)
  16. {
  17. }
  18. public void Read(ref T value,uint count, uint timeout, ref uint elementsRemaining)
  19. {
  20. switch(value)
  21. {
  22. case bool:
  23. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoBool>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  24. break;
  25. case byte:
  26. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  27. break;
  28. case sbyte:
  29. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI8>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  30. break;
  31. case ushort:
  32. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  33. break;
  34. case short:
  35. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI16>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  36. break;
  37. case int:
  38. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  39. break;
  40. case uint:
  41. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU32>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  42. break;
  43. case long:
  44. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoI64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  45. break;
  46. case ulong:
  47. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoU64>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  48. break;
  49. case float:
  50. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoSgl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  51. break;
  52. case double:
  53. _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_ReadFifoDbl>()(_Session.Session, FifoSession, ref Unsafe.As<T, byte>(ref value), count, timeout, ref elementsRemaining));
  54. break;
  55. }
  56. }
  57. public uint ElementsRemaining
  58. {
  59. get
  60. {
  61. T val = default;
  62. uint elementsRemaining = 0;
  63. Read(ref val, 0, 10, ref elementsRemaining);
  64. return elementsRemaining;
  65. }
  66. }
  67. }
  68. public class ReadFXPFifo : ReadFifo<ulong>
  69. {
  70. private NiFpga_FxpTypeInfo _TypeInfo;
  71. private IFxpConvert _Convert;
  72. internal ReadFXPFifo(FPGASession session, uint fifosession,NiFpga_FxpTypeInfo typeInfo, IFxpConvert convert) : base(session, fifosession)
  73. {
  74. if(convert ==null)throw new ArgumentNullException(nameof(convert));
  75. _Convert = convert;
  76. _TypeInfo = typeInfo;
  77. }
  78. public void Read(ref float value,uint count, uint timeout, ref uint elementsRemaining)
  79. {
  80. if (count == 0)
  81. {
  82. ulong[] temp = new ulong[1];
  83. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  84. }
  85. else
  86. {
  87. ulong[] temp = new ulong[count];
  88. base.Read(ref temp[0], count, timeout, ref elementsRemaining);
  89. _Convert.FxpConvertToFloat(ref temp[0], _TypeInfo, ref value, count);
  90. }
  91. }
  92. }
  93. }