public void WriteToPin(int pinNumber, bool value) { GpioPin pin = null; if (!activePins.ContainsKey(pinNumber)) { pin = gpio.OpenPin(pinNumber); activePins.Add(pinNumber, pin); } else { pin = activePins[pinNumber]; } if (pin.GetDriveMode() != GpioPinDriveMode.Output) { pin.SetDriveMode(GpioPinDriveMode.Output); } if (value) { pin.Write(GpioPinValue.High); } else { pin.Write(GpioPinValue.Low); } }
/// <summary> /// Check the driving mode of the selected gpio pin. /// </summary> /// <returns></returns> private async Task CheckDriveMode() { if (_selectedPin.GetDriveMode() == GpioPinDriveMode.Input || _selectedPin.GetDriveMode() == GpioPinDriveMode.InputPullDown || _selectedPin.GetDriveMode() == GpioPinDriveMode.InputPullUp) { tglSwDriveMode.IsOn = false; tglSwGpioLevel.IsEnabled = false; tbxTLow.IsEnabled = false; tbxTHigh.IsEnabled = false; btnStartPulseGenerator.IsEnabled = false; btnStopPulseGenerator.IsEnabled = false; } else { tglSwDriveMode.IsOn = true; tglSwGpioLevel.IsEnabled = true; tbxTLow.IsEnabled = true; tbxTHigh.IsEnabled = true; btnStartPulseGenerator.IsEnabled = true; } }
private void InitGPIO112() { //Debug.WriteLine("Main: InitGPIO112"+value); var gpio = GpioController.GetDefault(); // Show an error if there is no GPIO controller if (gpio == null) { Debug.WriteLine("Flash: There is no GPIO controller on this device."); return; } try { gpio.TryOpenPin(Flash_PIN, GpioSharingMode.Exclusive, out flashPin112, out openStatus); status.Text = "Open GPIO state value to " + openStatus; if (openStatus == 0) { Debug.WriteLine("Flash: GPIO pins 112 value1" + flashPin112.Read().ToString() + flashPin112.GetDriveMode().ToString()); flashPin112.SetDriveMode(GpioPinDriveMode.Output); if (gpioValue) { flashPin112.Write(GpioPinValue.High); } else { flashPin112.Write(GpioPinValue.Low); } status.Text = "Init GPIO value to " + flashPin112.Read().ToString(); Debug.WriteLine("Flash: GPIO pins 112 value2" + flashPin112.Read().ToString() + flashPin112.GetDriveMode().ToString()); //status.Text = "GPIO pins initialized correctly. OPEN GPIO 112 successful\n"; Debug.WriteLine("Flash: GPIO pins 112 initialized correctly"); InitGPIO(); } else { Debug.WriteLine("Flash: GPIO pins 112 value" + flashPin112.Read().ToString() + flashPin112.GetDriveMode().ToString()); //status.Text += "OPEN GPIO 112 fail\n"; Debug.WriteLine("GPIO pins initialized fail. OPEN GPIO 112 fail\n"); //toggleSwitch_FLASH112.IsEnabled = false; } } catch (Exception e) { Debug.WriteLine("GPIO pins initialized fail. OPEN GPIO 112 fail\n"); } }
public bool ReadFromPin(int pinNumber) { GpioPin pin = null; if (!activePins.ContainsKey(pinNumber)) { pin = gpio.OpenPin(pinNumber); activePins.Add(pinNumber, pin); } else { pin = activePins[pinNumber]; } if (pin.GetDriveMode() != GpioPinDriveMode.InputPullUp) { pin.SetDriveMode(GpioPinDriveMode.InputPullUp); } return(pin.Read() == GpioPinValue.High); }
/// <summary> /// Writes to digital pin. /// </summary> /// <param name="ch">Digital output channel. 1, 2, 3 or 4.</param> /// <param name="st">True for high, false for low output state.</param> public void WriteDPinState(int ch, bool st) { switch (ch) { case 1: if (d1.GetDriveMode() == GpioPinDriveMode.Output) { d1.Write(st ? GpioPinValue.High : GpioPinValue.Low); } break; case 2: if (d2.GetDriveMode() == GpioPinDriveMode.Output) { d2.Write(st ? GpioPinValue.High : GpioPinValue.Low); } break; case 3: if (d3.GetDriveMode() == GpioPinDriveMode.Output) { d3.Write(st ? GpioPinValue.High : GpioPinValue.Low); } break; case 4: if (d4.GetDriveMode() == GpioPinDriveMode.Output) { d4.Write(st ? GpioPinValue.High : GpioPinValue.Low); } break; default: break; } }
public void WaitForPinHigh(int pinNumber) { GpioPin pin = null; if (!activePins.ContainsKey(pinNumber)) { pin = gpio.OpenPin(pinNumber); activePins.Add(pinNumber, pin); } if (pin.GetDriveMode() != GpioPinDriveMode.InputPullUp) { pin.SetDriveMode(GpioPinDriveMode.InputPullUp); } if (pin.Read() == GpioPinValue.High) { return; } waitForPinChange(pin); return; }
static void Main(string[] args) { UpBridge.Up upb = new UpBridge.Up(); Console.WriteLine(upb.BoardGetManufacture() + "\n" + "Board Name: " + upb.BoardGetName() + "\n" + "BIOS Ver: " + upb.BoardGetBIOSVersion() + "\n" + "Firmware Ver:" + upb.BoardGetFirmwareVersion().ToString("X") + "\n"); Console.WriteLine("Up UWP console GPIO test:"); if (GpioController.GetDefault().PinCount > 0) { GpioPin gpioPin = null; int selpin = -1; while (true) { string input; if (selpin == -1) { Console.WriteLine("please select pin to control(pin %s)"); } Console.Write(selpin.ToString() + ">"); input = Console.ReadLine(); string[] inArgs = input.Split(' '); if (inArgs[0] == "pin") { if (inArgs.Length == 2) { selpin = int.Parse(inArgs[1]); try { gpioPin = GpioController.GetDefault().OpenPin(selpin); }catch (InvalidOperationException ie) { Console.WriteLine(ie.Message); selpin = -1; } } continue; } switch (input) { case "status": Console.WriteLine(gpioPin.GetDriveMode().ToString()); break; case "input": gpioPin.SetDriveMode(GpioPinDriveMode.Input); Console.WriteLine(gpioPin.GetDriveMode().ToString()); break; case "output": gpioPin.SetDriveMode(GpioPinDriveMode.Output); Console.WriteLine(gpioPin.GetDriveMode().ToString()); break; case "high": gpioPin.Write(GpioPinValue.High); Console.WriteLine(gpioPin.Read().ToString()); break; case "low": gpioPin.Write(GpioPinValue.Low); Console.WriteLine(gpioPin.Read().ToString()); break; case "read": Console.WriteLine(gpioPin.Read().ToString()); break; case "list": Console.WriteLine("Available Pins:" + GpioController.GetDefault().PinCount.ToString() + " (start from 0)"); break; case "help": default: Console.WriteLine(Usage); break; } } } else { Console.WriteLine("No available GPIO pins!"); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); } }
public GpioPinDriveMode GetDriveMode() { return(Pin.GetDriveMode()); }
public void Init() { gpioController = GpioController.GetDefault(); if (gpioController == null) { controllerFlag = false; } else { controllerFlag = true; } if (controllerFlag) { if (this.gpioController.TryOpenPin(TRIGGER, GpioSharingMode.Exclusive, out gpioTrigger, out gpioOpenStatusTrigger)) { this.gpioTrigger.SetDriveMode(GpioPinDriveMode.Output); EventGpioStatus(this, new GpioStatusEventArgs($"TRIGGER {TRIGGER} Status: {gpioOpenStatusTrigger} Drive Mode: {gpioTrigger.GetDriveMode()}")); this.gpioTrigger.Write(GpioPinValue.Low); } if (this.gpioController.TryOpenPin(ECHO, GpioSharingMode.Exclusive, out gpioEcho, out gpioOpenStatusEcho)) { this.gpioEcho.SetDriveMode(GpioPinDriveMode.Input); EventGpioStatus(this, new GpioStatusEventArgs($"ECHO {ECHO} Status: {gpioOpenStatusEcho} Drive Mode: {gpioEcho.GetDriveMode()}")); this.gpioEcho.ValueChanged += gpioEcho_ValueChanged; } } else { EventGpioStatus(this, new GpioStatusEventArgs($"No Gpio provider!")); } }
private void ThrowExceptionIfDriveModeIsInput() { if (_isInputPin) { throw new InvalidOperationException($"Can't set state on pin {PinNumber} since it is configured as an input pin ({_gpioPin.GetDriveMode().ToString()})"); } }
public void ReadPinValue(GpioPin pin) { Value = pin.Read(); Mode = pin.GetDriveMode(); }