Beispiel #1
0
        /// <summary>
        /// Open the IR device and begin reading.
        /// </summary>
        protected void CaptureFromDevice()
        {
            if (!System.Threading.Monitor.TryEnter(_capturingLocker))
            {
                throw new InvalidOperationException("Capture already in progress.");
            }
            try
            {
                OnBeforeRawCapture();

                if (string.IsNullOrWhiteSpace(CaptureDevice))
                {
                    throw new ArgumentNullException(nameof(CaptureDevice), $"The capture device must be set.");
                }

                using (var irDevice = _fileSystem.OpenRead(CaptureDevice))
                {
                    DeviceFeatures deviceFeatures = _utility.GetFeatures(irDevice);
                    CheckDevice(irDevice, deviceFeatures);

                    if (TimeoutMicrosecs >= 0)
                    {
                        _utility.SetRxTimeout(irDevice, TimeoutMicrosecs);
                    }
                    byte[] buffer = new byte[4];
                    while (true)
                    {
                        int readCount = irDevice.Stream.Read(buffer, 0, buffer.Length); // Note if "modprobe -r gpio_ir_recv" is used to unload the driver while this is reading then we get an IOException.
                        if (readCount != 4)
                        {
                            throw new System.IO.IOException($"Read {readCount} bytes instead of the expected 4.");
                        }

                        Mode2PacketType packetType = (Mode2PacketType)buffer[3];
                        buffer[3] = 0;

                        Lazy <int> number = new Lazy <int>(() => BitConverter.ToInt32(buffer));

                        if (!OnReceivePacket(packetType, number))
                        {
                            return;
                        }
                    }
                }
            }
            finally
            {
                System.Threading.Monitor.Exit(_capturingLocker);
            }
        }
Beispiel #2
0
 /// <returns>
 /// Return TRUE to continue capturing IR or FALSE to stop.
 /// </returns>
 protected abstract bool OnReceivePacket(Mode2PacketType packetType, Lazy <int> packetData);
 public UnknownPacketTypeException(Mode2PacketType type) : base($"Unknown packet type {type}.")
 {
     Mode2PacketType = (byte)type;
 }