public void Dispose() { if (_disposed) { throw new ObjectDisposedException("Selected pin has already been disposed."); } WriteToPi(GPIO_ROOT_DIR + "unexport", GPIOPin.ToString().Substring(4)); _exported_pins.Remove(this.GPIOPin); _disposed = true; }
/// <summary> /// Sets the GPIO pin low (CLEAR) /// </summary> /// <param name="pin">GPIO Pin ID</param> public void ClearBit(GPIOPin pin) { try { this.DeviceIoControl(IOCTL_GPIO_CLRBIT, SerializeToByteArray(pin)); } catch (Exception) { throw new Exception("Unable to complete ClearBit DeviceIoControl:" + Marshal.GetLastWin32Error()); } }
/// <summary> /// Sets the GPIO direction /// </summary> /// <param name="pin">GPIO Pin</param> /// <param name="dir">Direction</param> public void SetDirection(GPIOPin pin, Direction dir) { try { PinDirection pd = new PinDirection(pin, dir); this.DeviceIoControl(IOCTL_GPIO_SETMODE, SerializeToByteArray(pd)); } catch (Exception) { throw new Exception("Unable to complete SetDirection DeviceIoControl:" + Marshal.GetLastWin32Error()); } }
static void HandleSpeedButtonRelease(GPIOPin pin, Edge edge) { try { Speed = Speeds[++SpeedIndex]; } catch { SpeedIndex = 0; Speed = Speeds[SpeedIndex]; } Console.WriteLine($"Speed changed to {Speed}"); pin.WaitForEdge(edge, HandleSpeedButtonRelease); }
public PinDirection(GPIOPin gpioId, Direction dir) { this.gpioId = (UInt32)gpioId; if (dir == Direction.Input) { this.direction = 1; } else { this.direction = 0; } }
static void HandleModeButtonRelease(GPIOPin pin, Edge edge) { var newValue = Mode + 1; if (Enum.IsDefined(typeof(LedMode), newValue)) { Mode = newValue; } else { Mode = LedMode.LTR; } Console.WriteLine($"Mode changed to {Mode}"); pin.WaitForEdge(edge, HandleModeButtonRelease); }
/// <summary> /// Get the status of a GPIO pin /// </summary> /// <param name="pin">GPIO pin ID</param> /// <returns></returns> public bool GetBit(GPIOPin pin) { try { byte [] data = new byte[4]; this.DeviceIoControl(IOCTL_GPIO_GETBIT, SerializeToByteArray(pin), data); if (1 == data[0]) { return(true); } else { return(false); } } catch (Exception) { throw new Exception("Unable to complete GetBit DeviceIoControl:" + Marshal.GetLastWin32Error()); } }
public InitInterruptInfo(GPIOPin gpioId, UInt32 hEvent) { this.gpioId = (UInt32)gpioId; this.hEvent = hEvent; }
public EnableWakeIn(GPIOPin gpioId, bool enable) { this.gpioId = (UInt32)gpioId; this.enable = enable; }
public InterruptMask(GPIOPin gpioId, bool enable) { this.gpioId = (UInt32)gpioId; this.enable = enable; }
public DebounceTime(GPIOPin gpioId, UInt32 debounceTime) { this.gpioId = (UInt32)gpioId; this.debounceTime = debounceTime; }
static void Main(string[] args) { /* * This example simulates a cursor keyboard. * It's made with RB2 & 3 in mind. The pinout used is * * * o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o * x o o o o o o o o o o o o o o o o o o o o o o o o o o o o o * 1 2 3 3 3 3 3 * 9 1 3 5 7 9 * | | | | | | * s r l d u g * p i e o p n * a g f w d * c h t n * e t * * As you can see the schematics uses GND for the inputs, so we * must configure the pull control to pull-up. Also, as we are * using pulled up inputs the signal must be reversed, we will * have false (or zero) on the input when the button is pressed * and true (or one) when the button is released. * * Remember to execute it with root privileges! */ //Remember always to initialize the chip!! BCM2835.BCM2835Managed.bcm2835_init(); //Create new pin GPIOPin up = new GPIOPin(BCM2835.BCM2835Managed.RPiGPIOPin.RPI_V2_GPIO_P1_37); //Configure it as input up.Function = GPIOFunction.Input; //Enable pull-ups up.PullControl = GPIOPullControl.PullUp; //Detect both edges up.Edge = Edge.Both; //Hook to the event detector up.EventDetected += Up_EventDetected; GPIOPin down = new GPIOPin(BCM2835.BCM2835Managed.RPiGPIOPin.RPI_V2_GPIO_P1_35); down.Function = GPIOFunction.Input; down.PullControl = GPIOPullControl.PullUp; down.Edge = Edge.Both; down.EventDetected += Down_EventDetected; GPIOPin left = new GPIOPin(BCM2835.BCM2835Managed.RPiGPIOPin.RPI_V2_GPIO_P1_33); left.Function = GPIOFunction.Input; left.PullControl = GPIOPullControl.PullUp; left.Edge = Edge.Both; left.EventDetected += Left_EventDetected; GPIOPin right = new GPIOPin(BCM2835.BCM2835Managed.RPiGPIOPin.RPI_V2_GPIO_P1_31); right.Function = GPIOFunction.Input; right.PullControl = GPIOPullControl.PullUp; right.Edge = Edge.Both; right.EventDetected += Right_EventDetected; GPIOPin button = new GPIOPin(BCM2835.BCM2835Managed.RPiGPIOPin.RPI_V2_GPIO_P1_29); button.Function = GPIOFunction.Input; button.PullControl = GPIOPullControl.PullUp; button.Edge = Edge.Both; button.EventDetected += Button_EventDetected; /* * Keyb simulator is a nifty class I created to simulate a physical keyboard * You can run it in a SSH console and the generated input will be consumed by the * session existing on the physical machine, not by the SSH session. */ KeybSimulator.Init(); Console.WriteLine("Ready, press any key to exit..."); Console.ReadKey(); /* * Very important!! * * Dispose always any resource which implements IDisposable * * In this particular case if you don't dispose the pins those would block * the program's end as each pin uses a thread for event detection. * */ up.Dispose(); down.Dispose(); left.Dispose(); right.Dispose(); button.Dispose(); BCM2835.BCM2835Managed.bcm2835_close(); }
static void Main(string[] args) { Console.WriteLine("GPIO FUN!"); var ledPins = new GPIOPin[] { Raspberry.Pins.Pin2, Raspberry.Pins.Pin3, Raspberry.Pins.Pin4, Raspberry.Pins.Pin17, Raspberry.Pins.Pin27, Raspberry.Pins.Pin22 }; var mainTask = Task.Factory.StartNew(() => { // reset GPIO to default state foreach (var pin in ledPins) { Console.WriteLine($"Setting pin {pin.GpioId} mode to output"); pin.ChangeMode(GPIOMode.Out); Console.WriteLine($"Writing 0 on pin {pin.GpioId}"); pin.Write(false); } Console.WriteLine($"Setting pin 20 and 21 mode to input"); Raspberry.Pins.Pin20.ChangeMode(GPIOMode.In); Raspberry.Pins.Pin21.ChangeMode(GPIOMode.In); Raspberry.Pins.Pin20.WaitForEdge(Edge.Falling, HandleSpeedButtonRelease); Raspberry.Pins.Pin21.WaitForEdge(Edge.Falling, HandleModeButtonRelease); Mode = LedMode.LTR; while (!ShuttingDown) { switch (Mode) { case LedMode.LTR: foreach (var pin in ledPins) { pin.Toggle(); System.Threading.Thread.Sleep(Speed); pin.Toggle(); } break; case LedMode.RTL: for (var i = ledPins.Length - 1; i >= 0; i--) { var pin = ledPins[i]; pin.Toggle(); System.Threading.Thread.Sleep(Speed); pin.Toggle(); } break; case LedMode.SuperCar: foreach (var pin in ledPins) { pin.Toggle(); System.Threading.Thread.Sleep(Speed); pin.Toggle(); } for (var i = ledPins.Length - 2; i >= 1; i--) { var pin = ledPins[i]; pin.Toggle(); System.Threading.Thread.Sleep(Speed); pin.Toggle(); } break; case LedMode.Syren: var tasks = new List <Task>(); foreach (var pin in ledPins) { tasks.Add(Task.Factory.StartNew(() => pin.Write(true))); } Task.WaitAll(tasks.ToArray()); System.Threading.Thread.Sleep(Speed); tasks.Clear(); foreach (var pin in ledPins) { tasks.Add(Task.Factory.StartNew(() => pin.Write(false))); } Task.WaitAll(tasks.ToArray()); System.Threading.Thread.Sleep(Speed); break; default: break; } } // reset GPIO to default state foreach (var pin in ledPins) { Console.WriteLine($"Writing 0 on pin {pin.GpioId}"); pin.Write(false); } }); while (!ShuttingDown) { var keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.X && keyInfo.Modifiers == ConsoleModifiers.Control) { ShuttingDown = true; Console.WriteLine("shutting down"); } mainTask.Wait(); } }
public void DigitalWrite(GPIOPin pin, PinState value) { DigitalWrite((uint)pin, value); }
public PinState DigitalRead(GPIOPin pin) { return(DigitalRead((uint)pin)); }