private void ReadThread()
        {
            try
            {
                byte[] timingCode = new byte[DeviceBuffer];
                int    codeLength = 0;
                int    returnCode;

                DoSetInfraBufferEmpty();

                while (true)
                {
                    returnCode = DoGetInfraCode(ref timingCode, ref codeLength);

                    switch (returnCode)
                    {
                    case NO_ERROR:
                        byte[] data = new byte[codeLength];
                        Array.Copy(timingCode, data, codeLength);

                        int[] timingData = GetTimingData(data);

                        IrDecoder.DecodeIR(timingData, RemoteEvent, null, null);

                        Thread.Sleep(100);
                        DoSetInfraBufferEmpty();
                        break;

                    case NO_DATA_AVAILABLE:
                        continue;

                    case DEVICE_NOT_PRESENT:
                        throw new IOException("Device not present");

                    case INVALID_BAUDRATE:
                        throw new IOException("Invalid baud rate");

                    case OVERRUN_ERROR:
                        throw new IOException("Overrun error");

                    default:
                        throw new IOException(String.Format("Unknown error ({0})", returnCode));
                    }
                }
            }
#if TRACE
            catch (ThreadAbortException ex)
            {
                Trace.WriteLine(ex.ToString());
            }
#else
            catch (ThreadAbortException)
            {
            }
#endif
        }
        private static IntermediateRepresentation DecodeIr(byte[] buffer)
        {
            var irDecoder = new IrDecoder(buffer);

            return(irDecoder.Decode());
        }
示例#3
0
        /// <summary>
        /// Device read thread method.
        /// </summary>
        private void ReadThread()
        {
            byte[] packetBytes;

            WaitHandle waitHandle     = new ManualResetEvent(false);
            SafeHandle safeWaitHandle = waitHandle.SafeWaitHandle;

            WaitHandle[] waitHandles = new WaitHandle[] { waitHandle, _stopReadThread };

            IntPtr deviceBufferPtr = IntPtr.Zero;

            bool success = false;

            safeWaitHandle.DangerousAddRef(ref success);
            if (!success)
            {
                throw new InvalidOperationException("Failed to initialize safe wait handle");
            }

            try
            {
                IntPtr dangerousWaitHandle = safeWaitHandle.DangerousGetHandle();

                DeviceIoOverlapped overlapped = new DeviceIoOverlapped();
                overlapped.ClearAndSetEvent(dangerousWaitHandle);

                deviceBufferPtr = Marshal.AllocHGlobal(DeviceBufferSize);

                while (_readThreadMode != ReadThreadMode.Stop)
                {
                    int  lastError;
                    int  bytesRead;
                    bool readDevice = ReadFile(_readHandle, deviceBufferPtr, DeviceBufferSize, out bytesRead,
                                               overlapped.Overlapped);
                    lastError = Marshal.GetLastWin32Error();

                    if (!readDevice)
                    {
                        if (lastError != ErrorSuccess && lastError != ErrorIoPending)
                        {
                            throw new Win32Exception(lastError);
                        }

                        while (true)
                        {
                            int handle = WaitHandle.WaitAny(waitHandles, 2 * PacketTimeout, false);

                            if (handle == ErrorWaitTimeout)
                            {
                                continue;
                            }

                            if (handle == 0)
                            {
                                break;
                            }

                            if (handle == 1)
                            {
                                throw new ThreadInterruptedException("Read thread stopping by request");
                            }

                            throw new InvalidOperationException(String.Format("Invalid wait handle return: {0}", handle));
                        }

                        bool getOverlapped = GetOverlappedResult(_readHandle, overlapped.Overlapped, out bytesRead, true);
                        lastError = Marshal.GetLastWin32Error();

                        if (!getOverlapped && lastError != ErrorSuccess)
                        {
                            throw new Win32Exception(lastError);
                        }
                    }

                    if (bytesRead == 0)
                    {
                        continue;
                    }

                    packetBytes = new byte[bytesRead];
                    Marshal.Copy(deviceBufferPtr, packetBytes, 0, bytesRead);

                    DebugWrite("Received bytes ({0}): ", bytesRead);
                    DebugDump(packetBytes);

                    int[] timingData = null;

                    if (_decodeCarry != 0 || packetBytes[0] >= 0x81 && packetBytes[0] <= 0x9E)
                    {
                        timingData = GetTimingDataFromPacket(packetBytes);
                    }
                    else
                    {
                        double firmware = 0.0;

                        int indexOfFF = Array.IndexOf(packetBytes, (byte)0xFF);
                        while (indexOfFF != -1)
                        {
                            if (packetBytes.Length > indexOfFF + 2 && packetBytes[indexOfFF + 1] == 0x0B) // FF 0B XY - Firmware X.Y00
                            {
                                byte b1 = packetBytes[indexOfFF + 2];

                                firmware += (b1 >> 4) + (0.1 * (b1 & 0x0F));
                                DebugWriteLine("Firmware: {0}", firmware);
                            }

                            if (packetBytes.Length > indexOfFF + 2 && packetBytes[indexOfFF + 1] == 0x1B) // FF 1B XY - Firmware 0.0XY
                            {
                                byte b1 = packetBytes[indexOfFF + 2];

                                firmware += (0.01 * (b1 >> 4)) + (0.001 * (b1 & 0x0F));
                                DebugWriteLine("Firmware: {0}", firmware);
                            }

                            if (packetBytes.Length > indexOfFF + 1)
                            {
                                indexOfFF = Array.IndexOf(packetBytes, (byte)0xFF, indexOfFF + 1);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    switch (_readThreadMode)
                    {
                    case ReadThreadMode.Receiving:
                    {
                        IrDecoder.DecodeIR(timingData, _remoteCallback, _keyboardCallback, _mouseCallback);
                        break;
                    }

                    case ReadThreadMode.Learning:
                    {
                        if (timingData == null)
                        {
                            if (_learningCode.TimingData.Length > 0)
                            {
                                _learningCode   = null;
                                _readThreadMode = ReadThreadMode.LearningFailed;
                            }
                            break;
                        }

                        if (_learningCode == null)
                        {
                            throw new InvalidOperationException("Learning not initialised correctly, _learningCode object is null");
                        }

                        _learningCode.AddTimingData(timingData);

                        // Example: 9F 01 02 9F 15 00 BE 80
                        int indexOf9F = Array.IndexOf(packetBytes, (byte)0x9F);
                        while (indexOf9F != -1)
                        {
                            if (packetBytes.Length > indexOf9F + 3 && packetBytes[indexOf9F + 1] == 0x15) // 9F 15 XX XX
                            {
                                byte b1 = packetBytes[indexOf9F + 2];
                                byte b2 = packetBytes[indexOf9F + 3];

                                int onTime, onCount;
                                GetIrCodeLengths(_learningCode, out onTime, out onCount);

                                double carrierCount = (b1 * 256) + b2;

                                if (carrierCount / onCount < 2.0)
                                {
                                    _learningCode.Carrier = IrCode.CarrierFrequencyDCMode;
                                }
                                else
                                {
                                    double carrier = 1000000 * carrierCount / onTime;

                                    // TODO: Double-Check this calculation.
                                    if (carrier > 32000)
                                    {
                                        _learningCode.Carrier = (int)(carrier + 0.05 * carrier - 0.666667);
                                        // was: _learningCode.Carrier = (int) (carrier + 0.05*carrier - 32000/48000);
                                    }
                                    else
                                    {
                                        _learningCode.Carrier = (int)carrier;
                                    }
                                }

                                _readThreadMode = ReadThreadMode.LearningDone;
                                break;
                            }

                            if (packetBytes.Length > indexOf9F + 1)
                            {
                                indexOf9F = Array.IndexOf(packetBytes, (byte)0x9F, indexOf9F + 1);
                            }
                            else
                            {
                                _readThreadMode = ReadThreadMode.LearningFailed;
                                break;
                            }
                        }

                        break;
                    }
                    }
                }
            }
            catch (ThreadInterruptedException ex)
            {
                DebugWriteLine(ex.Message);

                if (_readHandle != null)
                {
                    CancelIo(_readHandle);
                }
            }
            catch (Exception ex)
            {
                DebugWriteLine(ex.ToString());

                if (_readHandle != null)
                {
                    CancelIo(_readHandle);
                }
            }
            finally
            {
                if (deviceBufferPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(deviceBufferPtr);
                }

                safeWaitHandle.DangerousRelease();
                waitHandle.Close();
            }

            DebugWriteLine("Read Thread Ended");
        }
示例#4
0
        private void buttonAttemptDecode_Click(object sender, EventArgs e)
        {
            // NOTES:
            // All start with a correct RC6 0xFCA93 header.

            /*
             * int[][] timingData = new int[][]
             * {
             *
             * new int[] { 2800, -750, 550, -350, 500, -400, 500, -800, 550, -800, 1400, -800, 550, -350, 500, -400, 500, -400, 500, -350, 500, -400, 500, -400, 500, -400, 500, -350, 500, -400, 500, -400, 950, -350, 500, -400, 500, -400, 500, -850, 450, -400, 500, -400, 500, -400, 6550, -850, 450, -400, 500, -400, 500, -850, 500, -800, 1400, -850, 500, -400, 450, -400, 500, -400, 500, -400, 500, -400, 450, -400, 500, -400, 500, -400, 500, -2800, 350, -400, 500, -400, 450, -450, 900, -25400 },
             *
             * new int[] { 2800, -750, 550, -350, 550, -350, 500, -800, 550, -800, 1400, -800, 550, -350, 550, -350, 500, -400, 500, -350, 550, -350, 500, -400, 500, -400, 500, -350, 550, -350, 500, -3750, 250, -400, 500, -800, 500, -400, 500, -400, 500, -400, 900, -850, 500, -400, 500, -400, 500, -350, 500, -400, 500, -400, 500, -400, 500, -350, 500, -400, 950, -31550 },
             *
             * new int[] { 2700, -850, 500, -400, 450, -400, 500, -850, 500, -850, 1350, -850, 500, -400, 500, -400, 450, -400, 500, -400, 500, -400, 500, -400, 450, -400, 500, -400, 500, -400, 500, -400, 900, -400, 500, -400, 500, -400, 450, -450, 450, -850, 500, -400, 450, -31750 },
             *
             * new int[] { 2700, -850, 450, -400, 500, -400, 500, -850, 450, -850, 1400, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 1850, -400, 500, -400, 450, -450, 450, -450, 900, -25400 },
             *
             * new int[] { 2700, -850, 450, -400, 500, -400, 450, -900, 450, -900, 1350, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -5350, 450, -400, 500, -850, 450, -450, 450, -400, 500, -400, 900, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -850, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 1100, -300, 450, -450, 450, -450, 450, -400, 500, -400, 900, -25400, },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -850, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -4400, 450, -450, 450, -450, 450, -900, 400, -450, 450, -450, 450, -400, 950, -850, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 900, -450, 450, -450, 450, -450, 450, -400, 500, -850, 5150, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -950, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -25400  },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -3750, 250, -400, 500, -400, 450, -450, 450, -900, 450, -400, 500, -400, 450, -450, 900, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -850, 450, -900, 1350, -900, 400, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -400, 500, -400, 450, -450, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -450, 450, -850, 450, -900, 1350, -900, 400, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -450, 450, -400, 1000, -400, 450, -450, 450, -450, 450, -450, 450, -400, 500, -400, 900, -25400 },
             *
             * new int[] { 2700, -850, 450, -400, 500, -400, 450, -900, 450, -900, 1350, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -3550, 450, -450, 450, -450, 450, -400, 500, -850, 450, -450, 450, -400, 500, -400, 900, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -31750 },
             *
             * new int[] { 2700, -900, 450, -400, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 900, -450, 450, -400, 500, -400, 450, -450, 5300, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -450, 450, -900, 400, -900, 1350, -900, 400, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -12900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 900, -25400 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 450, -900, 450, -900, 1350, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -2500, 650, -400, 450, -450, 450, -450, 450, -400, 500, -850, 450, -450, 450, -400, 500, -400, 900, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -850, 450, -400, 500, -400, 450, -900, 450, -900, 1350, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 950, -400, 450, -450, 450, -450, 450, -400, 4650, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -11950, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 900, -19050 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -850, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 2400, -300, 900, -400, 500, -400, 450, -450, 450, -450, 450, -900, 400, -450, 450, -450, 450, -400, 950, -850, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 4700, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 950, -400, 450, -450, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -900, 450, -400, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 5350, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -1300, 450, -450, 900, -450, 450, -400, 500, -400, 450, -450, 450, -900, 450, -400, 450, -450, 450, -450, 900, -900, 400, -450, 450, -450, 450, -450, 450, -400, 450, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -850, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -400, 500, -400, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -850, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 4700, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -19050 },
             *
             * new int[] { 2700, -900, 450, -400, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, 3350, 350, -900, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -400, 500, -400, 450, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 10650, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -19050 },
             *
             * new int[] { 2700, -900, 450, -400, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 550, -350, 450, -400, 450, -450, 900, -450, 450, -400, 500, -400, 450, -450, 450, -900, 450, -400, 450, -450, 450, -450, 900, -900, 450, -400, 450, -450, 450, -450, 4700, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -900, 400, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 900, -400, 500, -400, 450, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 500, -400, 450, -900, 450, -900, 1300, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 10650, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 900, -19050 },
             *
             * new int[] { 2700, -900, 450, -400, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 900, -450, 450, -450, 450, -400, 500, -400, 450, -900, 450, -400, 500, -400, 450, -450, 900, -900, 450, -400, 450, -450, 450, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -450, 450, -400, 3350, -31750 },
             *
             * new int[] { 2650, -900, 450, -400, 450, -450, 450, -900, 450, -900, 1300, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, 2400, 50, -450, 900, -900, 450, -400, 450, -450, 450, -450, 450, -450, 450, -400, 450, -450, 450, -450, 450, -450, 900, -19050 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -450, 450, -400, 500, -400, 450, -450, 450, -900, 450, -400, 450, -450, 450, -450, 900, -900, 400, -450, 450, -450, 4300, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 450, -850, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -450, 3600, -31750 },
             *
             * new int[] { 2700, -850, 450, -450, 450, -400, 500, -850, 450, -900, 1350, -850, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 2300, -200, 450, -400, 900, -900, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -19050 },
             *
             * new int[] { 2650, -900, 450, -400, 450, -450, 450, -900, 450, -900, 1300, -900, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 900, -450, 450, -450, 450, -400, 500, -400, 450, -900, 450, -400, 500, -400, 450, -450, 900, -900, 450, -400, 4650, -31750 },
             *
             * new int[] { 2700, -900, 400, -450, 450, -450, 450, -900, 400, -900, 1350, -900, 450, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 450, -450, 450, -400, 500, -400, 450, -450, 900, -3500, 550, -25400 },
             *
             * };
             *
             * for (int index = 0; index < timingData.GetLength(0); index++)
             * {
             * IrDecoder.DecodeIR(timingData[index], new RemoteCallback(RemoteEvent), new KeyboardCallback(KeyboardEvent), new MouseCallback(MouseEvent));
             *
             * //IrCode newCode = new IrCode(timingData[index]);
             * //Pronto.WriteProntoFile(String.Format("C:\\{0}.ir", index), Pronto.ConvertIrCodeToProntoRaw(newCode));
             * }
             */
            IrDecoder.DecodeIR(_code.TimingData, RemoteEvent, KeyboardEvent, MouseEvent);
        }