protected override bool OnReceiveIRPulseMessage(PulseSpaceDurationList rawDataBuffer, IRPulseMessage message)
        {
            if (CheckMessage(message))
            {
                bool pulse = true; // or space.

                if (rawDataBuffer.Count != message.PulseSpaceDurations.Count)
                {
                    throw new Exception("These should be the same length.");
                }

                var merged = rawDataBuffer
                             .Zip(message.PulseSpaceDurations, (r, m) => new { Raw = r, Clean = m })
                             .Zip(message.PulseSpaceUnits, (durations, units) => new { Raw = durations.Raw, Clean = durations.Clean, Units = units });

                foreach (var item in merged)
                {
                    string type = pulse ? "PULSE" : "SPACE";

                    ConsoleWriteLine($"{type} {item.Raw.ToString().PadLeft(5)} {item.Clean.ToString().PadLeft(5)} {item.Units}");

                    pulse = !pulse;
                }
                ConsoleWriteLine($"----------------------   UnitCount:{message.UnitCount}");
            }

            return(true);
        }
        public void Constructor_Capacity()
        {
            var buffer = new PulseSpaceDurationList(5);

            Assert.That(buffer.Capacity, Is.EqualTo(5));
            Assert.That(buffer.Count, Is.EqualTo(0));
        }
Exemplo n.º 3
0
        public int LearnUnitDuration()
        {
            if (LeadInPatternDurations == null)
            {
                throw new ArgumentNullException(nameof(LeadInPatternDurations));
            }

            _lastReceived = null;

            foreach (int i in LeadInPatternDurations)
            {
                if (i < 30)
                {
                    // A lead-in pattern is unlikely to be more than 30 units, but is also unlikely to be less than 30 microseconds. So we can use this as a sanity check.
                    throw new ArgumentException("The lead-in pattern is specified in microseconds (not units) in this class.");
                }
            }

            var receivedMessages = CaptureIR();

            int smallest     = Min_SkipVerySmallest(receivedMessages.SelectMany(b => b.Copy(100)));
            var smallSamples = receivedMessages.SelectMany(x => x).Where(x => x <(smallest + 80) && x> (smallest - 80));

            return((int)smallSamples.Average());
        }
        public void Constructor_FromDuration_OutOfRangeUnitDuration(int unitDuration)
        {
            IReadOnlyPulseSpaceDurationList list = new PulseSpaceDurationList()
            {
                88,
                106,
                200
            };

            Assert.That(() => new IRPulseMessage(list, unitDuration), Throws.Exception.TypeOf <ArgumentOutOfRangeException>().With.Property("ParamName").EqualTo("unitDuration"));
        }
        public void Constructor_FromUnitList1()
        {
            var buffer = new PulseSpaceDurationList(100, new PulseSpaceUnitList()
            {
                1, 2, 3, 1
            });

            Assert.That(buffer.Count, Is.EqualTo(4));
            Assert.That(buffer[0], Is.EqualTo(100));
            Assert.That(buffer[1], Is.EqualTo(200));
            Assert.That(buffer[2], Is.EqualTo(300));
            Assert.That(buffer[3], Is.EqualTo(100));
        }
Exemplo n.º 6
0
        /// <returns>
        /// The lead-in pattern, expressed as microseconds.
        /// </returns>
        /// <remarks>
        /// This can't return the pattern as a number of units because the unit duration is not known in this class.
        /// </remarks>
        public PulseSpaceDurationList LearnLeadInDurations()
        {
            _debounceTimer.Restart();
            _foundLeadIn = null;
            _received.Clear();

            if (MinimumMatchingCaptures <= 2)
            {
                throw new ArgumentOutOfRangeException(nameof(MinimumMatchingCaptures));
            }

            CaptureFromDevice();
            return(_foundLeadIn);
        }
        protected override bool OnReceivePulseSpaceBlock(PulseSpaceDurationList buffer)
        {
            bool pulse = true; // or space.

            foreach (int item in buffer)
            {
                string type = pulse ? "PULSE" : "SPACE";

                ConsoleWriteLine($"{type} {item.ToString().PadLeft(5)}");

                pulse = !pulse;
            }
            ConsoleWriteLine("----------------------");
            return(true);
        }
        public void CopyWithRounding()
        {
            var buffer = new PulseSpaceDurationList()
            {
                97,
                120,
                180,
                3000,
                3009
            }.Copy(100);

            Assert.That(buffer[0], Is.EqualTo(100));
            Assert.That(buffer[1], Is.EqualTo(100));
            Assert.That(buffer[2], Is.EqualTo(200));
            Assert.That(buffer[3], Is.EqualTo(3000));
            Assert.That(buffer[4], Is.EqualTo(3000));
        }
 protected override bool OnReceiveIRPulseMessage(PulseSpaceDurationList rawDataBuffer, IRPulseMessage message)
 {
     if (CheckMessage(message))
     {
         if (_message != null)
         {
             throw new Exception("Message already has a value.");
         }
         _message = message;
         return(false); // Got the message, stop receiving IR.
     }
     else
     {
         Miss?.Invoke(this, EventArgs.Empty);
         return(true); // Not a good IR signal, keep trying.
     }
 }
Exemplo n.º 10
0
        protected override bool OnReceivePulseSpaceBlock(PulseSpaceDurationList buffer)
        {
            if (buffer.Count < (LeadInPatternDurations.Count + 5))
            {
                return(true);
            }

            for (int i = 0; i < LeadInPatternDurations.Count; i++)
            {
                const int errorMagin = 100;
                if (buffer[i] < (LeadInPatternDurations[i] - errorMagin) || buffer[i] > (LeadInPatternDurations[i] + errorMagin))
                {
                    Miss?.Invoke(this, EventArgs.Empty);
                    return(true); // Wrong pattern, discard this IR message.
                }
            }

            _lastReceived = buffer.Copy();
            return(false);
        }
Exemplo n.º 11
0
        protected override bool OnReceivePulseSpaceBlock(PulseSpaceDurationList buffer)
        {
            if (buffer.Count < 10) // If the message is short then assume it is noise or a key repeat signal.
            {
                return(true);
            }

            if (buffer[0] < 600 || buffer[1] < 400) // The lead-in is usually quite long (typically a few thousand microsecs for the pulse at least). So if the signals are short then it is probably noise.
            {
                return(true);
            }

            if (!_debounceTimer.ReadyToDoAnother) // Allow the user time to let go of the button.
            {
                return(true);
            }

            _received.Add(buffer.Copy());
            Received?.Invoke(this, EventArgs.Empty);
            _debounceTimer.Restart();

            if (_received.Count < MinimumMatchingCaptures)
            {
                // Not even worth trying to work it out before we have enough.
                return(true);
            }

            _foundLeadIn = WorkOutLeadIn();
            if (_foundLeadIn == null)
            {
                if (_received.Count >= MinimumMatchingCaptures * 3)
                {
                    throw new Exceptions.InsufficientMatchingSamplesException("Got 3 times the target number of captures and still can't find a pattern. Either there is too much noise or there is no lead-in.");
                }
                return(true);
            }
            return(false); // Got it, we can stop now.
        }
Exemplo n.º 12
0
        private void LearnLeadInDurations(Mock <FileSystem.IOpenFile> fileHandle, int expectedSignals)
        {
            // ARRANGE
            var fileSystem = MockFileSystem(new[] { fileHandle });
            var subject    = NewLeadInLearner(fileSystem.Object);

            int hitCount = 0;

            subject.Received += (s, e) => hitCount++;

            // ACT
            PulseSpaceDurationList result = subject.LearnLeadInDurations();

            // ASSERT
            fileSystem.Verify(x => x.OpenRead(It.Is <string>(arg => arg == LircPath)), Times.Once);
            fileHandle.Verify(x => x.Dispose(), Times.Once);

            Assert.That(hitCount, Is.EqualTo(expectedSignals));

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(2));
            Assert.That(result[0], Is.InRange(14960, 15040));
            Assert.That(result[1], Is.InRange(4960, 5040));
        }
        public void Constructor_FromUnitListEmpty()
        {
            var buffer = new PulseSpaceDurationList(100, new PulseSpaceUnitList());

            Assert.That(buffer.Count, Is.EqualTo(0));
        }
Exemplo n.º 14
0
 /// <summary>
 /// Handle in incoming IR message. Typically the first thing to do is to call <see cref="CheckMessage"/> to see if it is any good.
 /// </summary>
 /// <param name="rawDataBuffer">The raw PULSE/SPACE data received from the IR driver without any rounding. Typically only useful for diagnostics. Note this buffer is not yours to keep! Once this method returns this buffer will be reused by the calling code. So copy the data if you want to keep it.</param>
 /// <param name="message">The rounded data. This one you can keep.</param>
 /// <returns>
 /// Return TRUE to continue capturing IR or FALSE to stop.
 /// </returns>
 protected abstract bool OnReceiveIRPulseMessage(PulseSpaceDurationList rawDataBuffer, IRPulseMessage message);
Exemplo n.º 15
0
 protected sealed override bool OnReceivePulseSpaceBlock(PulseSpaceDurationList buffer)
 {
     return(OnReceiveIRPulseMessage(buffer, new IRPulseMessage(buffer, UnitDurationMicrosecs)));
 }