public static IGPIO CreatePin(GPIOPins pin, GPIODirection dir = GPIODirection.Out, PinState pinState = PinState.Low) { if (_ExportedPins.ContainsKey(pin)) { if (_ExportedPins[pin].Direction != dir) { _ExportedPins[pin].Direction = dir; } if (_ExportedPins[pin].State != pinState) { _ExportedPins[pin].State = pinState; } return(_ExportedPins[pin]); } try { return(new GPIO(pin, dir, pinState)); } #if DEBUG catch (Exception e) { throw new Exception("Unable to create pin " + (uint)pin + ". Infomation: " + e.ToString()); } #else catch //stuff like lib load problems, wrong exports, etc... { return(null); } #endif }
/// <summary> /// Sets up an interface for accessing the specified GPIO pin with the given direction and given initial value. /// </summary> /// <param name="gpioPin">The GPIO pin to be accessed.</param> /// <param name="direction">The direction the GPIO pin should have.</param> /// <param name="initialValue">The initial value the GPIO pin should have.</param> public GPIOPinDriver(Pin gpioPin, GPIODirection direction, GPIOState initialValue) { this._disposed = false; this.GPIOPin = gpioPin; this.Direction = direction; if (this.Direction == GPIODirection.Out) { this.State = initialValue; } }
/// <summary> /// Sets up an interface for accessing the specified GPIO pin with the given direction and given initial value. /// </summary> /// <param name="gpioPin">The GPIO pin to be accessed.</param> /// <param name="direction">The direction the GPIO pin should have.</param> /// <param name="initialValue">The initial value the GPIO pin should have.</param> public GPIOPinDriver(Pin gpioPin, GPIODirection direction, GPIOState initialValue, bool _bSimMode) { SetupPath(_bSimMode); this._disposed = false; this.GPIOPin = gpioPin; this.Direction = direction; if (this.Direction == GPIODirection.Out) { this.State = initialValue; } }
public string RequestListener(HttpListenerRequest request) { GPIOMem gpio = null; string rtnValue = null; try { // log out requests for debugging Console.WriteLine(request.Url); // set pin direction via "dir" argument GPIODirection dir = (GPIODirection)((new[] { "OUT", "TRUE", "1" }).Contains(request.QueryString["dir"].ToUpper()) ? 1 : 0); // set pin number via "pin" argument GPIOPins pin = (GPIOPins)int.Parse(request.QueryString["pin"]); gpio = new GPIOMem((GPIOPins)pin, dir); if (dir == GPIODirection.Out) { // set pin state via "state" argument bool state = (new[] { "HIGH", "1", "TRUE", "T" }).Contains(request.QueryString["state"].ToUpper()); gpio.Write(state); rtnValue = state.ToString(); } else { rtnValue = (gpio.Read() == PinState.High).ToString(); } } catch (Exception ex) { Console.WriteLine("ERROR:"); Console.WriteLine(ex.Message); Console.WriteLine(ex.Source); Console.WriteLine(ex.StackTrace); rtnValue = "ERROR"; } finally { gpio.Dispose(); } return(rtnValue); }
/// <summary> /// Access to the specified GPIO setup with the specified direction with the specified initial value /// </summary> /// <param name="pin">The GPIO pin</param> /// <param name="direction">Direction</param> /// <param name="initialValue">Initial Value</param> protected GPIO(GPIOPins pin, GPIODirection direction, bool initialValue) { if (pin == GPIOPins.GPIO_NONE) throw new ArgumentException("Invalid pin"); lock (_exportedPins) { if (_exportedPins.ContainsKey(pin)) throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (uint)pin + ")"); _exportedPins[pin] = new WeakReference(this); _pin = pin; try { PinDirection = direction; Write(initialValue); } catch { Dispose(); throw; } } }
/// <summary> /// Access to the specified GPIO setup with the specified direction with the specified initial value /// </summary> /// <param name="pin">The GPIO pin</param> /// <param name="direction">Direction</param> /// <param name="initialValue">Initial Value</param> private GPIO(GPIOPins pin, GPIODirection direction, PinState pinState) { if (pin == GPIOPins.GPIO_NONE) { throw new ArgumentException("Invalid pin"); } if (_ExportedPins.ContainsKey(pin)) { throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (byte)pin + ")"); } _ExportedPins[pin] = this; _pin = pin; this.Direction = direction; this.State = pinState; Write(pinState); }
/// <summary> /// Creates a pin if it has not already been created (exported), creates a GPIOMem if possible, otherwise falls back to GPIOFile. /// </summary> /// <param name="pin">The pin to create or export</param> /// <param name="dir">The direction the pin is to have</param> /// <returns>The GPIO instance representing the pin</returns> public static GPIO CreatePin(GPIOPins pin, GPIODirection dir) { lock (_exportedPins) if (_exportedPins.ContainsKey(pin)) { if (_exportedPins[pin].PinDirection != dir) { _exportedPins[pin].PinDirection = dir; } return(_exportedPins[pin]); } try { return(new GPIOMem(pin, dir)); } #if DEBUG catch (Exception e) { System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOMem because: " + e.ToString()); } #else catch //stuff like lib load problems, wrong exports, etc... { } #endif try { return(new GPIOFile(pin, dir)); } #if DEBUG catch (Exception e) { System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOFile because: " + e.ToString()); } #else catch //stuff like GPIO Sys FS does not exist or is not responding, open by another process, etc... { } #endif #if DEBUG System.Diagnostics.Debug.WriteLine("Using debug GPIO pin for pin number " + (uint)pin); return(new GPIODebug(pin, dir)); #else throw new Exception("Cannot access GPIO pin " + (uint)pin + ". Make sure libbcm2835.so is accessible, or that GPIO SYSFS is working and not in use by another process"); #endif }
public GPIO(string number, GPIODirection direction) { this.Number = number; this.Direction = direction; try { UnexportGPIO(); } catch (Exception) { } ExportGPIO(); if (direction == GPIODirection.input) { SetInputDirection(); } else { SetOutputDirection(); } }
/// <summary> /// Access to the specified GPIO setup with the specified direction with the specified initial value /// </summary> /// <param name="pin">The GPIO pin</param> /// <param name="direction">Direction</param> /// <param name="initialValue">Initial Value</param> protected GPIO(GPIOPins pin, GPIODirection direction, bool initialValue) { if (pin == GPIOPins.GPIO_NONE) { throw new ArgumentException("Invalid pin"); } lock (_exportedPins) { if (_exportedPins.ContainsKey(pin)) { throw new Exception("Cannot use pin with multiple instances. Unexport the previous instance with Dispose() first! (pin " + (uint)pin + ")"); } _exportedPins[pin] = this; _pin = pin; try { PinDirection = direction; Write(initialValue); } catch { Dispose(); throw; } } }
/// <summary> /// Access to the specified GPIO setup with the specified direction with the specified initial value /// </summary> /// <param name="pin">The GPIO pin</param> /// <param name="direction">Direction</param> /// <param name="initialValue">Initial Value</param> public GPIODebug(GPIOPins pin, GPIODirection direction, bool initialValue) : base(pin, direction, initialValue) { }
public static void DirectPin(GPIOPins pin, GPIODirection direction) { bcm2835_gpio_fsel(pin, direction); }
/// <summary> /// Access to the specified GPIO setup with the specified direction with an initial value of false (0) /// </summary> /// <param name="pin">The GPIO pin</param> /// <param name="direction">Direction</param> public GPIODebug(GPIOPins pin, GPIODirection direction) : this(pin, direction, false) { }
/// <summary> /// Creates a pin if it has not already been created (exported), creates a GPIOMem if possible, otherwise falls back to GPIOFile. /// </summary> /// <param name="pin">The pin to create or export</param> /// <param name="dir">The direction the pin is to have</param> /// <returns>The GPIO instance representing the pin</returns> public static GPIO CreatePin(GPIOPins pin, GPIODirection dir) { lock (_exportedPins) { if (_exportedPins.ContainsKey(pin) ) { var reference = _exportedPins[pin]; if (reference.IsAlive) { var gpio = (GPIO)_exportedPins[pin].Target; if (gpio.PinDirection != dir) gpio.PinDirection = dir; return gpio; } else { _exportedPins.Remove(pin); } } } try { return new GPIOMem(pin, dir); } #if DEBUG catch (Exception e) { System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOMem because: " + e.ToString()); } #else catch //stuff like lib load problems, wrong exports, etc... { } #endif try { return new GPIOFile(pin, dir); } #if DEBUG catch (Exception e) { System.Diagnostics.Debug.WriteLine("Unable to create pin " + (uint)pin + " as GPIOFile because: " + e.ToString()); } #else catch //stuff like GPIO Sys FS does not exist or is not responding, open by another process, etc... { } #endif #if DEBUG System.Diagnostics.Debug.WriteLine("Using debug GPIO pin for pin number " + (uint)pin); return new GPIODebug(pin, dir); #else throw new Exception("Cannot access GPIO pin " + (uint)pin + ". Make sure libbcm2835.so is accessible, or that GPIO SYSFS is working and not in use by another process"); #endif }
protected static extern void bcm2835_gpio_fsel(GPIOPins pin, GPIODirection direction);
/// <summary> /// Sets up an interface for accessing the specified GPIO pin with the given direction and initial value set to LOW. /// </summary> /// <param name="gpioPin">The GPIO pin to be accessed.</param> /// <param name="direction">The direction the GPIO pin should have.</param> public GPIOPinDriver(Pin gpioPin, GPIODirection direction, bool _bSimMode) : this(gpioPin, direction, GPIOState.Low, _bSimMode) { }
/// <summary> /// Sets up an interface for accessing the specified GPIO pin with the given direction and initial value set to LOW. /// </summary> /// <param name="gpioPin">The GPIO pin to be accessed.</param> /// <param name="direction">The direction the GPIO pin should have.</param> public GPIOPinDriver(Pin gpioPin, GPIODirection direction) : this(gpioPin, direction, GPIOState.Low) { }
public GPIOModel(GPIO gpio) { this.Number = gpio.Number; this.Direction = gpio.Direction; }