コード例 #1
0
        /// <summary>
        /// Tests the infrared sensor HX1838.
        /// </summary>
        public static void TestInfraredSensor()
        {
            Console.Clear();
            "Send a signal...".Info("IR");
            var inputPin = Pi.Gpio[BcmPin.Gpio25]; // BCM Pin 25 or Physical pin 22 on the right side of the header.
            var sensor   = new InfraredSensor(inputPin, true);

            sensor.DataAvailable += (s, e) =>
            {
                Console.Clear();
                var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses);
                if (necData != null)
                {
                    $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12}    Pulses: {e.Pulses.Length,4}    Duration(us): {e.TrainDurationUsecs,6}    Reason: {e.FlushReason}".Warn("IR");

                    if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses))
                    {
                        return;
                    }
                }
                else
                {
                    if (e.Pulses.Length >= 4)
                    {
                        var debugData = InfraredSensor.DebugPulses(e.Pulses);
                        $"RX    Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Warn("IR");
                        debugData.Info("IR");
                    }
                    else
                    {
                        $"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Error("IR");
                    }
                }

                ExitMessage.WriteLine();
            };

            while (true)
            {
                var input = Console.ReadKey(true).Key;
                if (input != ConsoleKey.Escape)
                {
                    continue;
                }

                break;
            }

            sensor.Dispose();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Jo0/raspberryio
        /// <summary>
        /// Tests the infrared sensor HX1838.
        /// </summary>
        public static void TestInfraredSensor()
        {
            var inputPin = Pi.Gpio[P1.Gpio23]; // BCM Pin 23 or Physical pin 16 on the right side of the header.
            var sensor   = new InfraredSensor(inputPin, true);
            var emitter  = new InfraredEmitter(Pi.Gpio[P1.Gpio18]);

            sensor.DataAvailable += (s, e) =>
            {
                var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses);
                if (necData != null)
                {
                    $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12}    Pulses: {e.Pulses.Length,4}    Duration(us): {e.TrainDurationUsecs,6}    Reason: {e.FlushReason}".Warn("IR");

                    if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses))
                    {
                        return;
                    }

                    // Test repeater signal
                    var outputPulses = InfraredEmitter.NecEncoder.Encode(necData);

                    emitter.Send(outputPulses);
                    var debugData = InfraredSensor.DebugPulses(outputPulses);
                    $"TX       Length: {outputPulses.Length,5}".Warn("IR");
                    debugData.Info("IR");
                }
                else
                {
                    if (e.Pulses.Length >= 4)
                    {
                        var debugData = InfraredSensor.DebugPulses(e.Pulses);
                        $"RX    Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Warn("IR");
                        debugData.Info("IR");
                    }
                    else
                    {
                        $"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}".Error("IR");
                    }
                }
            };

            Console.ReadLine();
            sensor.Dispose();
        }
コード例 #3
0
        public async Task <IActionResult> Start(double speed = 0.8)
        {
            if (ishw == "stop")
            {
                var inputPin = Pi.Gpio[BcmPin.Gpio25]; // BCM Pin 25 or Physical pin 22 on the right side of the header.
                sensor = new InfraredSensor(inputPin, true);
                sensor.DataAvailable += async(s, e) =>
                {
                    Console.Clear();
                    var necData = InfraredSensor.NecDecoder.DecodePulses(e.Pulses);
                    if (necData != null)
                    {
                        var decdata = BitConverter.ToString(necData);
                        Console.WriteLine($"NEC Data: {decdata.Replace("-", " "),12}    Pulses: {e.Pulses.Length,4}    Duration(us): {e.TrainDurationUsecs,6}    Reason: {e.FlushReason}");

                        #region 遥控器控制小车
                        switch (decdata)
                        {
                        case "DD-22-0B-F4":
                            _l298n.Move(1, speed);
                            L298NController.InitFX();
                            L298NController.isup = "start";
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "42", "前进.");

                            break;

                        case "DD-22-0D-F2":
                            _l298n.Move(2, speed);
                            L298NController.InitFX();
                            L298NController.isdown = "start";
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "43", "后退.");

                            break;

                        case "DD-22-0E-F1":
                            _l298n.Move(3, speed);
                            L298NController.InitFX();
                            L298NController.isleft = "start";
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "44", "左转.");

                            break;

                        case "DD-22-0C-F3":
                            _l298n.Move(4, speed);
                            L298NController.InitFX();
                            L298NController.isright = "start";
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "45", "右转.");

                            break;

                        case "DD-22-0A-F5":
                            _l298n.Move(5, 0);
                            L298NController.InitFX();
                            L298NController.ispause = "start";
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "46", "暂停.");

                            break;
                        }
                        await _chatHub.Clients.All.SendAsync("ReceiveMessage", "3", $"NEC Data: {BitConverter.ToString(necData).Replace("-", " "),12}");

                        #endregion

                        if (InfraredSensor.NecDecoder.IsRepeatCode(e.Pulses))
                        {
                            return;
                        }
                    }
                    else
                    {
                        if (e.Pulses.Length >= 4)
                        {
                            var debugData = InfraredSensor.DebugPulses(e.Pulses);
                            //Console.WriteLine($"RX    Length: {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}");
                            //Console.WriteLine($"Debug data: {debugData}");
                            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "3", $"Debug data: {debugData}");
                        }
                        else
                        {
                            //Console.WriteLine($"RX (Garbage): {e.Pulses.Length,5}; Duration: {e.TrainDurationUsecs,7}; Reason: {e.FlushReason}");
                        }
                    }
                };
            }
            ishw = "start";
            await _chatHub.Clients.All.SendAsync("ReceiveMessage", "70", "红外遥控器开启");

            return(Content("红外遥控器开启"));
        }