/// <summary> /// Decodes the incoming PPM signal (each complete cycle) using the CPPM protocol. /// </summary> /// <param name="cycle">PPM cycle to decode.</param> /// <returns> /// <see cref="PpmFrame"/> when complete else null whilst decoding or skipping invalid cycles. /// </returns> private PpmFrame DecodeCycle(PpmCycle cycle) { // Validate if (!cycle.IsValid() || cycle.LowLength >= LowLimit) { // Discard frame _channel = null; return(null); } // Detect start frame if (cycle.HighLength >= SyncLengthMinimum) { // Start decoding from channel 0 at next pulse _channel = 0; _frame = new PpmFrame(cycle.LowTime, new int[ChannelCount]); return(null); } // Do nothing when not decoding if (!_channel.HasValue) { return(null); } var decodeIndex = _channel.Value; // Store channel value whilst decoding if (decodeIndex < ChannelCount) { // Store channel value _frame.Channels[decodeIndex] = (int)cycle.Length; // Wait for next channel... _channel = decodeIndex + 1; // Complete frame when all channels decoded... if (decodeIndex == ChannelCount - 1) { var frame = _frame; _frame = null; return(frame); } } // Continue... return(null); }
/// <summary> /// Creates an instance. /// </summary> public CppmDecoder() { _frame = new PpmFrame(); _cycle = new PpmCycle(); }