/// <summary> /// Allocates the specified pin. /// </summary> /// <param name="pin">The pin.</param> /// <param name="direction">The direction.</param> public void Allocate(ProcessorPin pin, PinDirection direction) { var gpioId = string.Format("gpio{0}", (int)pin); if (Directory.Exists(Path.Combine(gpioPath, gpioId))) { HelperFileStream.SaveToFile(Path.Combine(gpioPath, "unexport"), (int)pin); } // Export pin for file mode HelperFileStream.SaveToFile(Path.Combine(gpioPath, "export"), (int)pin); // Set the direction on the pin and update the exported list SetPinMode(pin, direction == PinDirection.Input ? Interop.BCM2835_GPIO_FSEL_INPT : Interop.BCM2835_GPIO_FSEL_OUTP); // Set direction in pin virtual file var filePath = Path.Combine(gpioId, "direction"); HelperFileStream.SaveToFile(Path.Combine(gpioPath, filePath), direction == PinDirection.Input ? "in" : "out"); if (direction == PinDirection.Input) { PinResistor pinResistor; if (!pinResistors.TryGetValue(pin, out pinResistor) || pinResistor != PinResistor.None) { SetPinResistor(pin, PinResistor.None); } SetPinDetectedEdges(pin, PinDetectedEdges.Both); InitializePoll(pin); } }
/// <summary> /// Releases the specified pin. /// </summary> /// <param name="pin">The pin.</param> public void Release(ProcessorPin pin) { UninitializePoll(pin); HelperFileStream.SaveToFile(Path.Combine(gpioPath, "unexport"), (int)pin); SetPinMode(pin, Interop.BCM2835_GPIO_FSEL_INPT); }
/// <summary> /// Allocates the specified pin. /// </summary> /// <param name="pin">The pin.</param> /// <param name="direction">The direction.</param> public void Allocate(ProcessorPin pin, PinDirection direction) { Release(pin); HelperFileStream.SaveToFile(Path.Combine(gpioPath, "export"), (int)pin); if (!gpioPathList.ContainsKey(pin)) { var gpio = new FileGpioHandle { GpioPath = GuessGpioPath(pin) }; gpioPathList.Add(pin, gpio); } var filePath = Path.Combine(gpioPathList[pin].GpioPath, "direction"); try { SetPinDirection(filePath, direction); } catch (UnauthorizedAccessException) { // program hasn't been started as root, give it a second to correct file permissions Thread.Sleep(TimeSpan.FromSeconds(1)); SetPinDirection(filePath, direction); } gpioPathList[pin].GpioStream = new FileStream(Path.Combine(GuessGpioPath(pin), "value"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); }
/// <summary> /// Releases the specified pin. /// </summary> /// <param name="pin">The pin.</param> public void Release(ProcessorPin pin) { if (gpioPathList.ContainsKey(pin) && gpioPathList[pin].GpioStream != null) { gpioPathList[pin].GpioStream.Flush(); gpioPathList[pin].GpioStream = null; } if (Directory.Exists(GuessGpioPath(pin))) { HelperFileStream.SaveToFile(Path.Combine(gpioPath, "unexport"), (int)pin); } }
/// <summary> /// Sets the detected edges on an input pin. /// </summary> /// <param name="pin">The pin.</param> /// <param name="edges">The edges.</param> /// <remarks>By default, both edges may be detected on input pins.</remarks> public void SetPinDetectedEdges(ProcessorPin pin, PinDetectedEdges edges) { var edgePath = Path.Combine(gpioPath, string.Format("gpio{0}/edge", (int)pin)); HelperFileStream.SaveToFile(edgePath, ToString(edges)); }
private static void SetPinDirection(string fullFilePath, PinDirection direction) { HelperFileStream.SaveToFile(fullFilePath, direction == PinDirection.Input ? "in" : "out"); }