internal override bool SetGpioValue(int pin, GpioPinMode mode) { if (!PinController.IsValidPin(pin) || !IsDriverInitialized) { return(false); } try { if (DriverController == null) { return(false); } if (!DriverController.IsPinModeSupported(pin, (PinMode)mode)) { return(false); } if (!DriverController.IsPinOpen(pin)) { DriverController.OpenPin(pin); } if (!DriverController.IsPinOpen(pin)) { return(false); } DriverController.SetPinMode(pin, (PinMode)mode); return(true); } finally { ClosePin(pin); } }
public void IsPinModeSupported() { _mockedGpioDriver.Setup(x => x.IsPinModeSupportedEx(1, PinMode.Input)).Returns(true); var ctrl = new GpioController(PinNumberingScheme.Logical, _mockedGpioDriver.Object); Assert.NotNull(ctrl); Assert.True(ctrl.IsPinModeSupported(1, PinMode.Input)); }
public static void RunAndDisplay() { using var controller = new GpioController(PinNumberingScheme.Logical); for (int i = 0; i < controller.PinCount; i++) { Console.WriteLine(string.Concat( $"Pin {i:000}", $" | Input = {controller.IsPinModeSupported(i, PinMode.Input)}", $" | InputPullDown = {controller.IsPinModeSupported(i, PinMode.InputPullDown)}", $" | InputPullUp = {controller.IsPinModeSupported(i, PinMode.InputPullUp)}", $" | Output = {controller.IsPinModeSupported(i, PinMode.Output)}" )); } // Quick and simple way to find a thermometer and print the temperature Console.WriteLine("Enumerating temperature sensors..."); foreach (var dev in OneWireThermometerDevice.EnumerateDevices()) { var temperature = dev.ReadTemperature().Celsius; Console.WriteLine($"Temperature reported by '{dev.DeviceId}': {temperature}°C"); } }
public void AttachToPin(int pin) { _logger.LogDebug($"Open pin {pin}!"); if (!_controller.IsPinModeSupported(pin, PinMode.Input)) { _logger.LogError($"Pin {pin} do not support pin mode!"); return; } _controller.OpenPin(pin, PinMode.Input); if (!_controller.IsPinOpen(pin)) { _logger.LogError($"Pin {pin} do not open!"); return; } _logger.LogDebug($"Attach to pin {pin}!"); _controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Rising, OnPinValueChanged); _logger.LogDebug($"Registered pin {pin} rising!"); _controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Falling, OnPinValueChanged); _logger.LogDebug($"Registered pin {pin} falling!"); }
/// <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="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 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 (var cancelEvent = new ManualResetEvent(false)) { int count = 0; 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); } // Set the event handler for changes to the pin value. PinEventTypes bothPinEventTypes = PinEventTypes.Falling | PinEventTypes.Rising; controller.RegisterCallbackForPinValueChangedEvent(ButtonPin, bothPinEventTypes, valueChangeHandler); // Wait for the cancel (Ctrl+C) console event. cancelEvent.WaitOne(); controller.ClosePin(ButtonPin); if (LedPin != null) { controller.ClosePin(LedPin.Value); } // Unregister the event handler for changes to the pin value controller.UnregisterCallbackForPinValueChangedEvent(ButtonPin, valueChangeHandler); Console.WriteLine("Operation cancelled. Exiting."); Console.OpenStandardOutput().Flush(); return(Task.FromResult(0)); // Declare a local function to handle the pin value changed events. void valueChangeHandler(object sender, PinValueChangedEventArgs pinValueChangedEventArgs) { if (LedPin != null) { PinValue ledValue = pinValueChangedEventArgs.ChangeType == PressedValue ? OnValue : OffValue; controller.Write(LedPin.Value, ledValue); } var pressedOrReleased = pinValueChangedEventArgs.ChangeType == PressedValue ? "pressed" : "released"; Console.WriteLine($"[{count++}] Button {pressedOrReleased}: logicalPinNumber={pinValueChangedEventArgs.PinNumber}, ChangeType={pinValueChangedEventArgs.ChangeType}"); } // Local function void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; cancelEvent.Set(); Console.CancelKeyPress -= Console_CancelKeyPress; } } } }
/// <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 bool IsPinModeSupported(int pinNumber, PinMode mode) { return(_controller.IsPinModeSupported(pinNumber, mode)); }