DataSrcFifo.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Uestc.Auto6.Dso.ComModel;
  6. using Uestc.Auto6.Dso.Hardware.Driver;
  7. using Uestc.Auto6.Dso.MathExt;
  8. namespace Uestc.Auto6.Dso.Core
  9. {
  10. internal class DataSrcFifo : IDataSource
  11. {
  12. private CohAverager Averager
  13. {
  14. get;
  15. init;
  16. }
  17. private Envelope Evlp
  18. {
  19. get;
  20. init;
  21. }
  22. private sealed record Parameter(WfmProperties Properties, List<UInt16> Buffer, Double Pos0ByAdc, AnaChnlCoupling Coupling, Boolean Inverted, AnaChnlAcqMode Mode, CancellationToken CancelToken);
  23. public Object? Prepare(Boolean init, ChannelId aid, CancellationToken ct)
  24. {
  25. _ = Hd.AnalogChannel!.TakeChannelWaveform((Int32)aid, out var buffer, out var si);
  26. var ach = (AnalogModel)DsoModel.Default.GetChannel(aid);
  27. var prop = new WfmProperties(ach.Name)
  28. {
  29. ChnlPosition = (ach.Conditioning.PosIndex, ach.Conditioning.PositionBymV),
  30. ChnlScale = ((Int32)ach.Conditioning.ScaleIndex, ach.Conditioning.ScaleBymV),
  31. ChnlUnit = (ach.Conditioning.Prefix, ach.Conditioning.Unit),
  32. TmbPosition = (ach.Sampling.PosIndex, ach.Sampling.PositionByus),
  33. TmbScale = ((Int32)ach.Sampling.ScaleIndex, ach.Sampling.ScaleByus),
  34. TmbUnit = (ach.Sampling.Prefix, ach.Sampling.Unit),
  35. SampleIntByus = si.SampleIntervalByus,
  36. Factor = si.SampleIntervalByus * Constants.IDX_PER_XDIV / ach.Sampling.ScaleByus,
  37. };
  38. Averager.MaxCnts = ach.Sampling.AverageCnt;
  39. //Evlp.MaxCnts = ach.Sampling.EnvelopeCnt;
  40. if (init)
  41. {
  42. Averager.Reset();
  43. //Evlp.Reset();
  44. }
  45. var pos0 = ach.Conditioning.PosIndex / Constants.IDX_PER_YDIV * Constants.SAMPS_PER_YDIV + Constants.MAX_ADC_RES / 2;
  46. return new Parameter(prop, buffer, pos0, ach.Conditioning.Coupling, ach.Conditioning.IsInverted, ach.Sampling.Mode, ct);
  47. }
  48. public (Double[,], Object)? Read(Object? param)
  49. {
  50. var pm = (Parameter)param!;
  51. return (pm.Buffer.Select((s) => (Double)s).ToRowVector(), pm.Properties);
  52. }
  53. public WfmPack Process((Double[,] Buffer, Object Prop) pkg, Object? param)
  54. {
  55. var pm = (Parameter)param!;
  56. for (Int32 i = 0; i < pkg.Buffer.GetLength(0); i++)
  57. {
  58. if (pm.Inverted)
  59. {
  60. Double temp = pm.Pos0ByAdc * 2;
  61. for (Int32 j = 0; j < pkg.Buffer.GetLength(1); j++)
  62. pkg.Buffer[i, j] = temp - pkg.Buffer[i, j];
  63. }
  64. for (Int32 j = 0; j < pkg.Buffer.GetLength(1); j++)
  65. {
  66. if (pkg.Buffer[i, j] > Constants.MAX_ADC_RES)
  67. pkg.Buffer[i, j] = Constants.MAX_ADC_RES;
  68. else if (pkg.Buffer[i, j] < 0)
  69. pkg.Buffer[i, j] = 0;
  70. pkg.Buffer[i, j] = (pkg.Buffer[i, j] - pm.Pos0ByAdc) / Constants.SAMPS_PER_YDIV * pm.Properties.ChnlScale.Value;
  71. }
  72. switch (pm.Mode)
  73. {
  74. case AnaChnlAcqMode.Average:
  75. pkg.Buffer = Averager.Run(pkg.Buffer, 0);
  76. break;
  77. //case AnaChnlAcqMode.Envelope:
  78. // matrix = Evlp.Run(matrix);
  79. // break;
  80. }
  81. }
  82. return new WfmPack(pkg.Buffer, 0, pkg.Buffer.GetLength(1), (WfmProperties)pkg.Prop);
  83. }
  84. public DataSrcFifo(Int32 length)
  85. {
  86. Averager = new(1, length);
  87. Evlp = new(1, length);
  88. }
  89. }
  90. }