using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TBF.BenchControl.RegisterReaders.SerialStream { public class FrameFormat { public readonly int FrameLength; /// Frame length in characters/bytes, 0 = unknown public readonly double FrameFrequency; /// Number of frames per second (Hz) public readonly Config.Unit VolumeUnits; public readonly double VolumeScaleFactor; /// Volume in VolumeInits = volmeRaw / VolumeScaleFactor public readonly int VolumeFieldStart; /// Start of the volume field: 0-based index of the first character, -1 = no volume field in the frame public readonly int VolumeFieldEnd; /// End of the volume field: 0-based index of the last character public readonly FieldFormat VolumeFieldFormat; /// Hexadecimal or Decimal public readonly Config.Unit TimeUnits; public readonly double TimeScaleFactor; /// Time in TimeUnits = timeRaw / TimeScaleFactor public readonly int TimeFieldStart; /// Start of the time field: 0-based index of the first character, -1 = no time field in the frame public readonly int TimeFieldEnd; /// End of the time field: 0-based index of the last character public readonly FieldFormat TimeFieldFormat; /// Hexadecimal or Decimal /// /// Calculated in constructor /// public readonly int VolumeFieldLength; public readonly UInt64 VolumeWindowMask; public readonly Int64 VolumeWrapIncrement; public readonly Int64 VolumeWrapThreshold; public readonly int TimeFieldLength; public readonly UInt64 TimeWindowMask; public readonly Int64 TimeWrapIncrement; public readonly Int64 TimeWrapThreshold; public readonly long RawTimeIncrementPerFrame; public FrameFormat(int frameLength, double frameFrequency, Config.Unit volumeUnits, double volumeScaleFactor, int volumeFieldStart, int volumeFieldEnd, FieldFormat volumeFieldFormat, Config.Unit timeUnits, double timeScaleFactor, int timeFieldStart, int timeFieldEnd, FieldFormat timeFieldFormat) { FrameLength = frameLength; FrameFrequency = frameFrequency; VolumeUnits = volumeUnits; VolumeScaleFactor = volumeScaleFactor; VolumeFieldStart = volumeFieldStart; VolumeFieldEnd = volumeFieldEnd; VolumeFieldFormat = volumeFieldFormat; TimeUnits = timeUnits; TimeScaleFactor = timeScaleFactor; TimeFieldStart = timeFieldStart; TimeFieldEnd = timeFieldEnd; TimeFieldFormat = timeFieldFormat; RawTimeIncrementPerFrame = (FrameFrequency != 0) ? (long)Math.Round(TimeScaleFactor / FrameFrequency) : 1L; VolumeFieldLength = VolumeFieldEnd - VolumeFieldStart + 1; if (volumeFieldFormat == FieldFormat.HexadecimalWindow && volumeFieldStart >= 0 && VolumeFieldLength >= 1 && VolumeFieldLength <= 8) { VolumeWindowMask = 0xFFFFFFFFFFFFFFFFUL << (4 * VolumeFieldLength); VolumeWrapIncrement = 1L << (4 * VolumeFieldLength); VolumeWrapThreshold = 1L << (4 * VolumeFieldLength - 1); } TimeFieldLength = TimeFieldEnd - TimeFieldStart + 1; if (volumeFieldFormat == FieldFormat.HexadecimalWindow && volumeFieldStart >= 0 && TimeFieldLength >= 1 && TimeFieldLength <= 8) { TimeWindowMask = 0xFFFFFFFFFFFFFFFFUL << (4 * TimeFieldLength); TimeWrapIncrement = 1L << (4 * TimeFieldLength); TimeWrapThreshold = 1L << (4 * TimeFieldLength - 1); } } public override string ToString() { return string.Format("L={0} V={1}-{2}-{3}-{4}-{5} T={6}-{7}-{8}-{9}-{10}", FrameLength, VolumeUnits, VolumeScaleFactor, VolumeFieldStart, VolumeFieldEnd, VolumeFieldFormat, TimeUnits, TimeScaleFactor, TimeFieldStart, TimeFieldEnd, TimeFieldFormat); } } }