FPGAFXPWriteProperty.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using FxpConvert.Common;
  2. using System.Runtime.CompilerServices;
  3. namespace NIFPGA
  4. {
  5. public sealed class FPGAFXPWriteProperty: FPGABaseProperty
  6. {
  7. Interop.NiFpgaDll_ReadU64 Read;
  8. Interop.NiFpgaDll_WriteU64 Write;
  9. private object _Lock = new object();
  10. private NiFpga_FxpTypeInfo _TypeInfo;
  11. private IFxpConvert _Convert;
  12. internal FPGAFXPWriteProperty(FPGASession session, uint indicator,NiFpga_FxpTypeInfo typeInfo,IFxpConvert convert) : base(session, indicator,false)
  13. {
  14. _TypeInfo = typeInfo;
  15. _Convert = convert;
  16. Read = _Session.GetDelegate<Interop.NiFpgaDll_ReadU64>();
  17. Write = _Session.GetDelegate<Interop.NiFpgaDll_WriteU64>();
  18. }
  19. private double GetValue()
  20. {
  21. lock (_Lock)
  22. {
  23. ulong value = 0;
  24. double temp = 0;
  25. _Session.CheckResult(Read(_Session.Session, Indicator, ref value));
  26. _Convert.FxpConvertToDouble(ref value, _TypeInfo, ref temp, 1);
  27. return temp;
  28. }
  29. }
  30. private void SetValue(double value)
  31. {
  32. lock (_Lock)
  33. {
  34. ulong tempul = 0;
  35. _Session.CheckResult(Read(_Session.Session, Indicator, ref tempul));
  36. ulong temp = 0;
  37. _Convert.DoubleConvertToDxp(ref value, _TypeInfo, ref temp, 1);
  38. if (temp == tempul) return;
  39. var _ = _TypeInfo.wordLength switch
  40. {
  41. var x when x <= 8 => _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU8>()(_Session.Session, Indicator, Unsafe.As<ulong, byte>(ref temp))),
  42. var x when x > 8 && x <= 16 => _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU16>()(_Session.Session, Indicator, Unsafe.As<ulong, ushort>(ref temp))),
  43. var x when x > 16 && x <= 32 => _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU32>()(_Session.Session, Indicator, Unsafe.As<ulong, uint>(ref temp))),
  44. var x when x > 32 && x <= 64 => _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, temp)),
  45. _ => _Session.CheckResult(_Session.GetDelegate<Interop.NiFpgaDll_WriteU64>()(_Session.Session, Indicator, temp)),
  46. };
  47. //_Session.CheckResult(Write(_Session.Session, Indicator, temp));
  48. }
  49. }
  50. public double Value { get => GetValue(); set => SetValue(value); }
  51. }
  52. }