示例#1
0
        static void TestTimeoutWaitAsync()
        {
            Console.WriteLine("Testing async wait with a timeout of 10 seconds");
            var button = Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp);
            // Request a 10 second task to wait for the button pin rising edge
            var task  = button.WaitForEdgeAsync(PinEdge.Rising, 10000);
            var i     = 1;
            var timer = PreciseTimer.Every(1000, () =>
            {
                if (task.IsCompleted)
                {
                    return(false);
                }
                Console.WriteLine(i++);
                return(i < 10 && !task.IsCompleted);
            });

            // Check if the task detected a rising edge before timeout was reached
            if (task.Result)
            {
                Console.WriteLine("Rising edge found");
            }
            else
            {
                Console.WriteLine("Rising edge timed out after 10 seconds");
            }
            timer.Wait();
        }
示例#2
0
        static void TestTimeoutWait()
        {
            Console.WriteLine("Testing wait with a timeout of 10 seconds");
            var button = Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp);
            var done   = false;
            var i      = 1;
            var timer  = PreciseTimer.Every(1000, () =>
            {
                if (done)
                {
                    return(false);
                }
                Console.WriteLine(i++);
                return(i < 10 && !done);
            });

            // Wait 10 seconds for the button pin rising edge
            if (button.WaitForEdge(PinEdge.Rising, 10000))
            {
                Console.WriteLine("Rising edge found");
            }
            else
            {
                Console.WriteLine("Rising edge timed out after 10 seconds");
            }
            done = true;
            timer.Wait();
        }
示例#3
0
        public static void TimerEveryTest()
        {
            var  a       = new PreciseTimer();
            long i       = 0;
            var  running = true;
            var  every   = PreciseTimer.Every(1, () =>
            {
                i++;
                if (i % 1000 == 0)
                {
                    Console.WriteLine("elapsed = {0:0.000}, i = {1}", a.ElapsedMilliseconds, i);
                }
                return(running);
            });

            Console.ReadLine();
            running = false;
            every.Wait();
        }
示例#4
0
        static void TestSweep()
        {
            Console.WriteLine("Testing a servo motor in a sweeping motion");
            var angle = 0d;
            var step  = 0.1d;
            var done  = false;

            using (var left = Pi.Gpio.Pin(leftPin, PinKind.InputPullUp))
                using (var right = Pi.Gpio.Pin(rightPin, PinKind.InputPullUp))
                    using (var servo = new ServoMotor(maxAngle))
                    {
                        servo.PulseWidths(0.5, 2.5);
                        servo.Start();
                        var timer = PreciseTimer.Every(1, () =>
                        {
                            if (done)
                            {
                                return(false);
                            }
                            angle += step;
                            var a  = angle % (maxAngle * 2);
                            if (a > maxAngle)
                            {
                                a = (maxAngle * 2) - a;
                            }
                            servo.Angle = a;
                            return(true);
                        });
                        Console.WriteLine("Click the button to stop this test");
                        // Connect rotate and click events
                        left.OnRisingEdge += Rotate;
                        Pi.Gpio.Pin(buttonPin, PinKind.InputPullUp).WaitForEdge(PinEdge.Rising);
                        left.OnRisingEdge -= Rotate;
                        done = true;
                        timer.Wait();
                        servo.Angle = 0;
                        Pi.Wait(2000);

                        // Called on the falling edge of the left pin
                        void Rotate(object sender, PinEventHandlerArgs args)
                        {
                            if (args.Bounced)
                            {
                                return;
                            }
                            var s = step;

                            if (left.Value == right.Value)
                            {
                                s += 0.1;
                            }
                            else
                            {
                                s -= 0.1;
                            }
                            if (s < 0)
                            {
                                s = 0;
                            }
                            step = s;
                            Console.WriteLine("servo step {0:0.0}°", step);
                        }
                    }
        }