示例#1
0
        public void Reset()
        {
            pulseHigh = false;
            databits  = 0;
            length    = 0;
            pulseLengths.Clear();
            receiving       = false;
            data            = 0;
            commandReceived = IrCommandType.IrNoCommand;
            bitType         = IrBitType.IR_INVALID;

            Debug.WriteLine("IR detection reset");
        }
示例#2
0
        public IrCommandType AnalyzeData(List <double> points)
        {
            for (var i = 0; i < points.Count; i++)
            {
                if (points[i] > 0.8)
                {
                    // Rising edge starts new pulse
                    if (!pulseHigh)
                    {
                        // Handle current pulse
                        if (length > 0)
                        {
                            //var len = length;
                            //pulseLengths.Add(length * IR_SAMPLING_PERIOD_US);
                            //Debug.WriteLine("Pulse: " + len);
                            bitType = GetBitType(length);
                            //Debug.WriteLine(" (" + bitType + ")");

                            if (receiving)
                            {
                                if (bitType == IrBitType.IR_DATA_BIT_ONE)
                                {
                                    try
                                    {
                                        data |= (long)1 << databits;
                                        databits++;
                                    }
                                    catch (Exception e) {
                                        Debug.WriteLine(e.Message);
                                    }
                                }
                                else if (bitType == IrBitType.IR_DATA_BIT_ZERO)
                                {
                                    databits++;
                                }
                                else
                                {
                                    //Log.Files.Debug($"Idle: {len}");
                                    //Log.Files.Debug($"Index: {i}");
                                    //receiving = false;
                                    // Invalid bit received --> clear all and start over
                                    Reset();
                                    continue;
                                }

                                if (receiving && databits == 32)
                                {
                                    foreach (var item in pulseLengths)
                                    {
                                        Debug.WriteLine(item);
                                    }
                                    receiving = false;
                                    Debug.WriteLine("Data: 0x" + data.ToString("X2"));
                                    if ((data & 0xFFFFFFFF) == IR_TV_VOL_UP)
                                    {
                                        //lblCount.Text = "UP";
                                        commandReceived = IrCommandType.IrTvVolUp;
                                        //SendKeys.SendWait("{UP}");
                                        Debug.WriteLine(commandReceived);
                                    }
                                    else if ((data & 0xFFFFFFFF) == IR_TV_VOL_DOWN)
                                    {
                                        //lblCount.Text = "DOWN";
                                        commandReceived = IrCommandType.IrTvVolDown;
                                        //SendKeys.SendWait("{DOWN}");
                                        Debug.WriteLine(commandReceived);
                                    }
                                    else if ((data & 0xFFFFFFFF) == IR_TV_VOL_MUTE)
                                    {
                                        //lblCount.Text = "MUTE";
                                        commandReceived = IrCommandType.IrTvVolMute;
                                        //SendKeys.SendWait("{DOWN}");
                                        Debug.WriteLine(commandReceived);
                                    }
                                }
                            }

                            // Start pulse
                            if (bitType == IrBitType.IR_START_BIT)
                            {
                                //Debug.WriteLine($"Start: {i}");
                                receiving = true;
                                data      = 0;
                                databits  = 0;
                            }
                        }
                        pulseHigh = true;
                        length    = 0;
                    }
                    else
                    {
                        length++;
                    }
                }
                else
                {
                    if (pulseHigh)
                    {
                        pulseHigh = false;
                    }
                    if (length > 0)
                    {
                        length++;
                    }
                }
            }

            return(commandReceived);
        }