Exemple #1
0
        private async Task <TimeSpan> PlayButton(bool fakeOut)
        {
            int waitMs             = _r.Next(1500, 6000);
            WaitForEventResult pre = await _controller.WaitForEventAsync(_buttonPin, PinEventTypes.Falling,
                                                                         TimeSpan.FromMilliseconds(waitMs));

            if (!pre.TimedOut)
            {
                return(TimeSpan.FromSeconds(-1));
            }

            if (fakeOut)
            {
                return(await ShowFakeout());
            }
            else
            {
                return(await ShowReal());
            }
        }
Exemple #2
0
        public void WaitForEventAsyncFail()
        {
            var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object);

            _mockedGpioDriver.Setup(x => x.OpenPinEx(1));
            _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Input)).Returns(true);
            _mockedGpioDriver.Setup(x => x.WaitForEventEx(1, PinEventTypes.Rising | PinEventTypes.Falling, It.IsAny <CancellationToken>()))
            .Returns(new WaitForEventResult()
            {
                EventTypes = PinEventTypes.None, TimedOut = true
            });
            Assert.NotNull(ctrl);
            ctrl.OpenPin(1, PinMode.Input);

            var task = ctrl.WaitForEventAsync(1, PinEventTypes.Falling | PinEventTypes.Rising, TimeSpan.FromSeconds(0.01)).AsTask();

            task.Wait(CancellationToken.None);
            Assert.True(task.IsCompleted);
            Assert.Null(task.Exception);
            Assert.True(task.Result.TimedOut);
            Assert.Equal(PinEventTypes.None, task.Result.EventTypes);
        }
Exemple #3
0
        /// <summary>Executes the command asynchronously.</summary>
        /// <returns>The command's exit code.</returns>
        /// <remarks>
        ///     NOTE: This test app uses the base class's <see cref="GpioCommand.CreateGpioController"/> method to create a device.<br/>
        ///     Real-world usage would simply create an instance of <see cref="GpioController"/>:
        ///     <code>using (var controller = new GpioController())</code>
        /// </remarks>
        public async Task <int> ExecuteAsync()
        {
            if (LedPin != null)
            {
                Console.WriteLine($"Driver={Driver}, Scheme={Scheme}, ButtonPin={ButtonPin}, LedPin={LedPin}, PressedValue={PressedValue}, OnValue={OnValue}");
            }
            else
            {
                Console.WriteLine($"Driver={Driver}, Scheme={Scheme}, ButtonPin={ButtonPin}, PressedValue={PressedValue}, OnValue={OnValue}");
            }

            using GpioController controller = CreateGpioController();
            using CancellationTokenSource cancellationTokenSource = new();
            Console.WriteLine($"Listening for button presses on GPIO {Enum.GetName(typeof(PinNumberingScheme), Scheme)} pin {ButtonPin} . . .");

            // This example runs until Ctrl+C (or Ctrl+Break) is pressed, so register a local function handler.
            Console.CancelKeyPress += Console_CancelKeyPress;
            controller.OpenPin(ButtonPin);

            // Set the mode based on if input pull-up resistors are supported.
            PinMode inputMode = controller.IsPinModeSupported(ButtonPin, PinMode.InputPullUp) ? PinMode.InputPullUp : PinMode.Input;

            controller.SetPinMode(ButtonPin, inputMode);

            // Open the GPIO pin connected to the LED if one was specified.
            if (LedPin != null)
            {
                controller.OpenPin(LedPin.Value, PinMode.Output);
                controller.Write(LedPin.Value, OffValue);
            }

            PinEventTypes      bothPinEventTypes = PinEventTypes.Falling | PinEventTypes.Rising;
            WaitForEventResult waitResult;
            int count = 0;

            do
            {
                waitResult = await controller.WaitForEventAsync(ButtonPin, bothPinEventTypes, cancellationTokenSource.Token);

                if (!waitResult.TimedOut)
                {
                    var pressedOrReleased = waitResult.EventTypes == PressedValue ? "pressed" : "released";
                    Console.WriteLine($"[{count++}] Button {pressedOrReleased}: GPIO {Enum.GetName(typeof(PinNumberingScheme), Scheme)} pin number {ButtonPin}, ChangeType={waitResult.EventTypes}");

                    if (LedPin != null)
                    {
                        PinValue ledValue = waitResult.EventTypes == PressedValue ? OnValue : OffValue;
                        controller.Write(LedPin.Value, ledValue);
                    }
                }
            } while (!waitResult.TimedOut);

            controller.ClosePin(ButtonPin);
            if (LedPin != null)
            {
                controller.ClosePin(LedPin.Value);
            }

            Console.WriteLine("Operation cancelled. Exiting.");
            Console.OpenStandardOutput().Flush();

            return(0);

            // Local function
            void Console_CancelKeyPress(object?sender, ConsoleCancelEventArgs e)
            {
                e.Cancel = true;
                cancellationTokenSource.Cancel();
                Console.CancelKeyPress -= Console_CancelKeyPress;
            }
        }
Exemple #4
0
 public ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
 {
     return(_controller.WaitForEventAsync(pinNumber, eventTypes, timeout));
 }