protected internal override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventType, CancellationToken cancellationToken)
 {
     throw new PlatformNotSupportedException();
 }
Beispiel #2
0
 protected internal override ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventType, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
 public ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
 {
     return(_controller.WaitForEventAsync(pinNumber, eventTypes, timeout));
 }
 public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
 {
     throw new NotImplementedException();
 }
 public CallbackContainer(int pinNumber, PinEventTypes eventTypes)
 {
     PinNumber  = pinNumber;
     EventTypes = eventTypes;
 }
 public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, TimeSpan timeout)
 {
     return(_controller.WaitForEvent(pinNumber, eventTypes, timeout));
 }
Beispiel #7
0
 internal KeyMatrixEvent(PinEventTypes eventType, int output, int input)
 {
     EventType = eventType;
     Output    = output;
     Input     = input;
 }
 /// <summary>
 /// Adds a handler for a pin value changed event.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="callback">Delegate that defines the structure for callbacks when a pin value changed event occurs.</param>
 protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventType, PinChangeEventHandler callback)
 {
     throw new NotImplementedException();
 }
Beispiel #9
0
        /// <inheritdoc/>
        protected override ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
        {
            _pinModes[pinNumber].InUseByInterruptDriver = true;

            base.OpenPin(pinNumber);
            return(base.WaitForEventAsync(pinNumber, eventTypes, cancellationToken));
        }
 /// <inheritdoc/>
 protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback) => _internalDriver.AddCallbackForPinValueChangedEvent(pinNumber, eventTypes, callback);
Beispiel #11
0
 /// <summary>
 /// Async call until an event of type eventType is received or a cancellation is requested.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="token">The cancellation token of when the operation should stop waiting for an event.</param>
 /// <returns>A task representing the operation of getting the structure that contains the result of the waiting operation</returns>
 protected internal virtual ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
 {
     return(new ValueTask <WaitForEventResult>(Task.Run(() => WaitForEvent(pinNumber, eventTypes, cancellationToken))));
 }
Beispiel #12
0
 /// <summary>
 /// Blocks execution until an event of type eventType is received or a cancellation is requested.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="cancellationToken">The cancellation token of when the operation should stop waiting for an event.</param>
 /// <returns>A structure that contains the result of the waiting operation.</returns>
 protected internal abstract WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken);
Beispiel #13
0
 /// <summary>
 /// Adds a handler for a pin value changed event.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="callback">Delegate that defines the structure for callbacks when a pin value changed event occurs.</param>
 protected internal abstract void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback);
Beispiel #14
0
        /// <summary>
        /// Async call until an event of type eventType is received or a cancellation is requested.
        /// </summary>
        /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
        /// <param name="eventTypes">The event types to wait for.</param>
        /// <param name="cancellationToken">The cancellation token of when the operation should stop waiting for an event.</param>
        /// <returns>A task representing the operation of getting the structure that contains the result of the waiting operation</returns>
        protected internal override ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
        {
            ValidatePinNumber(pinNumber);
            InitializeSysFS();

            _sysFSDriver.OpenPin(pinNumber);
            _sysFSDriver.SetPinMode(pinNumber, GetModeForUnixDriver(_sysFSModes[pinNumber]));

            return(_sysFSDriver.WaitForEventAsync(pinNumber, eventTypes, cancellationToken));
        }
Beispiel #15
0
 /// <summary>
 /// Adds a handler for a pin value changed event.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="callback">Delegate that defines the structure for callbacks when a pin value changed event occurs.</param>
 protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
 {
     if (!_devicePins.ContainsKey(pinNumber))
     {
         _devicePins.Add(pinNumber, new UnixDriverDevicePin());
         _pinsToDetectEventsCount++;
         AddPinToPoll(pinNumber, ref _devicePins[pinNumber].FileDescriptor, ref _pollFileDescriptor, out _);
     }
     if (eventTypes.HasFlag(PinEventTypes.Rising))
     {
         _devicePins[pinNumber].ValueRising += callback;
     }
     if (eventTypes.HasFlag(PinEventTypes.Falling))
     {
         _devicePins[pinNumber].ValueFalling += callback;
     }
     SetPinEventsToDetect(pinNumber, (GetPinEventsToDetect(pinNumber) | eventTypes));
     InitializeEventDetectionThread();
 }
Beispiel #16
0
        /// <summary>
        /// Adds a handler for a pin value changed event.
        /// </summary>
        /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
        /// <param name="eventTypes">The event types to wait for.</param>
        /// <param name="callback">Delegate that defines the structure for callbacks when a pin value changed event occurs.</param>
        protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
        {
            ValidatePinNumber(pinNumber);
            InitializeSysFS();

            _sysFSDriver.OpenPin(pinNumber);
            _sysFSDriver.SetPinMode(pinNumber, GetModeForUnixDriver(_sysFSModes[pinNumber]));

            _sysFSDriver.AddCallbackForPinValueChangedEvent(pinNumber, eventTypes, callback);
        }
Beispiel #17
0
 public static IDisposable RegisterCallbackForPinValueChangedEventAsDisposable(this IGpioController controller, int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
 {
     controller.RegisterCallbackForPinValueChangedEvent(pinNumber, eventTypes, callback);
     return(Disposable.Create(() => controller.UnregisterCallbackForPinValueChangedEvent(pinNumber, callback)));
 }
Beispiel #18
0
 /// <summary>
 /// Blocks execution until an event of type eventType is received or a cancellation is requested.
 /// </summary>
 /// <param name="pinNumber">The pin number in the driver's logical numbering scheme.</param>
 /// <param name="eventTypes">The event types to wait for.</param>
 /// <param name="cancellationToken">The cancellation token of when the operation should stop waiting for an event.</param>
 /// <returns>A structure that contains the result of the waiting operation.</returns>
 protected internal override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
 /// <inheritdoc />
 protected internal override void AddCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
 {
     throw new PlatformNotSupportedException();
 }
 public void RegisterCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, PinChangeEventHandler callback)
 {
     _controller.RegisterCallbackForPinValueChangedEvent(pinNumber, eventTypes, callback);
 }
Beispiel #21
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;
            }
        }
 public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
 {
     return(_controller.WaitForEvent(pinNumber, eventTypes, cancellationToken));
 }
 public void RegisterCallbackForPinValueChangedEvent(byte pin, PinEventTypes rising, PinChangeEventHandler e)
 => _controller.RegisterCallbackForPinValueChangedEvent(pin, rising, e);
 public ValueTask <WaitForEventResult> WaitForEventAsync(int pinNumber, PinEventTypes eventTypes, CancellationToken token)
 {
     return(_controller.WaitForEventAsync(pinNumber, eventTypes, token));
 }
Beispiel #25
0
 public void RegisterCallbackForPinValueChangedEvent(int pinNumber, PinEventTypes eventTypes, Action <PinValueChangedEventArgs> callback)
 {
     _gpioController.RegisterCallbackForPinValueChangedEvent(pinNumber, eventTypes, (sender, pinValueChangedEventArgs) => callback?.Invoke(pinValueChangedEventArgs));
 }
 public WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
Beispiel #27
0
        /// <inheritdoc/>
        protected override WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                ushort queueSize;
                var    ftStatus = FtFunction.FT4222_GPIO_GetTriggerStatus(_ftHandle, (GpioPort)pinNumber, out queueSize);
                if (ftStatus != FtStatus.Ok)
                {
                    throw new IOException($"Can't get trigger status, error {ftStatus}");
                }

                if (queueSize > 0)
                {
                    Span <GpioTrigger> gpioTriggers = stackalloc GpioTrigger[queueSize];
                    ushort             readTrigger;
                    ftStatus = FtFunction.FT4222_GPIO_ReadTriggerQueue(_ftHandle, (GpioPort)pinNumber, in MemoryMarshal.GetReference(gpioTriggers), queueSize, out readTrigger);
                    if (ftStatus != FtStatus.Ok)
                    {
                        throw new IOException($"Can't read trigger status, error {ftStatus}");
                    }

                    switch (eventTypes)
                    {
                    case PinEventTypes.Rising:
                        if (_gpioTriggers[pinNumber].HasFlag(GpioTrigger.Rising))
                        {
                            if (gpioTriggers.ToArray().Where(m => m == GpioTrigger.Rising).Count() > 0)
                            {
                                return(new WaitForEventResult()
                                {
                                    EventTypes = PinEventTypes.Rising,
                                    TimedOut = false
                                });
                            }
                        }
                        break;

                    case PinEventTypes.Falling:
                        if (_gpioTriggers[pinNumber].HasFlag(GpioTrigger.Falling))
                        {
                            if (gpioTriggers.ToArray().Where(m => m == GpioTrigger.Falling).Count() > 0)
                            {
                                return(new WaitForEventResult()
                                {
                                    EventTypes = PinEventTypes.Falling,
                                    TimedOut = false
                                });
                            }
                        }
                        break;

                    case PinEventTypes.None:
                    default:
                        break;
                    }
                }
            }

            return(new WaitForEventResult()
            {
                EventTypes = PinEventTypes.None,
                TimedOut = true
            });
        }